atoi or scanf()

This is a discussion on atoi or scanf() within the C Programming forums, part of the General Programming Boards category; Why do I sometimes see atoi used instead of scanf(). struct store{ int number; }; int main() { struct store ...

  1. #1
    Unregistered
    Guest

    atoi or scanf()

    Why do I sometimes see atoi used instead of scanf().

    struct store{
    int number;
    };

    int main()
    {
    struct store shop;

    char age[10];

    printf("input a number:>");
    gets(age); /* ok. should be fgets */

    shop.number=atoi(age); /* why this */
    scanf("%d",&shop.number), /* instead of this */
    }

    /* also does atoi work like this */

    /* scans the array age element by element 0=49, 1=50, 2=51 */
    /* converts these ascii values into there digit equivalents 1,2,3 */
    /* and add each one to shop.number */


    thanks for all the guidence guys.

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    >>printf("input a number:>");
    >>gets(age); /* ok. should be fgets */

    >>shop.number=atoi(age); /* why this */
    >>scanf("%d",&shop.number), /* instead of this */

    gets() returns a string, not an integer which is what was entered for input, atoi() will change a string into an integer. scanf can read an integer directly. Which one is used really depends on style in this particular case, but you can't use gets without atoi to get an integer.
    pointer = NULL

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    Here the atoi() function is not used to scan in an integer, but to convert a string that was inputted using gets().

    I am pretty damn sure that atoi() can NEVER be used instead of scanf() or any other input/output function for that matter.

    printf("input a number:>");

    gets(age); /* gets a string from the user */
    shop.number=atoi(age); /* converts that string to an int *
    * and stores it in shop.number */


    scanf("%d",&shop.number), /* scans another int into *
    * into shop.number */


    I don't understand why someone would want to store the converted string into an int, that is overwritten with the next statement.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I don't understand why someone would want to store the
    > converted string into an int, that is overwritten with the next
    > statement.

    They weren't saying to actually have atoi() followed by the scanf.
    They were asking, which of the two lines is preferred.

    Quzah.
    /*god I hate this board. i keep forgetting to sign in. They need to
    have a better cookie.*/

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Re: atoi or scanf()

    /* THE FOLLOWING PROGRAMME IS TO SOLVE A PROBLEM THAT CAN'T BE SOLVED WITHOUT THE USE OF atoi. THIS PROGRAMME DOESN'T ALLOW THE USER TO ENTER ANYTHING OTHER THAN
    0 TO 9 .*/

    #include <stdio.h>
    #include <conio.h>

    int main(void)
    {
    char ch[6];
    int unsigned number; //unsigned because age can't be negative
    do{
    int static count;

    printf ("enter the age: ");
    ch[count]=getch();


    if(ch[count]==13){ //checking if enter is pressed
    ch[count]='\0';
    break;
    };

    if((ch<48) || (ch>57)) { //checking if the number is 0 to 9
    printf ("error!");
    ch[count]='\0';
    count-=1;
    }

    ch[count+1]='\0'; //a string is terminated by a null

    gotoxy(1,2);
    printf ("%s",ch); //printing the string

    count+=1; //increasing the value of count by 1;
    }while(1); //no condition is need since pressing enter will do that


    number=atoi(ch); //converting the string into integer

    gotoxy(1,4);
    printf ("Here is the perfect age: %d",number);

    getch(); //wait for a ending key press
    return 0;
    }



    Last edited by rahat; 10-22-2001 at 07:37 AM.
    RaHaTk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 08:28 AM
  3. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 07:36 AM
  4. Difference between using atoi and scanf
    By James00 in forum C Programming
    Replies: 4
    Last Post: 04-12-2003, 10:59 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 05:34 AM

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