Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading