0

I have some doubts on what are argc and argv, i cant seem to grasp the concept, for what should i use them and how should i use them?

like i have this program that receives from the command line two integers between -100000 and 100000 computes thir addition and prints the result, while performing all needed check about te number of paramenters and their correctness.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int a, b;
    char ch;

    if (argc != 4)
    {
        printf("ERROR - Wrong number of command line parameters.\n");
        exit(1);

    }

    if (sscanf(argv[1], "%d", &a) != 1)
    {
        printf("ERROR - the first parameter (%s) is not a valid integer.\n",
                argv[1]);
        exit(2);
    }

    if (sscanf(argv[2], "%d", &b) != 1)
    {
        printf("ERROR - the second parameter (%s) is not a valid integer.\n",
                argv[2]);
        exit(2);
    }
    ch = argv[3][0];

    if (ch == 'a')
        printf("The sum result is %d\n", a + b);
    else if (ch == 'b')
        printf("The subtraction result is %d\n", a - b);
    else if (ch == 'c')
        printf("The multiplication result is %d\n", a * b);
    else if (ch == 'd')
    {
        if (b != 0)
            printf("The division result is %d\n", a / b);
        else
            printf("ERROR the second value shoulb be different than 0 \n");
    }
    else
        printf("ERROR parameter (%c) does not correspond to a valid value.\n",
                ch);
    return 0;
}

but how does the program receive from the command line two arguments?? where do i input them?? i am using codeblocks.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
DAN
  • 11
  • 1
  • 2
  • 3
    Possible duplicate of [How to take command line argument in Codeblock 10.05?](http://stackoverflow.com/questions/11888528/how-to-take-command-line-argument-in-codeblock-10-05) – OmG Feb 01 '17 at 14:00
  • 1
    Don't use `sscanf()` for that. Use `strtol()` and check the `endptr` to confirm that it was a valid integer or to find out if it wasn't. Try to compile from the command line. You see, you say command line but you are not using it, you are using an IDE. I am sure that you can set them in the IDE so that it passes the parameters when it executes the program. But it would be much simpler if you compile from the command line like `gcc -Wall -Werror -O0 -g3 my_source.c -o my_executable && ./my_executable`. – Iharob Al Asimi Feb 01 '17 at 14:03

2 Answers2

8
  • argc is the number of parameters passed to your program when it's invoked from command line.

  • argv is the array of received parameters, and it is an array of strings.

Note that the name of the program is always passed automatically. Assuming your program executable is test, when you invoke from terminal:

./text 145 643

argc will be 3: program name and the two numbers
argv will be the char* array {"./text","145","643"}

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
magicleon94
  • 4,887
  • 2
  • 24
  • 53
  • 1
    Except if the program is invoked with `exec()` from another program where you can decide what the first parameter will be. It's normally the program name or more precisely the value used in the command line. For instance if you call the program with the absolute path the path will be included in `argv[0]`. – Iharob Al Asimi Feb 01 '17 at 14:07
  • it may seem like a dumb question, but how do i invoke from terminal? – DAN Feb 01 '17 at 14:08
  • @IharobAlAsimi didn't know that! DAN are you on Windows? – magicleon94 Feb 01 '17 at 14:10
  • Well, I'm not a windows user, but this should help: http://stackoverflow.com/questions/16367190/how-to-run-a-c-program-using-command-prompt And, even though I'm not a codeblock user, this should help too: http://stackoverflow.com/questions/11888528/how-to-take-command-line-argument-in-codeblock-10-05 – magicleon94 Feb 01 '17 at 14:13
  • Thanks!! i'll look it up! – DAN Feb 01 '17 at 14:16
  • Yeah, let me know! – magicleon94 Feb 01 '17 at 14:16
  • Ok, finally i think iunderstood how to use how to use them, and what are they for, thanks! – DAN Feb 01 '17 at 14:26
  • Pleased to have been helpful :) – magicleon94 Feb 01 '17 at 14:26
  • "name of the program is always passed automatically." --> not always. `argc` may be 0. Even if `argc > 0`, "argv[0][0] shall be the null character if the program name is not available from the host environment." is possible. – chux - Reinstate Monica Feb 01 '17 at 14:46
0

When you write a code, say hello.c, you can run it from the terminal, by going to that directory/folder from the terminal, and compile it using a compiler like gcc.

gcc hello.c -o hello

If you are using Windows, with a compiler like Turbo C or Visual Studio, then it would create a .exe file. This creates an executable file.

When you run the file from command line, you can give command-line arguments as a way of input to the program.

On terminal, you could ./hello arg1 arg2, where arg1 and arg2 are the command line arguments to it. To see how to do this in Windows using a compiler like Turbo C, see this link too.

So what are argc and argv[]? Your main functions uses main(int argc, char *argv[]), to take the command line arguments.

  • argc is the number of command line arguments passed. In the above case, that is 3.
  • argv[] is an array of strings, which in this case are 3 strings. argv[1] will be equal to "arg1" and argv[2] will be equal to "arg2". "./hello" will be in argv[0].

So you give your command line arguments in the command line, be it Linux or Windows. The above explanation was more for Linux. See this and this for command line arguments in Turbo C (I do not recommend Turbo C), and this in case of Visual C.

To know more about command line arguments, you can read this.

Mihir Khandekar
  • 108
  • 1
  • 16