# ch-'0'

• 11-13-2003
kes103
ch-'0'
I came across the following code in a parsing example in a book on C++
Code:

if(ch>='0' && ch<= '0')    //if its a digit,
s.push(ch-'0');            //save numerical value

Question is: What does ch-'0' accomplish? Is it removing the null from a string?
• 11-13-2003
JaWiB
>>if(ch>='0' && ch<= '0')

Seems odd to me...Wouldn't that be the same as
if (ch=='0')

Not sure what it is supposed to do, but maybe it helps to keep in mind that '0' is a character, not the number 0
• 11-13-2003
major_small
Re: ch-'0'
Quote:

Originally posted by kes103
I came across the following code in a parsing example in a book on C++
Code:

if(ch>='0' && ch<= '0')    //if its a digit,
Question is: What does ch-'0' accomplish? Is it removing the null from a string?

that doesn't determine if it's a digit... chars are not digits... they're chars...
• 11-13-2003
Salem
Well
'0'-'0' is 0
'1'-'0' is 1
'2'-'0' is 2
...
'9'-'0' is 9

It's a quick char-to-num conversion for a single digit
• 11-13-2003
alphaoide
Re: Re: ch-'0'
Quote:

Originally posted by major_small
that doesn't determine if it's a digit... chars are not digits... they're chars...
a character could be a digit, an alphabet, etc.
• 11-13-2003
DougDbug
Type-char & ASCII characters
Computers only work with numbers (binary numbers, actually). So, letters and symbols are stored as numbers... usually using the ASCII code. So, a type-char always holds a numeric value. That value may represent an alpha-numeric ASCII character, or a simply the number.

If you want to print the number 2 on your printer, you don't send a 2. You send 50 which is the ASCII code for character 2. If you want to print the letter A, you send 65.

The method used in your book (and in Salem's example) works because the ASCII codes are in sequence... the value for an ASCII 2 (50), is 2 more than the ASCII value for a zero (48).

When you use single quotes 'A', this is the same as the number 65 to the compiler. If you have a type-char variable which holds the value 65, it may be used as the number, or the letter A. It depends on how it's used by the program.

When you use (normal) double-quotes "A", you do not get a single character. You get a c-style string (AKA string array, or null-terminated string). "HELLO" is an array of six numbers... 5 ASCII characters plus a null-terminating zero. "A" is an array of two numbers. 'HELLO' with single quotes will give you an error, because it's more than one number.
• 11-13-2003
major_small
actually, if you want to print a 2, you would be sending a 110010... ASCII just assigns a binary number to each character...

>>a character could be a digit, an alphabet, etc.
try doing math with a char and say that again...
• 11-13-2003
the Wookie
what you're basically doing it subtracting the ascii value for zero from the ascii value of 0

so i think the ascii value of 1 is 45, and 0 is 44, therefore 45-44 gives you 1. you use it if you read in numbers as characters or something like that.
• 11-13-2003
alphaoide
Quote:

Originally posted by major_small

>>a character could be a digit, an alphabet, etc.
try doing math with a char and say that again...

What I said relates to function isalpha(), isdigit(), isalnum(), etc, for example,
Code:

#include <ctype.h>
int isdigit( int ch )
The function isdigit() returns non-zero if its argument is a digit between 0 and 9. Otherwise, zero is returned.