C Programming: Output of Float Calculation

AI Thread Summary
The output of the given C program is 1.0 due to integer division. In the expression 6/4, both numbers are integers, resulting in an integer output of 1 before being converted to a float. This behavior is consistent with C's handling of arithmetic operations, where integer division yields an integer result. The final output is formatted to one decimal place, hence it appears as 1.0. Understanding this concept is crucial for accurate float calculations in C programming.
ubuntu2
Messages
7
Reaction score
0
The question is
What will be the output?
#include<stdio.h>
int main(void)
{float y=6/4;
printf("%.1f\n",y);}
The answer is
1.0
I got that right by trusting my senses:biggrin:; but I want to understand why.
 
Last edited:
Physics news on Phys.org
C does maths in the 'simplest' way possible, so 6/4 is done as an integer division = the answer is an integer which is then converted to a float.
 
right! integer operated by integer results in integer!
thanks a lot.
 

Similar threads

Back
Top