Home > other >  Implicit casting from float to integer in C — why not the other way around?
Implicit casting from float to integer in C — why not the other way around?

Time:08-10

Based on C's implicit casting rule, data types are converted to higher type.

Nerveless when I try:

float a; 
int b; 
a = b = 3.4;

The output is always an integer number for both a and b. Can I know the reason behind this? Why is it not converting int to float?

CodePudding user response:

Assignment (=) has right-to-left associativity (see operator precedence) so float a; int b; a = b = 3.4; is the same as:

float a;
int b;
b = 3.4; // b is now 3   (since it can only hold integer values)
a = b;   // a is now 3.f
  •  Tags:  
  • c
  • Related