#include<conio.h> #include<stdio.h> using namespace std; int main() { float divided; divided = 9/10; cout<<"divided = "<<divided; getch(); return 0; }
why is this code giving me output 0. i am using Dev C++ ver. 4.9.9.2
Because
9 / 10
is an integer operation with result zero. Zero implicitly cast to a float is still zero!
Correction:
divided = 9.0f / 10.0f ;
Note the f suffix explicitly makes the literal constants type float, otherwise they are double, and there would be an implicit cast in the assignment.
Log in to post a comment.
include<iostream>
why is this code giving me output 0. i am using Dev C++ ver. 4.9.9.2
Because
is an integer operation with result zero. Zero implicitly cast to a float is
still zero!
Correction:
Note the f suffix explicitly makes the literal constants type float,
otherwise they are double, and there would be an implicit cast in the
assignment.