Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why result output is integer even result is defined as float?

Tags:

c++

#include<iostream>

using namespace std;

int main(int argc, char** argv){
        float result;
        result=(340/101);
        cout<<"Result="<<result;

}

in this code result is=3, i'm curious about why is that? what is the prevention of this cause and it can be seen a minor problem but it really deprecate my results in my data analysis program.

EDIT: thanks for the answers, i was trying the code below and it also gives me 3, so that's why i confused now i'm clear.

result=(float)(340/101);

Thanks all

like image 703
berkay Avatar asked May 22 '26 00:05

berkay


1 Answers

Because the expression (340/101) is evaluated independent of its surrounding context. int/int always results in an int. It makes no difference if you store that int in a float variable later on.

like image 184
fredoverflow Avatar answered May 24 '26 19:05

fredoverflow