Thread: atoi()

  1. #1
    Unregistered
    Guest

    Question atoi()

    What does atoi() really do?

    does it convert alpha to integers or does it test for integers?

  2. #2
    Unregistered
    Guest
    int atoi(const char *s)

    converts s to int

  3. #3
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    Exclamation

    so I can't use use atoi(n) by itself on a new line

    n is an array filled with characters

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Code:
    char *s_num = "123";
    int num;
    
    num = atoi(s_num);
    /*   num will now have 123   */

  5. #5
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    what if char *s_num = "hello";

    instead of char *s_num = "123";


    will num have "hello" in it

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Look closely, num has been declared as int, so how can it ever return "hello"??? See help on atoi(). My man page says:

    int atoi(const char *nptr);

    The atoi() function converts the character string pointed to by the nptr
    parameter, up to the first character inconsistent with the format of a
    decimal integer, to an integer data type.
    Which means, if:
    Code:
    char *s_num = "123hello";
    num = atoi(s_num);
    num will have 123.

  7. #7
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    Smile

    Thanks your explanation helps a lot, beter than the book I have!!!!!!!!

  8. #8
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    My pleasure

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    atoi() will return a 0 if the string can not be converted.

  10. #10
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    That's right if i type a word i'll get 0 as the result

    Is there a way out of this anormal function of atoi( )?

  11. #11
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    What do you mean?

  12. #12
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    what acctually I want to know is that will atoi convert alpha to its ascii number or it has to print 0 if it encounters characters.

  13. #13
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    No it won't. Also realise that the ASCII number of an alpha is not a single digit.

    C Standard, 7.20.1 Numeric conversion functions, p306
    If the value of the result cannot be represented, the behavior is undefined.
    So how a compiler handles atoi() called on a string containing alpha characters is implementation dependent.

  14. #14
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Got it!!!!!!

    Thanks

  15. #15
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    A simple alternative to atoi()

    Code:
    long ConvertString(char* buffer)
    {
    	char *ptr;
    	long result=0;
    	int i=0;
    	ptr = buffer;
    	while(*ptr!='\0')
    	{
    		result = (result * 10) + (*ptr - '0');
    		ptr++;i++;
    	}
    	return result;
    }
    This code will work if the data passed to it is in the correct format.

    BUT

    Realise that this code is not crash proof, it's up to you to add
    checks and tests to ensure proper functionality.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  2. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  3. Problem with atoi in C
    By garagebrian in forum C Programming
    Replies: 5
    Last Post: 08-24-2004, 01:59 PM
  4. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM
  5. how to use atoi!!!
    By moon in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2002, 09:54 PM