Data type conversion in c ++

A, c ++ data type conversion is generally in the following situations:

1, the value of an arithmetic type arithmetic assign another type of variable, c ++ will convert in

2, when contained in the expression of different types, c ++ will convert in

3, the parameters passed to the function, c ++ will convert in

Second, during the data type conversion when the points to note:

1, the smaller data types into a large data type, no problem, generally just after conversion of bytes occupied by more, but you want to large data types into smaller data types, it would result the loss of data.

2, the larger float into smaller floating point number converted to double as float, which causes the precision (the number of effective bits) decreases, the value may exceed the range of the target type, in this case, the result will be It is uncertain.

3, to convert floating point to integer type, which can cause the fractional part is lost, the original value may exceed the target type of the range, in this case, the result will be uncertain.

4, when the operation involves two types of data, generally smaller types will be converted to a larger type.

Third, the cast:

1, in the form of:

a, (typeName) value (C language wording)

b, value (typeName) (c ++ wording)

c, static_cast <> value may be converted from one type to another numeric data type, format: static_cast <typeName> (value)  

E.g:

 1 #include <iostream>
 2 int main()
 3 {
 4     using namespace std;
 5     int auks ,bats , coots;
 6     auks=19.99+11.99;
 7     
 8     bats=(int)19.99+(int)11.99;
 9     coots=int(19.99)+int(11.99);
10     cout<<"auks="<< auks <<",bats = "<< bats;
11     cout<<",coots = "<<coots<<endl;
12     
13     char ch ='Z';
14     cout<<"The code for " << ch << "is";
15     cout<<int(ch)<<endl;
16     cout<<"yes,the code is ";
17     cout<<static_cast<int>(ch)<<endl;
18     return 0;
19 }

The results are as follows:

auks=31,bats = 30,coots = 30
The code for Zis90
yes,the code is 90

IV Summary:

Size sorting integer data types: bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long

Sort size float: float, double, long double

 

Guess you like

Origin www.cnblogs.com/1121518wo/p/11210591.html