Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to understand "main function's prototype cannot be supplied by the program"?

I read main function, and came across following words:

The main function has several special properties:

  1. A prototype for this function cannot be supplied by the program.

Then I wrote a simple program:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# cat foo.c
int main(void);

int main(void)
{
    return 0;
}

And compiled it:

# gcc -Wall -Wextra -Wpedantic -Werror foo.c
#

All seems OK! So I am little confused about how to understand "A prototype for this function cannot be supplied by the program". Anyone can give some insights?

>Solution :

The C standard (5.1.2.2.1) just says that the compiler (for hosted systems like PC etc) will not provide a prototype for the main function. So cppreference.com isn’t really correct, the C standard doesn’t prohibit the application programmer from writing a prototype, although doing so is probably meaningless practice in hosted systems.

In freestanding systems (embedded systems etc), it might be meaningful to declare a prototype for main in case it needs to be called from a reset ISR or from the "C runtime" (CRT).

What’s important to realize no matter system is that the compiler specifies which forms of main that are valid. Never the programmer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading