A Newbie Question

C

crackedh

Hi,
I'm a newbie to C, and to programming in general.

My question is:
-----------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
main()
{
int fahr;
for (fahr = 300; fahr >=0; fahr = fahr - 20)
{
printf("%3.0d %3.2f\n", fahr, (5.0/9.0) * (fahr -
32));
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
main()
{
int fahr;
for (fahr = 300; fahr >=0; fahr = fahr - 20)
{
printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr -
32));
}
}
 
R

Richard Heathfield

crackedh said:
Hi,
I'm a newbie to C, and to programming in general.

My question is:
-----------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
main()
{
int fahr;
for (fahr = 300; fahr >=0; fahr = fahr - 20)
{
printf("%3.0d %3.2f\n", fahr, (5.0/9.0) * (fahr -
32));
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
main()
{
int fahr;
for (fahr = 300; fahr >=0; fahr = fahr - 20)
{
printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr -
32));
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------

Why is it that both these segments of code give very diffrent outputs.

The first program looks correct to me (although it is poor style not to
return a value from main - if in doubt, return 0).

The second program asks printf to display a double precision floating-point
number, but provides fahr (an int, not a double) as the value to display.
This breaks the rules of C, and any output you get is therefore arbitrary
- indeed, the behaviour of the whole program becomes undefined.

To display an int using printf, match it with %d. To display a double,
match it with %f.

The second format specifier in that line, %3.2f, is fine, because it's
matched by a double. What's happening here is that fahr is an int and 32
is an int, so fahr - 32 is an int, but then you multiply by a double (that
is, 5.0/9.0), so the (fahr - 32) value is promoted to double for the
purposes of the calculation, and the expression as a whole yields a
double.
 
J

Joachim Schmitz

crackedh said:
Hi,
I'm a newbie to C, and to programming in general.

My question is:
int main(void)
{
int fahr;
for (fahr = 300; fahr >=0; fahr = fahr - 20)
{
printf("%3.0d %3.2f\n", fahr, (5.0/9.0) * (fahr -
32))
} return 0
}
int main(void)
{
int fahr;
for (fahr = 300; fahr >=0; fahr = fahr - 20)
{
printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr -
32));
fahr is int, %f expects double.
return 0
Because you tell printf to expect a double (%f) but give an int.

Bye, Jojo
 
M

Mark Bluemel

crackedh said:
#include <stdio.h>
main()
{
int fahr;
for (fahr = 300; fahr >=0; fahr = fahr - 20)
{
printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr - 32));

You lied to printf - the "%3.0f" indicated that you would pass it a
double value, and "fahr" is an int.
 
M

Martin Ambuhl

crackedh wrote:
[...]
int fahr; [...]
printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr -
32));

fahr is an int. The specifier "%3.0f" tells printf that the
corresponding argument (fahr) is a double (or a float whose value is
passed as a double). You lied to printf. Why should you expect the
output to make sense?
 
C

CBFalconer

crackedh said:
Thanks for the exdplanation.
:)

Don't answer anything without an explanatory quote. See below.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top