# Thread: double vs int for statement

1. ## double vs int for statement

Here are two programs, in the first program i get d=0 as my answer in the statement, in the second program i get the correct answer d=7. Why does an incorrect answer appear in the statement when I am using the double command?

In my google search something came up about doubles not being allowed for use in statements, but I couldn't find any other information on the topic. Thanks in advance for your responses.

Code:
```#include <stdio.h>

int main (void)
{
int a = 3, b = 4;
double d;

d = a + b;

printf ("The value of d is %d\n", d);
return (0);
}```

Code:
```#include <stdio.h>

int main (void)
{
int a = 3, b = 4, d;

d = a + b;

printf ("The value of d is %d\n", d);
return (0);
}```

2. In the first example, d is a double, hence to print it, you should use %f, e.g.,
Code:
`printf ("The value of d is %f\n", d);`
Other than that, the code looks fine since the implicit conversion from int to double is fine.

3. In addition to Laserlight you can also type cast in the first program to get the correct answer.
ex.)
Code:
`printf ("The value of d is %d\n", (int)d);`

4. Originally Posted by Josh Genser
In my google search something came up about doubles not being allowed for use in statements
"doubles are not allowed for use in statements"?? I wonder what that could mean. The main problem with double is that it cannot precisely represent all integers, so a statement like

d == SOME_INTEGER

can evaluate to false, because the double d is "almost" equal to SOME_INTEGER but not close enough. Also, for integers, as long as the statements are defined, the following will always be true

i+1 > i

However, with a double, you will eventually get a large enough value such that

d+1 > d

evaluates to false, which means incrementing and decrementing doubles is not recommended.

5. Thanks for the input guys. One this I'm still confused is the %f and %lf commands. Are they supposed to be used for doubles like %d is supposed to be used for integers?

Does anyone know of a list somewhere where someone can find the corresponding % command for a given variable?

Camel-man, would using that (int) turn d into an integer?

Sorry if these questions seem basic but I started programming less than a week ago.

6. Originally Posted by Josh Genser
Does anyone know of a list somewhere where someone can find the corresponding % command for a given variable?
They are mentioned in the standards as well as in the "man pages". If you don't have man pages installed, my favorite website for them is unix.com:

Man Page for printf (posix Section 3) - The UNIX and Linux Forums

Man Page for scanf (all Section 3) - The UNIX and Linux Forums

7. Originally Posted by Josh Genser
Camel-man, would using that (int) turn d into an integer?
Only for the line that you cast.

Example
Code:
```#include <stdio.h>

int main(void)
{
double pi = 3.14;

printf("%f\n", pi);
printf("%d\n", (int)pi);
printf("%f\n", pi);

return 0;
}```
Output:
Code:
```3.140000
3
3.140000```
And of course you don't need to apologize.