Thread: Have I understood the atoi funtion right?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Europe / Latvia
    Posts
    5

    Have I understood the atoi funtion right?

    Hey all,
    I'm a newbie and I'm learning C language (reading book for Dummies), I have no one else to ask, because I'm too shy, and sorry if this is too dumb or a third grade question, but I need to know if I have understood the atoi function right or am I missing something, because it just seems weird. I have been configuring the code for the fifth time and I'm just curious.

    As I understand, then atoi is for reading value strings from a keyboard (is that right)? Because we can't read values, using int integral through something simply as this:
    Code:
    int deer_age;
    printf("How old is the deer?");
    gets(deer_age);
    printf("Deer is %d years old",deer);
    , because it just won't work, instead we have to give an integer his years and then simply print it out. And of course there's not way giving an char variable his age either, cuz it won't work that way, so we use the atoi function to print out his years, which goes, of course, like this:
    Code:
    int deer_age;
    char deer[20];
    
    printf("How old is the deer?");
    gets(deer);
    deer_age=atoi(deer);
    printf("Deer is %d years old",deer_age);
    Have I got it right? Should I stop worry.
    Thanks for the replies. I know this seems a bit awkward.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    atoi doesn't read from the keyboard. You pass it a string that is a number "12345" and it will return it as an int.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The simple answer is that when you read from the keyboard, you get a string. You can print a string, sure, but you cannot do any mathematical operations on it.
    Also do not use gets: http://sourceforge.net/apps/mediawik...php?title=Gets
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Location
    Europe / Latvia
    Posts
    5
    Quote Originally Posted by Elysia View Post
    The simple answer is that when you read from the keyboard, you get a string. You can print a string, sure, but you cannot do any mathematical operations on it.
    Also do not use gets: SourceForge.net: Gets - cpwiki
    Thank you, you really helped me and I'll surely read the article !

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    Greetings.

    I seek assistance with the atoi() function. Below is what I am attempting:

    Code:
    FILE * f;
    f = fopen(argv[1], "r");
    int c;
    char * cp;
    ...
    ...
    while ((c = fgetc(f)) != EOF){
       cp = &c;                           //Line causing error
       printf("Printing...%d\n", atoi(cp));
    }
    The above code generates the following warning:

    Code:
    warning: assignment from incompatible pointer type
    Would greatly appreciate assistance with resolving the above warning.

    Best regards,
    wirefree101

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, you have an integer and try to make a char pointer point to it. That is wrong, wrong, wrong. You are pointing to an INTEGER, not a char. Therefore you get a warning.
    Secondly, atoi does NOT work with single characters. It works with strings. And a string must contain one or more characters and then a null character ('\0'). Otherwise you will cause undefined behavior.
    Lastly, I would also suggest strtol instead of atoi, simply because it is safer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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