By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

Trouble with formats in printf

P: n/a
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

Mar 21 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 21 '07 #2

P: n/a
Sanjay Kulkarni wrote:
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>.
Mar 21 '07 #3

P: n/a
Sanjay Kulkarni <sa***********@gmail.comwrote:
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.hisn'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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Mar 21 '07 #4

P: n/a
Sanjay Kulkarni <sa***********@gmail.comwrote:
>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

Mar 21 '07 #5

P: n/a
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.
Mar 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.