Is this comparison valid?

As the title says, I'm wondering if this comparison with characters is valid. I want to check if the character is a number from 0 to 9.

1
2
3
4
char c;
if(c >= '0' && c <= '9'){
    do something cool;               
}
Last edited on
What does confuse you (except that c is not initialized)?

By the way it could be written more clearly

if ( std::isdigit( c ) )
Last edited on
Don't worry about it not being initialized, I just wanted to show you that is a character. It is initialized in the final version of my program, I just didn't want to paste that here because that would be a mess. But basically, I wonder if the code c >= '0' && c <= '9' is a valid comparison.
The only question is whether you mean that c has to have 0 - 9 or '0' -'9'.
Yes, it is because I have a number that is not assembled yet. So I can check if a character is a digit by typing if ( std::isdigit( c ) )? That is way easier.
You should include header <cctype> that to use std::isdigit.
Or <locale>, right?
The function defined in <locale> has two parameters.
@GoranGaming - Yes, that is a valid comparison. I used that too in my chess program that I am writing.

A chess program? That's got to be very complicated, especially the AI for the computer.
Topic archived. No new replies allowed.