-1

So I have to write a program that contains two possible options depending on what the user chooses by entering either -i or -w. I'm very new to command line arguments in general and I have no idea how to do this. So far I have:

#include <stdio.h>

int main(int argc, char *argv[])
{
    if(argc == -'i') {
        puts("Test 1");
    }

    else if(argc == -'w') {
        puts("Test 2");
    }  

return 0;
}

It doesn't print anything... Any explanation is greatly appreciated. I'm just trying to understand the logic behind this.

user9537202
  • 25
  • 1
  • 5
  • 1
    `argc` is the *argument count*, `argv` is the *argument vector* where the parameter data is stored. You're looking at the *argument count* in your snippet. – mholle Apr 23 '18 at 23:08

5 Answers5

1

You have to check argv[i] where i is the array number of the command line argument being put in, argv[0] would be the file name called upon and after that argv[1] would be the first statement, argv[2] the next and so on

Scain40
  • 21
  • 3
1

First of all, you are comparing oranges with appels. argc stores the number of arguments. Second, even if you used argv[1], the comparison would be still wrong, because it would be comparing pointers, not the contents. Note that in C a string is a sequence of characters that terminates to '\0'-terminating byte. A char* variable only points to the start of that sequence. The == operator checks for equality of values, in case of pointers (and string literals), it compares whether both pointers point to the same location.

When you want to compare strings, you have to compare the strings themselves, that means you have to compare the contents where the pointers are pointing to. For strings you have to use the strcmp function like this:

#include <stdio.h>

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        fprintf(stderr, "usage: %s option\n", argv[0]);
        return 1;
    }

    if(strcmp(argv[1], "-i") == 0)
        puts("Test 1");
    else if(strcmp(argv[1], "-w") == 0)
        puts("Test 2");
    else {
        fprintf(stderr, "Invalid option '%s'\n", argv[1]);
        return 1;
    }

    return 0;
}

Note that it is important to check first that you have enough command line arguments (the first if of my code). If the user didn't pass any argument, argc will be 1 and argv[1] will point to NULL, which would yield undefined behaviour if it is passed to strcmp. That's why I only do strcmp when I'm 100% sure that argv[1] is not NULL.

Also if you are coding for a POSIX system (linux, mac, etc), then I recommend using getopt for parsing of the command line arguments.

Pablo
  • 13,271
  • 4
  • 39
  • 59
  • 1
    It seems like explaining that the number of command line arguments are given by the *argument count* (`argc`) with a pointer to each argument (stored as a *nul-terminated* string) provided in the *argument vector* (`*argv[]`) helps the concept sink in. – David C. Rankin Apr 23 '18 at 23:56
0
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("The name of the program is %s\n", argv[0]);
    if( strcmp(argv[1], "-i") == 0 ) { puts("Test 1"); }
    if( strcmp(argv[1], "-w") == 0 ) { puts("Test 2"); }  

return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • That would fail if no arguments are passed, as it would be calling `strcmp` with `NULL` – Pablo Apr 23 '18 at 23:11
  • Please edit this question to describe what this code does, in a way to respond to the specifics of the question. https://stackoverflow.com/help/how-to-answer – bignose Apr 24 '18 at 01:17
0

argc means "argument count". meaning the number of arguments

argv is a 2-dimensional array. Strings are 1-dimensional character arrays in C. The second dimension comes from you having multiple String.

So if you want the first String argument, you would access it as follows:

argv[0]

You are also attempting to compare strings, which are more than 1 character long. You should use strcmp to compare strings in C. See How do I properly compare strings?

and if you want to compare equality, you would not use ==, == is for basic data types such as int or char.

0

argc represents the number of parameters that were passed in at the command line including the program name itself.

In c, a character, e.g., 'i' is an 8-bit number representing the ASCII code of the letter i. So your conditional statement if(argc == -'i') is actually checking whether -105 (105 is the ascii value of the letter i) is the number of arguments that was passed to your program.

To check whether the arguments passed in are "-i" or "-w" you need to perform string comparison, using the c library functions or your own algorithm, and you need to be comparing those strings to argv[i] (where i is the position of the parameter you're checking in the program invocation)

user2686305
  • 1
  • 1
  • 2