Atoi

This is a discussion on Atoi within the C Programming forums, part of the General Programming Boards category; Hi, I've got a string which i need to convert into a integer, potentially the integer is a maximum of ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Atoi

    Hi,

    I've got a string which i need to convert into a integer, potentially the integer is a maximum of 8 bytes long.

    So first of all will atoi be able to convert it, secondly what definition will the int variable require, i believe unsigned long long
    im using visual studio 2003 if it makes any difference and c language.

    Thanks

    James

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    You can use either atoi or atol (which was built for that purpose). Neither guarantee what the size of a long int will actually be (because C doesn't itself). If you need more than long int, you'll be doing it yourself (atoi isn't exactly a complex function here).

    The variable would need to be whatever the return type of the function you're intending to use it. For atoi, that's int. For atol, that's long int. For anything else - you're crafting it yourself, so you decide.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    You should use strtol / strtoul rather than atoi, as these functions will tell you when there is numeric overflow (or other errors).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++.NET Can I use atoi ??
    By Swaine777 in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2004, 06:54 PM
  2. atoi?
    By mayfda in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2002, 08:45 AM
  3. atoi
    By Max in forum C Programming
    Replies: 1
    Last Post: 11-05-2002, 09:28 AM
  4. atoi()
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 07-13-2002, 02:09 AM
  5. atoi
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 02:39 AM

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