c – Why does the following program output 1?

Question:

main(_)
{
    printf("%d",_);
}

 gcc -o test.c test

test.exe

one

Answer:

From the standard (§ 5.1.2.2.1) it follows that it is impossible to write the main function like this, and the result depends on the specific implementation:

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.

I will assume that in your case:

  1. The typeless argument is treated as an int , as in older versions of C. The same applies to the return value of the function.
  2. argc maps to this variable (if you run the program with no arguments, argc == 1 ).

You can check this version by passing command line arguments to the program and checking how the program's output changes.

To make the compiler switch to a more modern dialect of the language, try the -std=c99 switch.

Scroll to Top