Simple Question in C++

In below program, I am assigning the float value to an integer variable, WHy the error is not coming??
Program is running without any errors WHY???


1
2
3
4
5
6
7
8
9
10
11
#include<iostream.h>
#include<stdlib.h>
int main()
{
system("CLS");
int i=3.2;
cout<<i;
system("pause");
return 0;
}
Last edited on
Because the compiler is doing an implicit (Automatic) conversion from the float to a integer. In this case the conversion is to "chop" off the decimal portion of the number and assign what is left to the integer.

Here is a nice tutorial on the site that goes into detail about this subject http://www.cplusplus.com/doc/tutorial/typecasting/
Last edited on
It should run without errors (provided you #include <iostream> instead of iostream.h), although the value of i would be 3, not 10.

As a QOI issue, one would expect a warning, which VS supplied:
warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data


http://ideone.com/bENmTw

If your compiler results in the value 10 being printed out, it is broken. (Of course, one might say it's already broken if it allows you to do the includes as you have.)
Of course, one might say it's already broken if it allows you to do the includes as you have
Or it was released before 1998 when namespaces were introduced and iostream.h became iostream. Turbo C++ is main example I often see still used.
Well, that does not make it more useful. So, to OP: update your compiler to something which wasn't made in previous millenium.
notice that it only print out
3
not
3.2
because,
Of course, you can actually convert a float value to an integer value and vice versa
Topic archived. No new replies allowed.