AtoI() function (no hating plz im a newbee)

This is a discussion on AtoI() function (no hating plz im a newbee) within the C Programming forums, part of the General Programming Boards category; im having trouble understanding this function i mean what is the purpose of it. if i wanted an integer i ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    AtoI() function (no hating plz im a newbee)

    im having trouble understanding this function i mean what is the purpose of it. if i wanted an integer i would ask for one. it makes no sense to me that if u want an integer you would ask for a string and then convert it into an integer i mean whats the point eg
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    int age;
    char years[8];
    printf(“How old was Methuselah?”);
    gets(years);
    age=atoi(years);
    printf(“Methuselah was %d years old.\n”,age);
    return(0);
    }
    
    why could i not just do this :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    int age;
    
              printf(“How old was Methuselah?”);
              scanf("%d",age);
              printf(“Methuselah was %d years old.\n”,age);
             return(0);
    }
    

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    this function is really beneficial when you're reading a data file. I work with financial institution's database, sometimes they just send over a text file. So you need a way to convert these number characters to numerical types. If your purpose is only for printing out the number, then atoi is useless. However, when you need to operate on them with "+,-,*,/,%" then you need to convert them from Ascii code to a numerical type.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    The scanf function takes more code to work; so, in small system (common in embedded systems) the atoi might save code space.
    Note: In most of those cases, the printf would also not be used in order to save code space.

    Note: To save code space you need to not use the function at all. If you use it once, using it a second time costs very little in code space.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Just a note. This code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    int age;
    
              printf(“How old was Methuselah?”);
              scanf("%d",age);
              printf(“Methuselah was %d years old.\n”,age);
             return(0);
    }
    is wrong and will crash when run. scanf() requires the address of the variable it's reading into, so you need to do

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    int age;
    
              printf(“How old was Methuselah?”);
              scanf("%d", &age);
              printf(“Methuselah was %d years old.\n”,age);
             return(0);
    }

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by Shinzu911 View Post
    im having trouble understanding this function i mean what is the purpose of it. if i wanted an integer i would ask for one. it makes no sense to me that if u want an integer you would ask for a string and then convert it into an integer i mean whats the point eg

    why could i not just do this :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    int age;
    
              printf(“How old was Methuselah?”);
              scanf("%d",age);
              printf(“Methuselah was %d years old.\n”,age);
             return(0);
    }
    
    Try typing in "H" like many people will do by mistake. What happens?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    Quote Originally Posted by WaltP View Post
    Try typing in "H" like many people will do by mistake. What happens?
    That's exactly the problem. Also, scanf() is generally abhorred due to various problems. See Question 12.19 of the C FAQ for an example of what WaltP was referrring to, and Question 12.20 for some more information.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Location
    Michigan
    Posts
    6
    here is how I would do it, but you probably are not programming on windows.
    Code:
    #define UNICODE
    #define _UNICODE
    #pragma comment(lib,"Shlwapi.lib") //for StrToInt
    #include <tchar.h>
    #include <windows.h>
    #include <iostream>
    #include <Shlwapi.h> //for StrToInt
    using namespace std;
    
    
    int _tmain(int argc, TCHAR **argv)
    {
        INT i;
        LPTSTR lpStr = TEXT("1000");
       
        i = StrToInt(lpStr);
        
        wcout << i << endl;
        
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    If you look at the homepage, you will see a "C++ Programming" board, and a "C Programming" board. By posting in the latter, you are expected to not use C++.

    Your code is incorrect, by the way, because the point of his problem was to read an integer from the user, not specify one in the code.

    Also, something to note: don't dump codes for people who ask questions. You're wasting your time, and teaching them nothing.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Location
    Michigan
    Posts
    6
    Sorry bout that, I just wanted to show a safer alternative to atoi. thanks for the advice.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    if one is using a debugger and breakpoints, combining the gets (or the preferred fgets) with sscanf would let you see what actually was entered where scanf will just eat the bytes and you would never see what was typed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R Page 61 atoi function example
    By optimizer_777 in forum C Programming
    Replies: 5
    Last Post: 12-30-2011, 11:05 PM
  2. The atoi function
    By jamesallen4u in forum C Programming
    Replies: 6
    Last Post: 11-30-2011, 06:19 PM
  3. complete function definition i.e. atoi
    By c_lady in forum Linux Programming
    Replies: 11
    Last Post: 03-31-2010, 12:28 PM
  4. What is the negative effect for atoi function ?
    By How2BecomePro in forum C Programming
    Replies: 5
    Last Post: 06-25-2009, 09:43 AM
  5. Newbee
    By Bio Hazord in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2003, 07:08 PM

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