C Programming: Output of Float Calculation

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
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.