How C Compiler identifies main() function in the C Program ???
How C Compiler identifies main() function in the C Program ???
The assembly stub code that starts up the program specifically calls main(). The prototype for main () is given to you by your compiler documentation. If you change main() to an unrecognizable signature the program will complain about an unresolved external reference to _main(<blah, blah, blah>).
In other words, the compiler doesn't "recognize" main [1].
When you compile a program, the compiler produces an object file. It then links [2] the object file from your source code with a startup runtime component (usually called crt0.o[bj]) and the C library components (such as printf).
crt0.o or whatever it is called MUST be first. It contains a label called "start" or something similar. That is where the application will start. From start (possibly via other stops on the way), main is called.
[1] Some compilers DO treat a function called main as "special" - but that's not the rule, and it's for other purposes than "where to start" - gcc puts some extra startup-code in C++ code, to run the global object construction before the main code of your application starts, for example.
[2] For more complex programs, the process is to compile many source files to object files, and when all have been compiled, the linker is invoked.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.