#include <stdio.h>
int main()
{
float x;
x=(int)(float)(double)(5.5);
printf("%#u",x);
return 0;
}
How the #
flag in the printf is working here?
Everytime I run this code I get different(garbage) values.
I know that the #
flag works only with o , 0x, 0X, e, E, f, g, G
but when it is not defined for integers.
So is this an Undefined behaviour? I am getting correct values when I am using the above flags. So tell me whether I am right or wrong.
From the manual page:
So yes, it's undefined.
From c11 standard.
7.21.6.1. p6:
So, to clarify, using
#
withu
is undefined.Using this flag with any other than the listed conversions is undefined behaviour. Don't use it with other conversions.
(taken from the printf(3)-manpage. Wording is essentially the same as in the standard. Emphasis mine)