Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correctly declaring the main() function in ANSI C [duplicate]

Tags:

c

kr-c

The C standard say:

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 or in some other implementation-defined manner.

However, Kernighan & Ritchie in their second edition (the canonical ANSI C) bible just use:

main()
{
  /* taram pampam ... */

  return 0;
}

Who is right? Does it have to do with function without return value automatic assume to be returning int in C?

like image 914
zaharpopov Avatar asked Nov 19 '09 18:11

zaharpopov


People also ask

What is int main () in C?

int main represents that the function returns some integer even '0' at the end of the program execution. '0' represents the successful execution of a program. The syntax of int main is as follows − int main(){ --- --- return 0; } int main(void) represents that the function takes NO argument.

Should main () always return a value explain with example?

The return value of main() function shows how the program exited. The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value. In C++ language, the main() function can be left without return value.

How do you declare a main function in C?

The main function signatureint main( void ); int main( int argc, char *argv[ ] ); int main( int argc, char *argv[ ], char *envp[ ] ); The main function is declared implicitly by using one of these signatures. You may use any of these signatures when you define your main function.

What is the main () function in C?

The main function in C programming is a special type of function that serves as the entry point of the program where the execution begins. By default, the return type of the main function is int. There can be two types of main() functions: with and without parameters.


3 Answers

Well, if you want ANSI C, then by definition the standard is right.

In C89/C90 the int return type is implied, so the K&R definition would be acceptable.

In C99 this is no longer the case.

The C90 standard has the following wording (5.1.2.2.1 Program startup), which is very similar to the C99 wording (probably most significantly it uses the less strong 'can' instead of 'shall'):

The function called at program startup is named main. The implementation declares no prototype for this function. It can be defined 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[]) { /* ... */ }

If they are defined, the parameters to the main function shall obey the following constraints:

[etc. ...]

There's nothing in that section directly about the fact that leaving off the return type will result in it defaulting to int.

Frankly, I have a hard time finding exactly where that behavior is specified by the standard. The closest I can come is in 6.7.1 (Functions definitions) where the grammar for function definitions indicates that the 'declaration-specifiers' are optional, and the examples say:

Examples:

  1. In the following:

      extern int max(int a, int b)
      {
          return a > b ? a : b;
      }
    

    extern is the storage class specifier and int is the type specifier (each of which may be omitted as those are the defaults)...

like image 94
Michael Burr Avatar answered Sep 28 '22 16:09

Michael Burr


Yes, in C89 (the original C standard), if a function is declared without a return type, it is assumed to return int. C99 requires an explicit return type on all functions.

like image 38
Eric Petroelje Avatar answered Sep 28 '22 16:09

Eric Petroelje


Also, there's a subtle difference (at least in declarations) between main() and main(void) --

main()

is a function (implicitly) returning int and taking an unspecified number of arguments

main(void)

takes no arguments.

like image 36
Steven Schlansker Avatar answered Sep 28 '22 18:09

Steven Schlansker