Question

Why my C program doesn't work when I declare the main function like this (I inverted the arguments position) :

int main(char * argv, int argc){
}

I compiled it without problem but I got errors when I run it.

Thanks.

Was it helpful?

Solution

Due to the incorrect main() signature, this is not a valid C program.

Try enabling compiler warnings. My compiler tells me about the problem:

$ gcc -Wall test.c 
test.c:1:5: warning: first argument of 'main' should be 'int' [-Wmain]
test.c:1:5: warning: second argument of 'main' should be 'char **' [-Wmain]

See What are the valid signatures for C's main() function?

OTHER TIPS

Unlike in C++, functions in C are solely identified by their names, not their arguments. E.g. linker will be pretty happy when it sees a 'main' function.

Nevertheless, there are certain assumptions how main() is called by the operating system resp. the runtime environment. When your parameters are wrong, your program will see unexpected values and might crash.

And btw, you probably will see errors or warnings when you enable diagnostics (e.g. '-Wall -W') when building the program.

This is an incorrect main() signature. You may check the main function.

The parameters argc, argument count, and argv, argument vector,1 respectively give the number and values of the program's command-line arguments. The names of argc and argv may be any valid identifier in C, but it is common convention to use these names.

Also check What should main() return in C and C++?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top