Suppose I have a floating point number x
and I want to convert it into an integer. Is there any difference between int(x)
and (int)x
in C++.
I am new to C++ coming from Python background and naturally I tried int(x) to convert x to an integer but reading about C++ I came across type-casting i.e. (int)x
and in this case it also works.
I was wondering what's the difference between these two approaches and in a wider context which one to use where?
float x = 5.5;
cout<<int(x)<<endl; //outputs 5
cout<<(int)x<<endl; //outputs 5