ch-'0'

This is a discussion on ch-'0' within the C++ Programming forums, part of the General Programming Boards category; I came across the following code in a parsing example in a book on C++ Code: if(ch>='0' && ch<= '0') ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    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?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>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

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: ch-'0'

    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...
    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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Re: Re: ch-'0'

    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.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    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.
    Last edited by DougDbug; 11-13-2003 at 03:45 PM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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

  8. #8
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    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.

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Help with using the return statement
    By mkdl750 in forum C Programming
    Replies: 4
    Last Post: 07-23-2008, 10:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Replies: 29
    Last Post: 11-01-2002, 03:46 PM
  5. Tic tac toe (how do i check for winnner?)
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 03:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21