Rebuttal to stackoverflow comment
I don't see any meaningful reason for any modern compiler to make more than one pass over the source code
This is quite an interesting claim considering there are 3 different languages that I can think of that perform multipasses. Those languages include Java, C# ( link and link ) and Python ( link ).
So, the concept of "looking ahead" is obviously present in C++, which is what is often referred to as "multi-pass compilation"
The illusion of looking ahead is not because of a multi-pass compilation at the parsing stage but it's just a look ahead.
gcc ( link ) uses a recursive decent single pass for C/C++. Recursive decent parser is a left-to-right single pass parsing algorithm ( link ). The recursive decent algorithm allows you look more than one token ahead.
Claims that C++ is "one pass" from that point of view are patently incorrect.
As stated - gcc utilizes a recursive decent parser which is only single pass.
C language does not require prototypes.
This is true.
However, there is a difference between what is true and what you should do.
According to the Open C book "Unless your stuck with an old compiler, function declarations should always be prototypes and this book uses the terms interchangeably.". Also stated "All identifiers in C need to be declared before they are used. This is true for functions as well as variables. For functions the declaration needs to be before the first call of the function. A full declaration includes the return type and the number and type of the arguments. This is also called the function prototype." ( link ).
This is perfectly valid C code: http://codepad.org/Jul73yc7
However, what is missing is what is spit out from the compiler. For both C89 and C99 it outputs for gcc -Wall sotest.c
sotest.c: In function ‘main’:
sotest.c:4:3: warning: implicit declaration of function ‘foo2’ [-Wimplicit-function-declaration]
sotest.c:4:7: warning: unused variable ‘i’ [-Wunused-variable]
Assuming the return type is of non-pointer. If the return type is a pointer then you must either use a function declaration or a prototype. Which according to the Open C book - we should use a prototype (however in this example it doesn't matter because the function takes no parameters anyways).
This does not compile: http://codepad.org/1UDVejt7
However when I add a prototype/function declaration it does compile - http://codepad.org/cFwswnWd
Here is another example of what happens when you lack a prototype: http://codepad.org/lqqpayZa
Declarations and prototypes are two completely different things.
According to the Open C book - "Older versions of the C language didn't have prototypes, the function declarations only specified the return type and did not list the argument types. Unless your stuck with an old compiler, function declarations should always be prototypes and this book uses the terms interchangeably." (link).