0
void main()
{
printf("%f",12/7.0);
getch();
}

Will the type conversion take place in this code and if yes then why it is taking place and if no why it is not taking place in this code please explain? Also I think 7.0 which is of double data type here (according to rule of type conversion) and 12 which is of integer data type would give data type double but when I print with %lf then the output on screen is again float I don't know how please correct me ?

  • 2
    See [Implicit type promotion rules](https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules). Specifically "the usual arithmetic conversions". You need to understand that the constants `12` and `7.0` have types just like variables. – Lundin May 11 '21 at 13:51
  • For printf arguments there is this https://en.cppreference.com/w/c/language/conversion#Default_argument_promotions and if you are wondering what type 7.0 is, see https://en.cppreference.com/w/c/language/floating_constant –  May 11 '21 at 13:54
  • To add to @Lundin useful comment, you can qualify constants with literals : `printf("%f",12.0f/7.0f);` for example qualify the constants as `float` (literal suffix f). – Zilog80 May 11 '21 at 14:19
  • 1
    What makes you think that the output is `float`? What do you expect to see if it is `double`? – the busybee May 11 '21 at 15:13
  • @the busybee because I have used format specifier %f therefore I think the output should convert from double (12/7.0) (because I have learnt 7.0 would as a constant would be stored as double in the memory) to float and if I use double (because I think that the expression (12/7.0) would be double according to type conversion and that should be stored in double format specifier %lf) and give output upto 15 decimal places but it is giving output only upto 6 decimal places tell me where i am wrong and if it has to do something with different rule please explain that rule also? – pratik jain May 11 '21 at 15:44
  • 1
    I was afraid that you mis-interpreted things, and I seem to be right. So please read Jean-Baptiste's answer, and I'm sure you will mark it. ;-) However, a closer look in any good C book could have revealed this to you. – the busybee May 11 '21 at 17:43

1 Answers1

2

You can't deduce the type of the expression by the result of the printing. printf may show you only part of the value...

man printf says for f or F format (emphasize is mine):

fF

The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal- point character appears. If a decimal point appears, at least one digit appears before it.

12 is an int literal, 7.0 is a double literal. By the rules of expression evaluation int will be promoted to double and the result will be a double that is printed according to the format (f is not for float but for double).

If you want to print the value computed by a float division, you need to restrict to float using:

12/7.0f

The result will be float, and you may ask why do I use a double specifier then? Because, in any variadic function every float will be promoted to double. As printf is a variadic function... This is why there is no float specifier in format string of printf.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69