Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is this pre-standard function declaration compiled with modern compiler?

Tags:

c

declaration

I found the following declaration code in the very early sources of C compiler

main(argc, argv)
int argv[]; { 
   return 0;
}

I tried to run it on ideone.com compiling it in "C" mode with gcc-4.7.2 and it compiles fine and even runs successfully

result: Success     time: 0s    memory: 1828 kB     returned value: 0

Now I'm aware that there was a pre-standard way of declaring function parameters this way:

int funct(crc, buf, len)
    int crc;
    register char* buf;
    int len;
{
   //function implementation
}

but in the latter style it's quite clear - the parameters are first just listed, then declared as if they were a kind of local variables and I see all the parameters declared and the return type.

Back to the first code

main(argc, argv)
int argv[]; { 
   return 0;
}

in the former code there're two parameters listed and only one declared and it looks like argv has type array of int.

How is it being treated by the compiler?

like image 760
sharptooth Avatar asked Nov 15 '25 12:11

sharptooth


2 Answers

You are talking about pre-ANSI C, and the style of prototype known as K&R prototypes. For such function declarations, parameters and return values whose types are not specified are deemed to be of type int.

like image 131
David Heffernan Avatar answered Nov 17 '25 01:11

David Heffernan


It is K&R C syntax where compiler won't check the types of arguments and arguments will be default to int.

K&R sysntax still gets suppot from the latest compilers for compatibility.In cases where code must be compilable by either standard-conforming or K&R C-based compilers, the STDC macro can be used to split the code into Standard and K&R sections to prevent the use on a K&R C-based compiler of features available only in Standard C.

like image 34
Dayal rai Avatar answered Nov 17 '25 02:11

Dayal rai