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!