K&R style function definition problem
Asked Answered
F

1

14

The following code works:

int main()
{
   void foo(int);
   foo(3);
   return 0;
}
void foo(a) int a;
{
   printf("In foo\n");
}

but this one does not:

int main()
{
   void foo(float);
   foo(3.24);
   return 0;
}
void foo(a) float a;
{
   printf("In foo\n");
}

Why does this happen?

Fcc answered 29/1, 2011 at 18:1 Comment(3)
What do you mean by works? Does it compile or not? Does it run or not? What's the problem?Peeved
Because the 2nd example hasn't been valid syntax for longer than either of us has been alive?Evalynevan
@Nick: No, it doesnt compile. This is the error: conflicting types for ‘foo’.Fcc
S
22

Actually, kind of an interesting question.

This has to do with the evolution of the C language and the way it arranges to be backwards-compatible to the older flavors.

In both cases, you have a K&R-era definition for foo(), but a C99 declaration (with prototype) earlier.

But in the first case, the default parameter of int actually is the parameter, so the function call is compatible.

In the second case, though, the K&R definition brings in the standard argument promotions rule from the K&R era and the type of the parameter is really double.

But, you used a modern prototype at the call site, making it a float. So the code at the call site might have pushed a real float, which is in any case a distinct type from double.

If all of the references to foo() were K&R style, I believe the most you would get would be a warning, which is what compilers would have done back then, and the compiler must act like that to compile the legacy code. It would even have been a type-safe call because the floats would all be promoted to double, at least for the procedure call interface. (Not necessarily for the internal function code.)

Shy answered 29/1, 2011 at 18:10 Comment(4)
Strictly speaking, I should have said that the rules that include promoting float arguments to doubles are called the default argument promotions. Also, we usually refer to any version of ANSI C as C99, because the C99 standard replaced the C89 one. But since we were specifically discussing language evolution, perhaps I should have noted that the prototype expressions originated with C89.Shy
Strictly speaking, ANSI C has been obsolete since 1990, since ANSI C = C89. The first ISO C standard is C90. Though of course, ANSI C and C90 are identical in function, and only different in chapter enumeration and various other cosmetics.Garibay
Sure, I was trying to refer to revisions with prototypes, which began with ANSI C, but I guess "ISO C" would have been a better choice.Shy
the question is good but I never will write something like this, always need a prototype in the headerLanda

© 2022 - 2024 — McMap. All rights reserved.