0

I'm a complete beginner doing the cs50 course and I need to check if an argument from a user is a digit or not.

this is the code:

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

int main(void)
{
  int i = 1;
  if (isdigit(i) == 1)
    {
      printf("i is a digit");
    }
  else if (isdigit(i) == 0)
    {
      printf("i is not a digit");
    }
return 0;
}

When I run this code I basically get a reverse of what I should be getting. When i is a number it prints out "i is not a number" and visa versa. What am I doing wrong? I thought isdigit returns a non-zero value if it is a digit and 0 if not. Basically 1 being true and 0 being false. Is this not correct? Much appreciated, Thanks!

U Kushi
  • 17
  • 1
  • 5
  • 1
    Perhaps some documentation would help: [`isdigit`](https://en.cppreference.com/w/c/string/byte/isdigit) – WhozCraig Jun 30 '20 at 16:42

5 Answers5

4

What am I doing wrong?

"The isdigit function tests for any decimal-digit character". i with a value of 1 is not a digit character.

Try i = '1';. Then i will have the value of a digit character.

Code is testing the return value incorrectly. @tadman. is...() returns 0 or non-zero.

// if (isdigit(i) == 1)
if (isdigit(i))

Note: is...(int ch) functions are only valid for ch in the unsigned char range and EOF.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

If you read the documentation for isdigit() you'll note the return value is expressed as:

Non-zero value if the character is a numeric character, zero otherwise.

In other words, don't compare to exactly one, that's not assured. Compare to non-zero.

That being said, this works on characters not integers, although in C the line is blurred. What you want is to ensure this is part of a string, like:

char* n = "12345";

if (isdigit(n[0]) == 0) {
  ...
}

In your case you're asking if ASCII character 1 is a digit, which it is not. That's the "Start of Heading" (SOH) control character.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    right got it, thanks! Basically I'm trying to make sure that argv[1][i] are all digits – U Kushi Jun 30 '20 at 16:52
  • There's an [answer for that](https://stackoverflow.com/questions/41776955/check-if-a-string-has-only-numbers-in-c) if that's your ultimate goal. – tadman Jun 30 '20 at 16:57
0

isdigit is a character classification function.

It will return zero or non-zero depending on if a character (or rather, a character promoted to an int) is a digit.

For example:

int i = '1';  // Initialize to the character '1'
if (isdigit(i))
  {
    printf("i is a digit");
  }
else
  {
    printf("i is not a digit");
  }

Note that isdigit returns a non-zero value for digit characters, it doesn't have to be 1.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The argument to isdigit should be a single character, not a number. Thus your test code should more properly be

#include <stdio.h>
#include <ctype.h>

int main(void)
  {
  char c = '1';

  if(isdigit(c))
    printf("c is a digit");
  else
    printf("c is not a digit");

  return 0;
  }
0

Your code should stick to the definition of the function, and not make assumptions about the returned value if it is a digit - the actual returned value may be implementation dependent, here's a better C-style, though as others have have said, you should be passing in a char, not an int - did the compiler not complain about that?

if (isdigit(i)) {
  printf("i is a digit");
} else {
  printf("i is not a digit");
}
Snips
  • 6,575
  • 7
  • 40
  • 64