Trouble with formats in printf

S

Sanjay Kulkarni

I am facing some problems while trying to explore the printf
statement:

Expected Actual
Result Statement Result

1 printf("x = %d", 18.0 / 14); 18725
5.0 printf("\nx = %f", 5); 0.000000
5 printf("\nx = %d", 5.0); 0

What am I missing? Is there some issue with floats?
I have included conio.h, stdio.h, float.h and math.h.

- Sanjay Kulkarni
 
R

Richard Heathfield

Sanjay Kulkarni said:
I am facing some problems while trying to explore the printf
statement:

Expected Actual
Result Statement Result

1 printf("x = %d", 18.0 / 14); 18725
5.0 printf("\nx = %f", 5); 0.000000
5 printf("\nx = %d", 5.0); 0

What am I missing?

18.0 / 14 is a double, not an int, but you promised printf an int. Since
you lied, printf is allowed to sulk.

5 is an int, not a double, but you promised printf a double. Since you
lied, printf is allowed to sulk.

5.0 is a double, not an int, but you promised printf an int. Since you
lied, printf is allowed to sulk.
 
M

Martin Ambuhl

Sanjay said:
I am facing some problems while trying to explore the printf
statement:

Expected Actual
Result Statement Result

1 printf("x = %d", 18.0 / 14); 18725

18.0 / 14 is a floating point value. It makes no sense to print it
with "%d".
18 / 14 is an integral number. Try that.
5.0 printf("\nx = %f", 5); 0.000000

5 is an integer. It makes no sense to print it with "%f".
5 printf("\nx = %d", 5.0); 0

5.0 is a floating point value. It makes no sense to print it with "%d".
What am I missing? Is there some issue with floats?

In all these cases you are producing undefined behavior. The issue is
with knowing the difference between a floating point number and an
integer. Further, there are no floats at all in the above, so it can
hardly be "some issue with floats".
I have included conio.h, stdio.h, float.h and math.h.

It's a good thing you didn't post that code with the non-standard <conio.h>.
 
J

Jens Thoms Toerring

Sanjay Kulkarni said:
I am facing some problems while trying to explore the printf
statement:
Expected Actual
Result Statement Result
1 printf("x = %d", 18.0 / 14); 18725

Here you tell printf() via the '%d' format specifier that the second
variable you're going to pass it wil be an integer. But you're lying
and pass it a floating point variable instead.
5.0 printf("\nx = %f", 5); 0.000000

Here you tell it to expect a float (or double) but you pass it an int.
5 printf("\nx = %d", 5.0); 0

And here, like in the first case you promise it an int but pass it
a double.
What am I missing? Is there some issue with floats?

There's no issue with floats, there's an issue with your use of
printf(). You _must_ pass printf() a variable of the type you
promised it you would send via the format specifiers or all bets
are off. You have to be very careful about this with all functions
that take variable number of arguments since the compiler can't
correct any mistakes you make.
I have included conio.h, stdio.h, float.h and math.h.

<conio.h> isn't a standard C header file, avoid if if you can.
And of all the rest you only need <stdio.h> - at least for the
three lines you have shown here.
Regards, Jens
 
B

Beej Jorgensen

Sanjay Kulkarni said:
What am I missing?

That printf() won't automatically convert your parameters to the correct
type. You have to make sure they are the correct type.

-Beej
 
A

Army1987

1 printf("x = %d", 18.0 / 14); 18725
printf("x = %d", (int)18.0/14);
(Why not simply use 18/14, of course?)
5.0 printf("\nx = %f", 5); 0.000000
printf("\nx = %d", (double)5);
(Why not simply use 5.0, of course?)
5 printf("\nx = %d", 5.0); 0
printf("\nx = %d", (int)5.0);
(Why not simply use 5, of course?)

Sometimes, values aren't automatically casted to the right type.
Use (appropriate_type)expression to do that.

BTW, why do you put newlines at the beginning of strings rather than at the
end of the previous ones?
This way you'll need an extra \n at the end of the program to prevent the
prompt from showing on the same line of the last output.
Using strings like "x = %d\n" everywhere needed will be more consistent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top