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

Time:11-21

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:

# 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?

CodePudding user response:

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.

CodePudding user response:

main function parameters and the return value is defined by the standard. You shall not provide your own one. But the compiler may accept and compile even non standard types of main.

On the other hand, the main function will not be called by your code so your prototype is not needed at all.

  •  Tags:  
  • c
  • Related