Thread: Question about atoi()

  1. #1
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26

    Question about atoi()

    I've been using atoi() to read a field in a text file and store the binary number in an unsigned int field of a struct. From what I can tell, the results are not what I want if the number is within the range of an unsigned int but would be negative as a signed int. Perhaps I need to convert the text number into a long long and then cast it into the struct. Is there a function like atoi() whose output is a long long integer?
    Last edited by Alogon; 09-03-2020 at 09:16 PM.

  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
    You should use the strtol(), strtoul() or strtod() functions for your string-to-number conversions.
    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
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Alogon View Post
    I've been using atoi() to read a field in a text file and store the binary number in an unsigned int field of a struct. From what I can tell, the results are not what I want if the number is within the range of an unsigned int but would be negative as a signed int. Perhaps I need to convert the text number into a long long and then cast it into the struct. Is there a function like atoi() whose output is a long long integer?
    Same tip as Salem's here... Use strtol() or strtoul() functions. Observe that C99 also offers strtoll() and strtoull() for 'long long int's...

  4. #4
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hallo Alogon!

    If you use gcc see this page:

    atoi(3): convert string to integer - Linux man page

    see for this:
    int atoi(const char *nptr);
    long atol(const char *
    nptr);
    long long atoll(const char *
    nptr);
    long long atoq(const char *
    nptr);

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by rusyoldguy View Post
    Hallo Alogon!

    If you use gcc see this page:

    atoi(3): convert string to integer - Linux man page

    see for this:
    int atoi(const char *nptr);
    long atol(const char *
    nptr);
    long long atoll(const char *
    nptr);
    long long atoq(const char *
    nptr);
    From man7.org, a more accurate resource for online manpages:
    errno is not set on error so there is no way to distinguish between 0 as an error and as the converted value. No checks for overflow or underflow are done. Only base-10 input can be converted. It is recommended to instead use the strtol() and strtoul() family of functions in new programs.
    Please compare the atoi() manpage against the strol() magpage, and the example on the latter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP WITH atoi()
    By dhoow in forum C Programming
    Replies: 5
    Last Post: 12-10-2015, 12:09 PM
  2. atoi question?
    By fayte in forum C Programming
    Replies: 3
    Last Post: 02-27-2006, 02:15 PM
  3. atoi related question
    By The Gweech in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2003, 11:14 PM
  4. atoi question
    By smd in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2002, 12:21 PM
  5. like atoi
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 09:42 AM

Tags for this Thread