printf

/* printf example */

#include <stdio.h>

int main()
{
printf ("%f\n", 172);
return 0;
}

what does the segment printf ("%f\n", 172); do ? Is it decimal to floating point conversion???


Since there is a mismatch between the format and the type of the argument it results in undefined behavior, i.e., in theory anything can happen. So this is an error. Your compiler may emit a warning.

In practice it is most likely that one of two things will occur:

1. the bit pattern for the int (and the 4 bytes contiguous to it on the stack if ints are 32-bits) are interpreted as a double floating point value.

2. The register reserved for passing a double float (which contains whatever was last written to it) is printed.

On my system it's the second option. E.g., the following program prints 1234.000000 twice for me.

1
2
3
4
5
6
7
#include <stdio.h>

int main() {
    printf("%f\n", 1234.0);
    printf("%f\n", 172);
    return 0;
}

Last edited on
Topic archived. No new replies allowed.