I ran this program in GCC(gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)) on Linux. It successfully compiled using gcc myprog.c
and run without any warnings or errors.
So, Why doesn't the compiler give any warnings or errors when only one argument is provided for main
in C?
#include <stdio.h>
int main(int i)
{
printf("%d\n", i);
}
With GCC 6 (on Linux/Debian/Sid/x86-64) my compiler does give some warning when compiling with
gcc -Wall rsp.c -o rsp
yourrsp.c
example.But the C11 programming language (read n1570) does not specify when a compiler should warn. It mostly defines what are the correct possible programs and how should they behave. Read more about undefined behavior (I guess, but I am not sure, that you have one).
If you want to really understand what is actually happening in your case on your computer, study the source code of your compiler, the emitted assembler code (compile with
gcc -S -fverbose-asm -O rsp.c
then look into the generatedrsp.s
), the source code of your C standard library, of your crt0 runtime. Study also the ABI and the calling conventions relevant to your system.I think you should be very scared of undefined behavior. Very bad things could happen.
As a practical advice when coding on Linux, always compile with
-Wall
and-g
passed togcc
The-Wall
option asks for nearly all warnings (and you could add-Wextra
). The-g
option asks for debug information. You'll then be able to use thegdb
debugger to understand more what is happening at runtime.Once you are confident in your code and have debugged it, consider compiling it with
-O
or-O2
to ask for optimization (in particular for benchmarking). (BTW,gcc
can accept both-g
and-O2
). Read documentation on GCC command options.As per the C standard
5.1.2.2.1 Program startup:
If an implementation defines it to accept a single parameter, then it can.