# Thread: confusion with integers (newbie question)

1. ## confusion with integers (newbie question)

hey, im 15 years old and just starting out learning C. my question is this:
when i use "atoi" to change a character variable to an integer its works fine. however when i use it to change a character variable into a float variable it still comes out as an integer and i cannot place a decimal. can someone help me figure out how to make this work. thanks.

here is a small code of mine to show you what i mean.
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
float num1;
float num2;
float ans;
char cnum1[20];
char cnum2[20];

printf("input number:");
gets(cnum1);
printf("inut number:");
gets(cnum2);
num1=atoi(cnum1);
num2=atoi(cnum2);
ans=num1+num2;
printf("%f+%f=%f", num1, num2, ans);
system("PAUSE");
return 0;
}```

&#91;code]&#91;/code]tagged by Salem

2. Use atof if you want floats.

3. An integer is not the same as a float in C, use atof.

4. atoi = array to integer

Of course the result is an integer . As was mentioned, use atof instead.

5. thanks ALOT. that was driving me crazy. now i can continue on my C learning adventure!

6. could be wrong here, but I'm not. atof() is used to go from ASCII to a double, not a float. Like I said, I could be wrong, but I'm almost certain that's the way atof works. Check out your reference books guys.

7. atof() converts a string to a double. Isn't double a float just with more precision?

8. Originally posted by damonbrinkley
atof() converts a string to a double. Isn't double a float just with more precision?
The point is, a double is larger than a float. Your compiler should warn you about possible loss of data. Example:

char c;
int x;

x = 1000000;
c = x; /* Data loss. */

The same holds true for double->float conversion.

Quzah.