Thread: Problems interpreting simple syntax construct and explanation from K&R

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    Problems interpreting simple syntax construct and explanation from K&R

    This is excerpted from K&R pages. 42-43

    The explanation reads that this function coverts a string of digits into its numeric equivalent.

    Do they explicitly mean numbers by saying digits? If so, what does it's numeric equivalent mean if digits are already numbers?

    Code:
    int atoi(char s[])
    {
    	int i, n;
    	
    	n = 0;
    	for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
    		n = 10 * n + (s[i] - '0');
    
    	return n;
    	
    }
    I'm having problems understanding what is happening here:

    Code:
    (s[i] - '0');
    It says that this gives the numeric value of the character stored in s[i], because the values of '0', '1', etc, form a contiguous increasing sequence.

    I really don't understand that construct or what this is saying.

    Help would be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you have a character, say '5'

    Then '5' - '0' will give you the numeric value 5
    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.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    What is the point of including - '0', if I'm just looking for the value of whatever is stored in s[i]?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by billybob2020
    What is the point of including - '0', if I'm just looking for the value of whatever is stored in s[i]?
    Ah, but you are not "looking for the value of whatever is stored in s[i]". Suppose '2' is stored in s[i]. Then the value of s[i], assuming ASCII, is 50. However, what you want is 2, not 50, so you subtract '0', which in ASCII has a value of 48. Even if you are not dealing with ASCII, it is guaranteed that the digits will be in order in the character set, as a contiguous sequence, so this method will always work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What is the point of including - '0'
    Hey, you can try it without the -'0' if you want, and see if the code still functions as atoi() as most people understand it.

    Go on, try it, see what happens.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    Woah. I was really confused. I didn't realize that '0' was an ASCII character that had an internal value, so it read as just zero to me. Laser cleared that up for me.

    Thanks to all of you guys though, and the speedy replies.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by billybob2020 View Post
    Woah. I was really confused. I didn't realize that '0' was an ASCII character that had an internal value, so it read as just zero to me. Laser cleared that up for me.

    Thanks to all of you guys though, and the speedy replies.
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. syntax problems =/
    By アストラル in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2008, 12:47 PM
  2. simple syntax error
    By MacNilly in forum C Programming
    Replies: 3
    Last Post: 08-11-2007, 07:00 PM
  3. Simple Explanation Needed
    By slowcoder in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 02:00 PM
  4. Need Help with syntax and overall problems!
    By Simon in forum C Programming
    Replies: 1
    Last Post: 09-11-2002, 11:15 PM
  5. strcpy syntax explanation needed
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 08:29 PM