My code is attempting to;

  • take user input from the command line;
  • check if the input is a whole number;
  • confirm positively if so;
  • confirm negatively if not;
  • however all numbers return a negative message - what's going wrong?
int main (int argc, string argv[])

    int k = atoi(argv[1]);

    if (isdigit(k))

        printf("is a number\n");


        printf("is not a number\n");


1 Answer 1


This is a common newbie mistake. The isdigit() function, like all of it's issomething functions take a single char as input. But, argv[1] is a string, so feeding a string to this function will cause it to choke and cough up a seg fault.

Further, in this case, k is an int. The isdigit() function simply won't operate on an int.

There's also the problem that if there's a non-digit in the string argv[1], then it will not convert the string to an int. (I don't remember if it throws an error or just puts 0 in the int. You can look it up.)

The right way to do this is to apply isdigit() to each char in the string argv[1] string, say, by using a for loop. Only after confirming that each char really is a digit should you then use atoi to store the actual number in an int.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Great - thanks Cliff. Solved.
    – Shaun
    Jul 31, 2021 at 14:45

You must log in to answer this question.

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