0

My program needs to accept a non-negative integer in a command-line argument. I also must use

int main(int argc, string argv[])

to declare main.

My Code:

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

int main(int argc, string argv[])
{
    if (argv[1] < 0)
    {
        printf("Give one non-negative integer.\n");
        return 1;
    }
}

My problem: When I input -1 as my command-line argument, my program doesn't printf or stop running. It goes on to the next block of code. What can I do to fix it (bearing in mind that I need to keep that exact declaration of main), and why is this current code wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ting
  • 151
  • 2
  • 2
  • 8
  • 2
    No such thing as "string" in c. Main should be declared as "int main(int argc, char* argv[])" – jpw Nov 05 '13 at 03:15
  • There is when you include `cs50.h` – Lee Taylor Nov 05 '13 at 03:18
  • @LeeTaylor Oh right, didn't notice the header for CS50. – jpw Nov 05 '13 at 03:19
  • If this code actually compiles string is a typedef of `char *` which is pretty confusing IMO – aaronman Nov 05 '13 at 03:19
  • The problem is that argv[1] is a string (actually a char *) and you can't compare that with an integer. Perhaps try `atoi(argv[1]) < 0` – Jerry Jeremiah Nov 05 '13 at 03:22
  • @jpw In C "A _string_ is a contiguous sequence of characters terminated by and including the first null character." C11dr 7.1.1 1. The phrase _string_ is used extensively throughout the C spec. I am curious as to your evidence that there is _No such thing as "string" in c._ – chux - Reinstate Monica Nov 05 '13 at 03:37
  • @chux What I meant to say was that there is no type named string in the C language. Strings as a concept exist of course. Maybe I wasn't clear enough. – jpw Nov 05 '13 at 03:47

2 Answers2

3

argv is an array of strings, so you need a function to convert the string to an integer, atoi is the function you are looking for.

if (atoi(argv[1]) < 0)

In real code, you may check for invalid command line arguments.


I guess in <cs50.h>, there is a line:

typedef char *string;

you should know that the normal way of declaring main is:

int main(int argc, char* argv[])
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

So. Two things.

int main(int argc, string argv[])

Notice the type of argv. It is an array of stings that is of unknown length (0 to n).

Next notice this:

if (argv[1] < 0)

This is a comparison operator between a string and an int. Now, strings can easily have integer values (everything is bits once you go down far enough) so.. if the string's value is less than 0, it'll do as you expect. The problem is that this will probably never fire. Needless to say, C will allow you to do this, because typing for it is kinda... well... it won't care if you do something like compare strings and ints.

You can take a look here: Converting string to integer C which will help you understand how to convert a string to an integer value.

a quick exert from the linked SO question above:

int num = atoi(s);

So you could do instead...

if (atoi(argv[1]) < 0)

or

int argument = atoi(argv[1])
if (argument < 0)
Community
  • 1
  • 1
QuestionMarcs
  • 1,672
  • 1
  • 12
  • 15