I'm not sure how to include the input from the command-line arguement.

I try to set the arguments equal to variables, bu it's not working.

#include <stdio.h>
#include <cs50.h>

int main(int argc, char* argv[])
    string hash = argv[0];
    int key = argv[1] - '0';

    printf("argc: %s \n", hash);
    printf("key: %i \n", key);

I get the following error:

caesar.c:15:9: error: incompatible pointer to integer conversion initializing 'int' with an expression of type 'char *' [-Werror,-Wint-conversion]
int key = argv[2] - '0';
    ^     ~~~~~~~~~~~~~

1 error generated. make: *** [caesar] Error 1


I read in C Programming Language, 2nd Edition that arg[0] is the program name:

By convention, argv [ 0] is the name by which the program was invoked, so argc is at least 1. If argc is 1, there are no command-line arguments after the program name. In the example above, argc is 3, and argv [0I, argv [ 1], and argv[2] are "echo", "hello, ", and "world" respectively. The first optional argument is argv[ 1] and the last is argv[argc-1]; additionally, the standard requires that argv[ argc] be a null pointer.

changed my initialization as such and testing to ensure argc is correct:

    printf("Usage: ./crack k\n");
    return 1;

string hash = argv[1];
 int key = argv[2] - '0';

1 Answer 1


Please never access argv[1] without checking argc earlier. argv is an array of length argc (so valid indices are 0 to argc-1). If you passed no argument (argc<2), argv[1] might be anything (or not readable at all).

Also, argv[0] is the programme name. argv[1] is the first command-line argument, if any (a pointer to char, so you cannot, or should not, subtract a char).

  • ok thanks for the checking information. I was reading in the C book about argv[0] being the programming name, so apologies for not making use of that in the question. how do I access the information passed in as argv[1] and argv[2]?
    – Ari Kalmen
    Jan 19, 2018 at 18:50
  • If argc is 2, what do you think argv[2] is? Also, if you look closely, there is only one command-line argument. And you still subtract a char from a char* (but as there's no argv[2], there's also no need to do that).
    – Blauelf
    Jan 19, 2018 at 19:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .