Difference between using atoi and scanf

This is a discussion on Difference between using atoi and scanf within the C Programming forums, part of the General Programming Boards category; I have a question, whats the difference between (1) using atoi to convert a 'char number' (number used in a ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    Question Difference between using atoi and scanf

    I have a question, whats the difference between (1) using atoi to convert a 'char number' (number used in a char variable) into a int variable , (2) and just using scanf to read the int directly instead of having to get it first as a char variable and then having to convert it to int variable? Example:

    First part of program (both examples start with this):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int getval(void);
    
    void main()
    {
    	int age,weight,area;
    	float iq;
    
    	printf("Program to calculate your IQ.
    ");
    	printf("Enter your age:");
    	age=getval();
    	printf("Enter your weight:");
    	weight=getval();
    	printf("Enter your area code:");
    	area=getval();
    
    	iq=(float)(age*weight)/area;
    	printf("This computer estimates your IQ to be %f.
    ",iq);
    }


    Now here are the 2 examples (the follow the first part of the program):

    (1)
    Code:
     
    int getval(void)
    {
    	char input[20];
    	int x;
    
    	gets(input);
    	x=atoi(input);
    	return(x);
    }


    (2)
    Code:
     
    int getval(void)
    {
    	int x;
    
    	scanf("%i",&x);
    	return(x);
    }


    Hope you underestand what I am trying to say. Thanks!

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    There is no difference. They will both work if the user enters correct information. You may not want to use gets()'s though, use fgets()'s

    ex: fgets(input, sizeof(input), stdin);
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    There is an advantage of using the atoi family because one can scan the string for digits and spit out other types. Users don't always follow instructions.

    The thing to watch out for is using the appropriate conversion function for the size of the anticipated number.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    James00: Read and read.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Thanks a lot!!! Hammer, your guide is awesome, I now know exactly what each function is. A million thanks!!!

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, 07:35 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  3. replacing scanf with fgets
    By subflood in forum C Programming
    Replies: 6
    Last Post: 08-19-2004, 01:59 PM
  4. atoi or scanf()
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-19-2001, 05:18 AM

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