-1

I wrote the following code:

void main() {

}

How can it run without any header files?

Nick
  • 6,012
  • 15
  • 35
  • 41
  • 3
    You have to write the definition of `main()` and just did it. – Arkadiusz Drabczyk Aug 07 '17 at 12:35
  • So is printf.But we have for it,what about main() @FilipKočica – Adesh Thakare Aug 07 '17 at 12:36
  • The only thing that you need to run a C program is a `main` function, it won't do much without including headers such as for IO, but you don't need them – Nick Aug 07 '17 at 12:37
  • Possible duplicate of [Do all C functions need to be declared in a header file](https://stackoverflow.com/questions/35355727/do-all-c-functions-need-to-be-declared-in-a-header-file) – Bill the Lizard Aug 07 '17 at 12:40
  • @BilltheLizard He doesnt ask if main has to be in header, he asks how it is possible to run program without any header. – kocica Aug 07 '17 at 12:53
  • @FilipKočica And that's answered by knowing that not all functions need to be declared in a header file. – Bill the Lizard Aug 07 '17 at 12:54
  • @BilltheLizard No, that's answered in comment below. :-) He doesn't ask for any function declaration, declaration could be in source file as well as in header. – kocica Aug 07 '17 at 13:00
  • Undefined behaviour. `main` must be declared `int main(void);`, `int main(int argc, char *argv[]);` or equivalent, or other in implementation-speficied manner. – Antti Haapala Aug 07 '17 at 13:07

4 Answers4

6

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.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
  • And what makes you think this is a hosted environment? See this: https://stackoverflow.com/a/31263079/584518 – Lundin Aug 07 '17 at 13:22
  • @Lundin If it is not a hosted environment then the author of the question mentioned this. – Vlad from Moscow Aug 07 '17 at 13:38
  • My point is that it cites the _sub-chapter_ concerning hosted environments, ignoring that other forms of main() are perfectly fine. So the quote does not prove the format of main(), but only shows some common formats for hosted systems. – Lundin Aug 07 '17 at 13:48
  • [This guy made the same mistake](http://www.stroustrup.com/bs_faq2.html#void-main) - that whole FAQ is incorrect in the context of "the C language" because it completely ignores the preceding text in another sub-chapter 5.1.2.1: "In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined." – Lundin Aug 07 '17 at 13:49
  • 3
    The implicit assumption that very much holds is that anyone who doesn't know the difference between a freestanding and a hosted environment - and therefore doesn't even consider mentioning it in the question - is using the latter. – Antti Haapala Aug 07 '17 at 14:09
  • 1
    @AnttiHaapala Tell that to the average electrical engineer who dabbles in C programming on the side. Or to all those mediocre PC programmers who have somehow ended up writing embedded systems programs. They don't have a clue about what the standard says and will just toss in some form of main() by chance. There are even embedded systems tool vendors who get this wrong - many IDE:s that use gcc fail to set `-ffreestanding` as the default option and therefore force their users to use a nonsensical form of main(), leading to wasted system resources. – Lundin Aug 10 '17 at 13:41
4

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.

Details regarding the various allowed forms of main().

Lundin
  • 155,020
  • 33
  • 213
  • 341
  • Also note that `void main()` is an obsolete format that may not work in future versions of the language. Only `void main (void)` is guaranteed to work in the future. – Lundin Aug 07 '17 at 13:55
2

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();
}
kocica
  • 6,172
  • 2
  • 11
  • 34
  • 2
    and having `main` declared with `void` has undefined / implementation-defined behaviour so it need not work at all. Redefining `printf` has undefined behaviour too. – Antti Haapala Aug 07 '17 at 13:05
  • 1
    Ofc i know it, it was just explanation of how these program compilation works, you cant tangle beginners head with things like implementation-defined / undefined behaviour etc. Thanks for downvote anyway :-). – kocica Aug 07 '17 at 13:09
1

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.

Jens Gustedt
  • 72,200
  • 3
  • 92
  • 164