0

I don't know why I am getting 0.000000 in return if I print integer variable value with %f.

`int main()
 {
    int age = 18;
    printf("%f",age);
    return 0;
 }`

According to my thinking, It should return 18.000000 as I am printing integer value in float number format.

  • 1
    Because you did not pass the `double` required by `%f`. Try `printf("%f", (double)age);` A good compiler will warn you about the mismatch. – Weather Vane Feb 11 '17 at 18:34
  • @WeatherVane: I more and more think we should make a test before a question can be posted: "what is undefined behaviour". – too honest for this site Feb 11 '17 at 18:36
  • @Olaf I am sure I have seen Help advice not to ask questions like *Why does `a = a++ * --a` print 42?* but I can't easily find it now. Perhaps you could make post on Meta? – Weather Vane Feb 11 '17 at 18:45
  • @WeatherVane It is on the info-page together with some others:http://stackoverflow.com/tags/c/info . But apparently not read very often. Asking seems to be easier than reading first. Yet I don't see such a meta will succeed. And I'm not in the mood for meta-fights. – too honest for this site Feb 11 '17 at 21:27
  • @Olaf would be a good idea if that page appears when any noob tries to post a C tag question. – Weather Vane Feb 11 '17 at 21:31
  • @WeatherVane: Indeed. But maybe I'm getting old, but I'm not very optimistic this will safe us many such questions. I had someone insisting his was not a dup as he used something like `a = (a += 5)`, not `i++ + ++i` ... – too honest for this site Feb 11 '17 at 21:39

1 Answers1

0

It's undefined behaviour because when you use the wrong format specifier in C.

C99 standard 7.19.6.1 paragraph 9 say's:

If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

msc
  • 33,420
  • 29
  • 119
  • 214
  • Please refer to this accepted answer for explanation, if you want to dig into C++ standards: http://stackoverflow.com/questions/11673503/using-f-to-print-an-integer-variable?answertab=active#tab-top – valleymanbs Feb 11 '17 at 18:41
  • @valleymanbs: This is about C, not C++. They are different languages; if you want to push an answer for C++, ask a question and self-answer it. – too honest for this site Feb 11 '17 at 21:29
  • Please updated your documents. C99 is not standard C since 6 years. You could use the final draft n1570. – too honest for this site Feb 11 '17 at 21:30
  • @Olaf, sorry, in my comment I dropped two plus after the C, but it's my mistake. If you try to read that answer, it refers to standard C. Also, the reference used is the final draft n1570 you are suggesting. – valleymanbs Feb 12 '17 at 00:59
  • 1
    @valleymanbs: I see. But this does not make sense in a comment to another answer. Instead flag the question as dup (comment at the answer if you don't have the reps, higher rep users often appreciate such help to close). Nitpick: there is only one C++ standard, as there is only one C standard. – too honest for this site Feb 12 '17 at 06:26