Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no error when passing command line arguments when declaring main as `int main(void)`?

Case 1:

void hello(void) {
    //something
}

int main()
{
    hello(1); //error
    return 0;
}

Case 2:

int main(void) {
    //something
    return 0;
}

Execution:

./a.out something something //No error, Why?

Why there is no error? main will not be able to take any arguments. So why it is possible to provide arguments from the command line?

like image 578
Jagdish Avatar asked Oct 24 '15 10:10

Jagdish


People also ask

Can we pass arguments in main ()?

Yes, we can give arguments in the main() function. Command line arguments in C are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution. The argc and argv are the two arguments that can pass to main function.

When command line arguments are passed to the main?

Explanation: Command line arguments are the arguments that passed to the main function when the program is starting its execution. 2.

Can command line arguments be converted into int?

Can command line arguments be converted into int automatically if required? Explanation: All command Line arguments are passed as a string. We must convert numerical value to their internal forms manually.

What is the benefit of command line arguments?

A command-line argument allows us to provide an unlimited number of arguments. The data is passed as strings as arguments, so we can easily convert it to numeric or other formats. It is useful for configuration information while launching our application.


2 Answers

Because the C compiler and the command line interpreter (or whatever is used to invoke your program) are different things.

The C language allows various ways how main () could be declared.

The command line interpreter will make any arguments available to the program. If the program ignores them, that's none of its business.

The command line interpreter doesn't even know that you used C to compile your program. On my computer, the program could be written in C, C++, Objective-C, Objective-C++, Swift, Fortran, Ada, and so on. Each of these compilers may or may not do things to accept commands from the command line.

like image 143
gnasher729 Avatar answered Oct 23 '22 17:10

gnasher729


Not checking the specification nor compiled result, it will cause no error because the C runtime will get the arguments and pass them to main(), but this type of main() will ignore the passed arguments, and if it is caller's duty to clean up the memory (stack) used as the arguments, it will cause no problems just as getting some arguments and not using them in the code.

This code won't emit errors in C:

void hello(); // in C, the compiler won't check arguments

int main() {
    hello(1); //no error
    return 0;
}

void hello(void) {
    //something 
}
like image 9
MikeCAT Avatar answered Oct 23 '22 16:10

MikeCAT