0

So I'm working on the first part of Caesar and this is what i have so far :

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

int main (int argc, string argv[])
{
    if (argc == 1)
    {
        printf ("Success");
    }
    else
    {
        printf ("Usage: ./ceasar key");
    }
}

This is clearly wrong since it won't work but i don't know how to check the number of command-line arguments without having to actually use string argv[]. The program seems to demand that if I'm to enter another argument then I have to also use it in the program but at this stage I don't want to, I just want to check that it exists without printing it. What am I doing wrong ?

1
  • Also I'm not sure why my program seems to show that way in the question, I did indent it and write it properly in the body but it keeps showing up that. Today is not my day – Devika Desai Jan 17 '19 at 17:32
0

argv[0] is the name of the executable itself (might be empty string if name is not available), so the first parameter would be argv[1], meaning you have to check for argc being 2, not 1.

Edit: I misread your question. You need to use all variables. A very simple way is to typecast the variable to void, which is always possible:

int main(int argc, char* argv[])
{
    printf("argc is %i\n", argc);
    (void)argv; // does nothing, but it "uses" the variable
}
2
  • the thing is regardless of what I check for, whether argc being 2 or 1 or 4, the system won't let me run the program if i don't use argv[1] . So if I just write printf ("Success"), then I get an error stating 'unused parameter string argv - how do I bypass that ? – Devika Desai Jan 19 '19 at 0:09
  • That's a compiler warning, made into an error via -Werror, you can circumvent this error by "using" the variable with some nonsense like (void)argv;, which is a type conversion to void, which is always possible, just does nothing. – Blauelf Jan 19 '19 at 21:43

You must log in to answer this question.

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