c - Check the command line arguments for a negative number -
i trying figure out how detect negative numbers in argc (in c).
code:
int main(int argc, char *argv[]){ printf("\n"); for(int = 0; < argc; i++){ printf("%s\n", argv[i]); //print typed in command line (arguments) //do if statement here? if less zero, great. } } i thinking struggling if statement (hopefully on track). suggestion? thanks.
the output expecting:
$ ./a.out 5 4 6 3 5 -5 5 4 6 3 5 -5 sorry, have negative value. then quit program.
thanks.
just add condition before printf statement in loop
if(argv[i][0] == '-') { printf("sorry, have negative value. \n"); exit (0); }
Comments
Post a Comment