U Kushi
U Kushi

Reputation: 17

How to use isdigit function in C

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!

Upvotes: 0

Views: 2034

Answers (5)

chux
chux

Reputation: 153457

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.

Upvotes: 4

Snips
Snips

Reputation: 6763

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");
}

Upvotes: 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;
  }

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

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.

Upvotes: 0

tadman
tadman

Reputation: 211580

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.

Upvotes: 2

Related Questions