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

七. 类型转换

● 隐式类型转换

隐式类型转换 implicit type conversions

#include<iostream>

using namespace std;

void main()

{

    double result;

    char a='k';            //kASCII码为107

    int b=10;

    float e=1.515;

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

    printf("%f ",result);

}

 

 

● 显式类型转换

显式类型转换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;

}

 

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

 

原文地址:https://www.cnblogs.com/ArrozZhu/p/8377714.html