I wrote the following code:
void main() {
}
How can it run without any header files?
I wrote the following code:
void main() {
}
How can it run without any header files?
From the C Standard (5.1.2.2.1 Program startup)
1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.
Though some compilers as for example the compiler of MS VS support the declaration of the function main with the return type void
nevertheless such a declaration is not a C standard declaration of the function main.
So as the implementation declares no prototype of the function main and if the function main does not call any other function then neither header is required.
You may just write
int main( void )
{
}
The return statement also may be omitted.
Pay attention to that it is the user who defines the function main. So in the presented program above there is a definition of the function main that does not contain any statement inside its body. The function does nothing and at once returns the control to the hosted environment.
Header files are just a language feature that provides means to organize code in different modules (translation units) or even in whole libraries. They are by no means mandatory to use.
The only thing that is mandatory in a C program is to have some manner of entry point to your program. On hosted systems (such as a PC with an OS), this entry point must be a function named main()
. It can be declared in several ways. void main()
is not guaranteed to work unless the compiler explicitly supports that form. The most portable and standardized format is:
int main (void)
{
return 0;
}
Now of course this program is not very exciting to run, as it does absolutely nothing. But it is perfectly valid.
There need not be any forward declaration of main(). For freestanding environments, the format of the entry point is completely implementation-defined. For hosted environments, the C standard explicitly says that "The implementation declares no prototype for this function." - implementation in this case meaning the compiler. In English it means that the main()
function must simply work without any previous declaration present, as per language definition.
void
is a built-in type, known by the compiler. main
is the entry point for a program, while printf
, as you have written, needs some prototype. If you write your own printf
definition in your source code, it will compile without a header.
The only thing you have to do to compile a C program is specify a entry point, i.e. main
.
The headers just provide other IO possibilities.
void printf()
{
}
int main()
{
printf();
}
If you are using printf
in your code without a header, the compiler doesn't know to what type of entity you are refering.
If you provide an implementation of main
, the compiler knows exactly what this is meant to be, you just specified it.