Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when you don't follow the practice of argv and argc [duplicate]

Possible Duplicates:
main(int argc, char *argv[])
Main's Signature in C++

If i write:

int main(int argc, char** argv)

i get proper commandline input. What would happen if i wrote say,

int main(int foo, double fooDouble, fooType FooVar)

Is this OS-dependent or does it depend on the compiler?

like image 426
Tim Avatar asked Jun 30 '11 12:06

Tim


People also ask

What is the purpose of argv and argc?

The first parameter, argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. The second parameter, argv (argument vector), is an array of pointers to arrays of character objects.

Why do we need argc?

Amongst other things, it allows for quick checking that the correct number of arguments has been passed. 2 ... argc shall be the number of arguments passed to the program from the environment in which the program is run. ....

What does argc and argv indicate in command line arguments ?( Assuming int main int argc char * argv [])?

argv and argc are how command line arguments are passed to main() in C and C++. argc will be the number of strings pointed to by argv . This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.


2 Answers

Given that it does compile, it will still only be called with the argc and argv arguments.

So your fooDouble will get the pointer value of argv, and FooVar will get whatever value is in that register/stack space used for that argument position (which may not have been initialized by the callee, so it may hold any undefined value).

like image 85
Kaos Avatar answered Oct 27 '22 00:10

Kaos


This code doesn't even have to compile. If it does, undefined behaviour may occur.

like image 40
ThiefMaster Avatar answered Oct 26 '22 23:10

ThiefMaster