Results 1 to 5 of 5
Cannot understand how the variables, or whatever they are called, argc and argv get their values from. For example argc seems to equal 4, why? This is an example program ...
- 08-03-2011 #1
C++ newbie puzzled by how things have aquired their value?
Cannot understand how the variables, or whatever they are called, argc and argv get their values from. For example argc seems to equal 4, why? This is an example program with the netbeans IDE. (Incidentaly the IDE itself tells you not to use endl like that.
)
Code:#include <iostream> int main(int argc, char**argv) { // Prints welcome message... std::cout << "Welcome ..." << std::endl; // Prints arguments... if (argc > 1) { std::cout << std::endl << "Arguments:" << std::endl; for (int i = 1; i < argc; i++) { std::cout << i << ": " << argv[i] << std::endl; } } return 0; } Output: Welcome ... Arguments: 1: arg 1 2: arg 2 3: arg 3 4: arg 4 RUN SUCCESSFUL (total time: 572ms)
- 08-03-2011 #2
argc and argv in most languages are the parameters passed from the command line when calling the program. I don't think this is any different in c++. I don't think there is anything but convention stopping you naming them ben and jerry.
argc is the count and in some languages includes the program name in the count
argv is an array of argument valuesLast edited by elija; 08-03-2011 at 06:13 AM.
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 08-03-2011 #3
Let's take a magical journey into the land of how executing a program works!
When you run a program, the OS ends up setting up a bunch of things for you. This is pretty much all handled as a part of the execve() system call. This does a number of things, but the last thing it does is to push the program's arguments onto the stack as arguments to the main function, and call the main function. So if I execute a program like "grep -i foo file", the last few assembly instructions of the program execution look like this in pseudoassembly:
So now the main function executes, with two arguments: the first being 4, and the second being the address of the argv array.Code:MOV [argv], "grep" MOV [argv+4], "-i" MOV [argv+8], "foo" MOV [argv+12], "file" PUSH argv PUSH 4 CALL main
So these values are set by the operating system when the program is launched. It does not matter what you call them in C/C++, but argc and argv are the standards, and some programming languages such as Perl and Ruby do require these names.
In fact, you do not need these at all. This is a valid program:
The parameters are still there, but not accessible to my program. Which is valid.Code:#include <stdio.h> int main() { puts "Hello, world!"; return 0; }
There is, in fact, even a third argument called "envp" that almost nobody uses. But this is the array of environment variables. It is usually accessed by the standard function getenv(). See the man page for execve() for more details.
Hope this helps.Last edited by Cabhan; 08-03-2011 at 05:47 PM.
DISTRO=Arch
Registered Linux User #388732
- 08-04-2011 #4
Great thanks, cannot say I fully understand but have vaughly got it.
Incidentally:Code:#include <stdio.h> int main() { puts "Hello, world!"; return 0; } Output: gcc -o hyperSimple.cpp hyperSimple.c++ hyperSimple.c++: In function ‘int main()’: hyperSimple.c++:4: error: expected ‘;’ before string constant
Or am I being stupider than usual.
- 08-04-2011 #5
Er, yeah, I totally know how to write C <_<. I swear.
Parentheses <_<.Code:#include <stdio.h> int main() { puts("Hello, world!"); return 0; }
And yeah, understanding Assembly is fun, but it's also a huge pain, so I wouldn't worry too much about it. The main point to get from my post is that when a program is launched, the OS basically calls the program's main function with the argc and argv values, whether you use them or not.DISTRO=Arch
Registered Linux User #388732


1Likes
Reply With Quote