Thread: Char to Int ( How atoi() works? )

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    74

    Char to Int ( How atoi() works? )

    Hello guys,
    I am following K&R to advance my C skills. In the book I found a piece of code which showed how atoi() works. It said like s[i] - '0' will give a numeric value of the character. Can someone please explain it to me. I tried to search around but they only explain it with integers and not with characters. Say you are given s[i] = 'Z'.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sankait Laroiya View Post
    Hello guys,
    It said like s[i] - '0' will give a numeric value of the character. Can someone please explain it to me. I tried to search around but they only explain it with integers and not with characters. Say you are given s[i] = 'Z'.
    Characters (as in the char types in C) are actually represented as a set of integral values.

    The character '0' is a printable character that, when displayed on or written to an output device (a screen, etc) produces a result that looks like a zero. However, the character '0' does NOT have a numeric value of zero (for example, in the ASCII character set, '0' has the numeric value of 48) in any character set - and doesn't necessarily have the same value in different character sets. Similarly for '1' through '9'.

    With all standard character sets (ASCII, EBCDIC, etc) that might be used by people who represent characters using arabic digits (0-9) the characters '0'-'9' are sequential (i.e. '0' is immediately before '1' is immediately before '2' ....) and the character '0' (the one rendered so it looks like a zero) does not have a numeric value of zero. So '1' is equal to '0' + 1, '2' is equal to '0' + 2, etc. Or, conversely, subtracting '0' is the way to map the printable digits ('0' through '9') to the numeric values (0 through 9).

    This mapping is not useful for characters that represent things other than digits (letters, punctuation, whitespace, control codes, etc). The result of subtracting '0' from 'Z' will give an integral value, but that value is not guaranteed to correspond to any particular character (the meaning does differ between character sets).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    when I input Z as an argument, or any other alphabet, atoi gives value 0. So, what is the use of it.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you were writing a function to convert a string to an integer, what result would you produce from the string "ZZZZ"?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Sankait Laroiya View Post
    when I input Z as an argument, or any other alphabet, atoi gives value 0. So, what is the use of it.
    What exactly were you expecting atoi to do with 'Z' as input? It is not a number, and therefore cannot be converted to a number. If you use strtol with a base of 36, 'Z' will likely give you a meaningful value. Under basically any other conditions, 'Z' is meaningless as a numeric digit.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    Well, if it cannot convert character, and only returns integers passed to it then what is the use of it?

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    Quote Originally Posted by grumpy View Post
    If you were writing a function to convert a string to an integer, what result would you produce from the string "ZZZZ"?


    It is still a 0.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Sankait Laroiya View Post
    Well, if it cannot convert character, and only returns integers passed to it then what is the use of it?
    Its use is for converting strings known to be numeric to integer representation. What is it that you were hoping for?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sankait Laroiya View Post
    It is still a 0.
    I know what atoi() does. You haven't answered my question though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    Okay, guys, I thought atoi() would convert string (including characters) to integers. Now I came to know it just converts the type of the char to int if the char is a string of digits.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You just worked that out, eh? The documentation for atoi() does say that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atoi and const char not being very friendly
    By flaris in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2011, 05:18 PM
  2. atoi() for char
    By xaykogeki in forum C Programming
    Replies: 6
    Last Post: 04-28-2010, 06:35 PM
  3. Huge issue with atoi/2d char array
    By MaestroRage in forum C Programming
    Replies: 4
    Last Post: 08-21-2009, 11:26 PM
  4. atoi(char) problem - C2664
    By patricio2626 in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2006, 08:34 PM
  5. Not atoi, not sprintf, but "int coded into char*"
    By Mariano L Gappa in forum C Programming
    Replies: 5
    Last Post: 11-26-2005, 01:53 AM