0

I was studying about formatted input output functions and found out that i could actually use printf() in a variety of ways, so I started trying out how to use flags in printf() . I was trying to understand # flag , it was stated that if I use it with e,E,f format specifier it "forces the written output to contain a decimal point even if no digits would follow ". So I thought what would happen if i did it with integer but with float formating and also + flag .

main()
{
  int  b = 0 , c = 4.53 , d = 3 ;
  float a = 1 ;
  double e = 5.32 ;//please dont mind a,b,c they are remanents from previous testing
  printf("%+#f" , d);    
  return 0 ;
}

Output - -1.#QNAN0

Your Help would be highly appreciated . Thanx in advance for taking your time to help me

0

1 Answer 1

0

You are invoking undefined behaviour, you can not pass an int when printf is expecting a double.

Variadic functions promotes float to double, so this works even if %f expects a double:

float f = 3.14;
printf("%f", f);

It also promotes char and short int to int:

char c = 1;
printf("%d", c);

But can not promote an int to double (as you were expecting), a cast is needed:

printf("%+#f", (double)d); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.