Thread: The atoi function

  1. #1
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43

    The atoi function

    Hello,

    I am trying to find a tutorial or a reference for using the atoi function in C. I found one on this site (atoi - C++ Function Reference) but I am not familiar with C++. I am trying to create a grid and an embedded multiplication table inside that grid so I thought I could use atoi as a more simpler alternative to printf. Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well since it's a C function, the interface is exactly the same.
    So you really should be able to get the jist of what is going on, even if you have only a glimmer of understanding of what cout might do.

    Plus, you should use strtol() or strtoul() in place of atoi().
    They have much better error detection.
    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
    Dec 2007
    Posts
    214
    The C version of atoi is used just like the C++ version. I'm not sure what you mean by using it as an alternative to printf.

  4. #4
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Quote Originally Posted by DaveH View Post
    The C version of atoi is used just like the C++ version. I'm not sure what you mean by using it as an alternative to printf.
    What I mean is that you can't create a char array with numbers so atoi would be an alternate solution to just manually printing the numbers.
    Linux Distro: Ubuntu 12.04
    Browser: Chromium

  5. #5
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Quote Originally Posted by Salem View Post
    Well since it's a C function, the interface is exactly the same.
    So you really should be able to get the jist of what is going on, even if you have only a glimmer of understanding of what cout might do.

    Plus, you should use strtol() or strtoul() in place of atoi().
    They have much better error detection.
    So if I understand it right, if you entered something like "123 hello" it would disregard the whitespace and letters and return 123?
    Linux Distro: Ubuntu 12.04
    Browser: Chromium

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jamesallen4u View Post
    What I mean is that you can't create a char array with numbers so atoi would be an alternate solution to just manually printing the numbers.
    Who says you can't create a char array with numbers?

    There's nothing magical about the char type, it's just a short short int ...

    Code:
    int main (void)
      {
         char x[3];
    
         x[0] = 11;
         x[1] = x[0] * 5;
         x[2] = 42;
    
         printf("%hhd %hhd %hhd\n", x[0], x[1], x[2]);
    
         return 0;
    }
    ... go ahead, try it for yourself.

  7. #7
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Quote Originally Posted by CommonTater View Post
    Who says you can't create a char array with numbers?

    There's nothing magical about the char type, it's just a short short int ...

    Code:
    int main (void)
      {
         char x[3];
    
         x[0] = 11;
         x[1] = x[0] * 5;
         x[2] = 42;
    
         printf("%hhd %hhd %hhd\n", x[0], x[1], x[2]);
    
         return 0;
    }
    ... go ahead, try it for yourself.
    Outputs 11, 55, and 42. Thanks CommonTater, I guess I was misinformed about char arrays.
    Linux Distro: Ubuntu 12.04
    Browser: Chromium

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. complete function definition i.e. atoi
    By c_lady in forum Linux Programming
    Replies: 11
    Last Post: 03-31-2010, 12:28 PM
  2. What is the negative effect for atoi function ?
    By How2BecomePro in forum C Programming
    Replies: 5
    Last Post: 06-25-2009, 09:43 AM
  3. like atoi
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 09:42 AM
  4. atoi
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 02:39 AM
  5. how to use atoi!!!
    By moon in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2002, 09:54 PM

Tags for this Thread