How # flag in printf works?

后端 未结 3 599
囚心锁ツ
囚心锁ツ 2021-01-28 16:03
#include 
int main()
{
  float x;
  x=(int)(float)(double)(5.5);
  printf(\"%#u\",x);
  return 0;
}

How the # flag in the p

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-28 16:07

    From c11 standard.

    7.21.6.1. p6:

    #:

    The result is converted to an ‘‘alternative form’’. For o conversion, it increases the precision, if and only if necessary, to force the first digit of the result to be a zero (if the value and precision are both 0, a single 0 is printed). For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to it. For a, A, e, E, f, F, g, and G conversions, the result of converting a floating-point number always contains a decimal-point character, even if no digits follow it. (Normally, a decimal-point character appears in the result of these conversions only if a digit follows it.) For g and G conversions, trailing zeros are not removed from the result. For other conversions, the behavior is undefined.

    So, to clarify, using # with u is undefined.

提交回复
热议问题