Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the C main() function be static?

Tags:

c

static

linker

Can the main() function be declared static in a C program? If so then what is the use of it?

Is it possible if I use assembly code and call the static main() function myself (consider embedded programs)?

like image 319
udpsunil Avatar asked May 29 '09 08:05

udpsunil


People also ask

Is main function a static?

Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class. In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method.

Why main method is static in C?

A main method is static since it is available to run when the C# program starts. It is the entry point of the program and runs without even creating an instance of the class.

Can a function be static in C?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

Which is Cannot be static in C?

Which of the following cannotbe static in C? variable: structures. functions.


2 Answers

No. The C spec actually says somewhere in it (I read the spec, believe it or not) that the main function cannot be static.

The reason for this is that static means "don't let anything outside this source file use this object". The benefit is that it protects against name collisions in C when you go to link (it would be bad bad bad if you had two globals both named "is_initialized" in different files... they'd get silently merged, unless you made them static). It also allows the compiler to perform certain optimizations that it wouldn't be able to otherwise. These two reasons are why static is a nice thing to have.

Since you can't access static functions from outside the file, how would the OS be able to access the main function to start your program? That's why main can't be static.

Some compilers treat "main" specially and might silently ignore you when you declare it static.

Edit: Looks like I was wrong about that the spec says main can't be static, but it does say it can't be inline in a hosted environment (if you have to ask what "hosted environment" means, then you're in one). But on OS X and Linux, if you declare main static, then you'll get a link error because the linker can't find the definition of "main".

like image 193
Dietrich Epp Avatar answered Sep 19 '22 23:09

Dietrich Epp


You could have a static function called main() in a source file, and it would probably compile, but it would not be the main() function because it would be invisible to the linker when the start-up code (crt0.o on many (older) Unix systems) calls main().

Given the code:

static int main(int argc, char **argv)
{
    return(argv + argc);
}

extern int x(int argc, char **argv)
{
    return(main(argc, argv));
}

GCC with -Wall helpfully says:

 warning: 'main' is normally a non-static function

Yes, it can be done. No, it is normally a mistake - and it is not the main() function.

like image 32
Jonathan Leffler Avatar answered Sep 21 '22 23:09

Jonathan Leffler