2,039 views
2 votes
2 votes
following statment

printf("%f",9/5);

prints

 

answer is 2.0 can any one explain plzzzzz

1 Answer

Best answer
4 votes
4 votes

The reason is pretty simple. Any integer constant is considered as "int" in C and when we operate on "int" we get only "int" (even when we divide). So, 9/5 returns "int". This return value 1 (See behaviour of integer division). But there is another significant issue with the code - "%f" is used and an integer is passed to the function- which causes "undefined behaviour" in C (integer value won't be promoted to float). So, output can be anything.

 

pl.c:4:2: warning: format ‘%f’ expects argument of type ‘double’,  but argument 2 has type ‘int’ [-Wformat=]   
arjun@parambara:~/dump$ ./a.out 0.000000

Related questions

0 votes
0 votes
1 answer
1
3 votes
3 votes
0 answers
3
A_i_$_h asked Oct 10, 2017
964 views
int main(){float a =5.2;if(a==5.2)pf("equal");else if(a<5.2)pf("less than");elsepf("greater than");}