I came across the following code in a parsing example in a book on C++
Question is: What does ch-'0' accomplish? Is it removing the null from a string?Code:if(ch>='0' && ch<= '0') //if its a digit, s.push(ch-'0'); //save numerical value
I came across the following code in a parsing example in a book on C++
Question is: What does ch-'0' accomplish? Is it removing the null from a string?Code:if(ch>='0' && ch<= '0') //if its a digit, s.push(ch-'0'); //save numerical value
>>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
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
that doesn't determine if it's a digit... chars are not digits... they're chars...Originally posted by kes103
I came across the following code in a parsing example in a book on C++
Question is: What does ch-'0' accomplish? Is it removing the null from a string?Code:if(ch>='0' && ch<= '0') //if its a digit,
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
a character could be a digit, an alphabet, etc.Originally posted by major_small
that doesn't determine if it's a digit... chars are not digits... they're chars...
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.
Last edited by DougDbug; 11-13-2003 at 04:45 PM.
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...
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
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.
What I said relates to function isalpha(), isdigit(), isalnum(), etc, for example,Originally posted by major_small
>>a character could be a digit, an alphabet, etc.
try doing math with a char and say that again...
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.