Problems interpreting simple syntax construct and explanation from K&R

This is a discussion on Problems interpreting simple syntax construct and explanation from K&R within the C Programming forums, part of the General Programming Boards category; This is excerpted from K&R pages. 42-43 The explanation reads that this function coverts a string of digits into its ...

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

  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
    22,127
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  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, 08:00 PM
  3. Simple Explanation Needed
    By slowcoder in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 03:00 PM
  4. Need Help with syntax and overall problems!
    By Simon in forum C Programming
    Replies: 1
    Last Post: 09-12-2002, 12:15 AM
  5. strcpy syntax explanation needed
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 08:29 PM

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