Find the answer to your Linux question:
Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Cabhan
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 ...
  1. #1
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    231

    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)
    All power is violence; all power is evil.
    Money As Debt

  2. #2
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,300
    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 values
    Last 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.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:

    Code:
    MOV [argv], "grep"
    MOV [argv+4], "-i"
    MOV [argv+8], "foo"
    MOV [argv+12], "file"
    PUSH argv
    PUSH 4
    CALL main
    So now the main function executes, with two arguments: the first being 4, and the second being the address of the argv array.

    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:
    Code:
    #include <stdio.h>
    
    int main() {
        puts "Hello, world!";
    
        return 0;
    }
    The parameters are still there, but not accessible to my program. Which is valid.

    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.
    elija likes this.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    231
    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.
    All power is violence; all power is evil.
    Money As Debt

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Er, yeah, I totally know how to write C <_<. I swear.

    Code:
    #include <stdio.h>
    
    int main() {
        puts("Hello, world!");
    
        return 0;
    }
    Parentheses <_<.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...