0

I've just met a weird-looking function declaration syntax in C, which is invalid in C++. It goes like this:

#include <stdio.h>

void my_func (foo)
     int foo;
{
    printf("my_func printf foo = %d\n", foo);
}

int main () {

    my_func(1089);

    return 0;
}

From what I could infer, it seems like it is a function definition, for which all parameters' types default to int, and if I replace int foo; by float foo;, I can change the argument's type.

But can anyone confirm that this is only what is happening, why does this syntax exit? Is it a relic from the past? Why is it invalid when compiled with g++, etc.?

Thanks!

nschoe
  • 1,967
  • 1
  • 19
  • 26
  • 3
    That is old K&R syntax. – JFMR Oct 13 '17 at 09:43
  • 1
    it's old K&R style. don't use that. – Jean-François Fabre Oct 13 '17 at 09:43
  • I was not planning on using it. I was merely "baffled" because it looked totally not semantically-valid. Sorry fro duplicate, I searched but did not really know what / how to search. Why is it still valid? – nschoe Oct 13 '17 at 10:04
  • @nschoe It's still "valid" because compilers have to handle legacy code. Code doesn't get rewritten just because the language standard changes. And if you run into C code that uses K&R function definitions, do not add currently-correct function declarations/prototypes to the code base unless you also redefine the function. A K&R function definition combined with a current-C-standard-compliant declaration/prototype is going to be badly broken. – Andrew Henle Oct 13 '17 at 10:51

0 Answers0