C Programming: Output of Float Calculation

Click For 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

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K