(C/C++学习笔记) 七. 类型转换

时间:2024-04-17 02:45:08

七. 类型转换

● 隐式类型转换

隐式类型转换 implicit type conversions

#include<iostream>

using namespace std;

void main()

{

double result;

char a='k';            //k的ASCII码为107

int b=10;

float e=1.515;

result=(a+b)-e;        //char转换为int,然后int转换为float

printf("%f\n",result);

}

(C/C++学习笔记) 七. 类型转换

(C/C++学习笔记) 七. 类型转换 (C/C++学习笔记) 七. 类型转换

● 显式类型转换

显式类型转换explicit type conversion

#include<iostream.h>

int main()

{

float z=7.56, fractionPart;

int integerPart;

integerPart=int(z);

fractionPart=z-(int)z;

cout<<integerPart<<endl;

cout<<fractionPart<<endl;

return 0;

}

(C/C++学习笔记) 七. 类型转换

type_name (expression)        //C++ cast notation

(type_name) expression        //C cast notation

※An expression consists of a combination of operators and operands. (An operand is what an operator operates on.) The simplest expression is a lone operand, and you can build in complexity from there. E.g.:

4

-6

4+21

a*(b + c/d)/20

q = 5*2

x = ++q % 3

q > 3