How do argc and argv work in C?

Posted on 2009-04-20
Last Modified: 2012-05-06
I'm not finding a whole lot of information or examples on how this type of sort works.   For example, if I needed to sort a list of words or names a user inputs, which one of these (or both) would I use?  Any examples, answers, explanations, etc. would be appreciated!
Question by:clscor2
    LVL 84

    Accepted Solution

    #include <stdio.h>
    int main(int argc,char *argv[] ){
      int i;
      for( i=0; i<argc; i++ ){
           printf("argv[%d] = %s\n",i,argv[i]);
    LVL 84

    Assisted Solution

    1 The function called at program startup is named main. The implementation declares no
    prototype for this function. It shall be defined with a return type of int and with no
    int main(void) { /* ... */ }
    or with two parameters (referred to here as argc and argv, though any names may be
    used, as they are local to the function in which they are declared):
    int main(int argc, char *argv[]) { /* ... */ }
    or equivalent;9) or in some other implementation-defined manner.
    2 If they are declared, the parameters to the main function shall obey the following
     The value of argc shall be nonnegative.
     argv[argc] shall be a null pointer.
     If the value of argc is greater than zero, the array members argv[0] through
    argv[argc-1] inclusive shall contain pointers to strings, which are given
    implementation-defined values by the host environment prior to program startup. The
    intent is to supply to the program information determined prior to program startup
    from elsewhere in the hosted environment. If the host environment is not capable of
    supplying strings with letters in both uppercase and lowercase, the implementation
    shall ensure that the strings are received in lowercase.
     If the value of argc is greater than zero, the string pointed to by argv[0]
    represents the program name; argv[0][0] shall be the null character if the
    program name is not available from the host environment. If the value of argc is
    greater than one, the strings pointed to by argv[1] through argv[argc-1]
    represent the program parameters.
     The parameters argc and argv and the strings pointed to by the argv array shall
    be modifiable by the program, and retain their last-stored values between program
    startup and program termination.

    9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as
    char ** argv, and so on.
    LVL 9

    Assisted Solution

    LVL 39

    Assisted Solution

    Some good info already given by ozo but just to provide a more simplistic explanation whereas I've used simple terms to try and describe them rather than the more accurate standard definition. You should use these descriptions as a guide whilst using ozo's information as the correct and standard definition.

    char const *argv[]

    This is a vector of pointers to strings. Element 0 will usually be a pointer to the applications executable name as it was typed in to the command line to invoke the program. Elements 1 to n will be the remaining arguments on the command line as they were entered, using space to separate each argument (unless it was surrounded by quoted when entered).

    int argc

    Is the size of argv, so your argument range will be 0 to (argc-1).

    Author Closing Comment

    Thank you for your help!

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now