I developed many years in C and only now discovered that a program can execute code prior to main() function. Here is a code example
int generateNum(){
// Some malicious code here...
return 5;
}
static int someArray[] = {generateNum(),generateNum()}
int main(){
// Some code here...
}
The function generateNum()
is called twice before main()
.
My questions are
Every C program has a primary (main) function that must be named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, wmain. The main function serves as the starting point for program execution.
The main method is the entry point to your program. If the class that contains the "main" method has static members that need to be initialized or a static code block, this will be executed BEFORE the "main" method. If you put a breakpoint in the object initialization line you will see it runs before the println line.
The answer is yes. We can write program, that has no main() function. In many places, we have seen that the main() is the entry point of a program execution. Just from the programmers perspective this is true.
It's a process that you spawn when you execute your program. The main function is called at the beginning of the program. It is all a part of the same program (i.e. one process).
main
. This can be taken care of by the operating system loader/linker, or by some special module linked against the object file that contained main
. For gcc, this is described here: http://gcc.gnu.org/onlinedocs/gccint/Initialization.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With