0

I ran these codes but I got a really weird number. I'm new to c++

ii) Predict the outputs for the following snippets of code:

a)

int x=75, y=105;
printf(" %c %lf " , x,y);

for the first one i got x is equal to K. This I understand but for y I got 0, is it because it was declared as int and not double?

b)

float pH=5.65582; 
printf (" %d %lf" , pH,pH);

Now this one is so weird. For the first pH it's 1073741824. I don't even know how they got it. And for the second pH is 2. How? c)

float p=0.345689;
double q=0.445566778899;
printf ( "%d %f %lf" , p*q, p*q, p/q);
printf ("%d %0.5f %9.3f", p+q , p-q, p*(p+q));

p*q -1713662420

p*q -0.000000

p/q -0.000000

p+q -1561213759

p-q 0

p*(p+q) a really long number

591890496450433740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000

What am I doing wrong?

  • [This](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) might be somewhat relevant.... – Recker Mar 25 '13 at 19:21
  • I think ``1073741824`` should be actually the direct interpretation of internal representation of floating number ``5.65582``. – gongzhitaao Mar 25 '13 at 19:22
  • 1
    As maligned as `iostream`s are by some, this is a prime example of why `std::cout` should be used over `printf`, at least until simple learning mistakes are in the past. – Drew Dormann Mar 25 '13 at 19:30
  • If you're new to C++ you shouldn't be doing this kind of problem. The results depend on knowing the details of how floating-point values are represented on your hardware and how your compiler passes arguments to variadic functions, neither of which is specified by the language definition. You've gotten some guesses that seem to be based on particular assumptions about those details, but even if they're right, if you change compilers or computers you may well get different results. Formally, this is deep in the realm of what the standard calls "undefined behavior". – Pete Becker Mar 25 '13 at 19:53

2 Answers2

4

You're using the wrong printf format specifiers for the types of the arguments you're passing.

To print a float or double value, use "%f", "%g", or "%e", or some variant. For an int argument, use "%d".

By using a particular format string, you're promising to pass an argument of the proper type. If you fail to do so, you're lying to printf, and the resulting behavior is undefined. Don't do that.

(What's probably happening is that, for example, printf is taking the representation of the int argument you actually give it, and assuming that it's the representation of a double object. But don't waste time figuring out why you got the results you did. Just fix the code.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 1073741824 is the low 32 bits of `5.65582`, and -1713662420 is the low 32 bits of `p*q`. I do not see a match for -1561213759. – Eric Postpischil Mar 25 '13 at 19:33
  • Do you have to? The fact that it's undefined behaviour should be an indication that patterns you find will not always hold true. – chris Mar 25 '13 at 19:33
  • 1
    @chris: No, you do not have to find a match. But sometimes people learn things by looking behind the curtain. – Eric Postpischil Mar 25 '13 at 19:35
  • The first `printf` prints “2” for `%lf` because the high 32 bits of `5.65582` were consumed by `%lf`, leaving the low 32 bits followed by the high 32 bits of the next copy. As stated previously, the low 32 bits are 1073741824 (0x40000000), which starts a floating-point number near 2. Then the next 32 bits are too low in value to change the display at the default precision, so “2” is printed. – Eric Postpischil Mar 25 '13 at 19:39
  • Ah, -1561213759 is the low 32 bits of `p+q`. – Eric Postpischil Mar 25 '13 at 19:40
1

The actual representation of a double and float and int is very different inside the machine. Incorrect format specifiers in printf will try to interpret this binary representation as of the particular type.

Garbage in - garbage out

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 1
    The difference between `float` and `double` representations is not relevant here; `float` arguments are promoted to `double`. The problem is that the OP is mixing integers and floating-point. – Keith Thompson Mar 25 '13 at 19:24
  • @KeithThompson yes I was also going to add `int` in the mix. I forgot =P – Aniket Inge Mar 25 '13 at 19:26
  • But I was given that exact code and write down what I expect as output. Would my answers be acceptable or I'm missing something here?. – Kavin Thipthamai Mar 25 '13 at 19:37
  • @KavinThipthamai: Who gave you this code, and why would you expect any particular output given format strings that do not match the parameter types? – Eric Postpischil Mar 25 '13 at 19:41
  • My TA for my class. It was for hw assignment. – Kavin Thipthamai Mar 25 '13 at 19:43
  • Reeking of undefined behaviour like this? If the assignment itself wasn't an exercise in seeing the effects of undefined behaviour, make sure you get that changed for next time. – chris Mar 25 '13 at 19:44
  • @KavinThipthamai: This was a bad homework assignment, unless you were supposed to answer that the code provided has behavior that is not defined by the C++ standard. – Eric Postpischil Mar 25 '13 at 19:49