Thread: Question in getint #2

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    8

    Question in getint #2

    Can someone please help me understand the code in bold below?
    I can't understand what it's doing...

    Code:
     
    #include <ctype.h>
    #include <stdio.h>
    
    int getch(void);
    void ungetch(int);
    
    /* getint: get next integer from input into *pn */
    int getint(int *pn)
    {
        int c, sign;
            
        while (isspace(c = getch()));/* skip white space */
                
        if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
            ungetch(c);  /* it's not a number */
            return 0;
        }
        sign = (c == '-') ? -1 : 1;
        if (c == '+' || c == '-')
            c = getch();
         for (*pn = 0; isdigit(c); c = getch())
            *pn = 10 * *pn + (c - '0');   // This is what I don't get...
        *pn *= sign;
     
    
        if (c != EOF)
            ungetch(c);
        return c;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Imagine the user enters:

    12345

    That's 5 ascii characters. If you subtract the ascii value of '0' from an ascii digit yeilds it's numerical value, ie: '8' - '0' == 8.

    So with the equation:

    value = (value * 10) + digit

    ...the number builds like this:

    1 [ (0 * 10)+1 == 1 ]
    12 [ (1 * 10)+2 == 12 ]
    123 [ (12 * 10)+3 == 123 ]
    1234 [ (123 * 10)+4 == 1234 ]
    12345 [ (1234 * 10)+5 == 12345 ]
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    8
    Originally posted by Sebastiani
    Imagine the user enters:

    12345

    That's 5 ascii characters. If you subtract the ascii value of '0' from an ascii digit yeilds it's numerical value, ie: '8' - '0' == 8.

    So with the equation:

    value = (value * 10) + digit

    ...the number builds like this:

    1 [ (0 * 10)+1 == 1 ]
    12 [ (1 * 10)+2 == 12 ]
    123 [ (12 * 10)+3 == 123 ]
    1234 [ (123 * 10)+4 == 1234 ]
    12345 [ (1234 * 10)+5 == 12345 ]
    Now it makes sense.. I had no idea about '8' - '0' == 8
    Thank you very very much.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    ASCII '8' - '0' == 0x38 (decimal 56)

    Unless of course we're talking about EBCDIC which
    is apparently the case.

    http://www.natural-innovations.com/c...ciiebcdic.html
    Last edited by c99; 03-10-2004 at 12:47 AM.
    R.I.P C89

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    ASCII '8' - '0' == 0x38 (decimal 56)

    Unless of course we're talking about EBCDIC which
    is apparently the case.
    Eh? '8' - '0' == 8 not 0x38. Even on the EBCDIC system.

    Only time this would break down is when the characters '0' to '9' aren't continuous or sequential

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Yes, of course.

    In my defense, parse error, hallucinating, and just woke up.

    What I did learn though, was that EBCDIC is the reason that
    the C programming language has trigraphs.
    Last edited by c99; 03-10-2004 at 01:12 AM.
    R.I.P C89

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. question in getint
    By jongmin in forum C Programming
    Replies: 2
    Last Post: 03-09-2004, 01:27 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM