C++语言程序设计 数据类型 型数值溢出例子 inc lude iostream using name space std int maino short int i;// signed short类型整数(-32768-32767) short unsigned int j;// unsigned short类型整数(0-65535) j=50000; i=j;/将 unsigned short赋值给 signed short变量 cout the short int is: i <endl //整型数值溢出 cout The short unsigned int is:<< jendl return 0: x The short int is: -15536 The short unsigned int is: 50000
C++语言程序设计 16 数据类型 ——整型数值溢出例子 #include <iostream> using namespace std; int main() { short int i; //signed short类型整数 (-32768-32767) short unsigned int j;//unsigned short类型整数(0-65535) j = 50000; i = j;//将unsigned short赋值给signed short变量 cout <<" The short int is:"<< i <<endl; //整型数值溢出 cout <<" The short unsigned int is:"<< j<<endl; return 0; } The short int is:-15536 The short unsigned int is:50000
C++语言程序设计 教据类型 实烈数据 #include <iostream> 基本数据类型和表达式 using namespace std int main( 实型变量 缺省为 double const int price=30:后级F(或f float4字节 int num. total 3.4×10±38 float v.rh 为 float型 7位有效数字num=10; 后缀L(或1) double8字节tota=num* PRICE; 为1 ong double 1.7×10±308 cout<<total-<cend:;|型 达15位有效数字Nr=25: long double h=3.2f 8字节v=314159r*rh 实型常量 1.7×0+308cout<<v<<endl; 15位有效数字 17
C++语言程序设计 17 #include <iostream> using namespace std; int main() { const int PRICE=30; int num,total; float v ,r,h; num=10; total=num*PRICE; cout<<total<<endl; r=2.5; h=3.2f; v=3.14159*r*r*h; cout<<v<<endl; } 数据类型 ——实型数据 实型常量 float 4字节 3.4×10±38 7位有效数字 double 8字节 1.7×10±308 15位有效数字 long double 8字节 1.7×10±308 15位有效数字 实型变量 •缺省为double型 •后缀 F(或 f) 为 float型 •后缀 L(或 l) 为 long double 型 基 本 数 据 类 型 和 表 达 式
C++语言程序设计 数据类烈 符型数据() 基本数据类型和表 字符常量 单引号括起来的一个字符,如:a,"D,"?, 字符变量 用来存放字符常量 例 char clc C2=A 达°字符数据在内存中的存储形式 以ASCI码存储,即以整数表示,占1字节,用7个三进 式制位 18
C++语言程序设计 18 数据类型 ——字符型数据(一) ⚫ 字符常量 – 单引号括起来的一个字符,如:'a', 'D', '?', '$' ⚫ 字符变量 – 用来存放字符常量 例:char c1,c2; c1='a'; c2='A'; ⚫ 字符数据在内存中的存储形式 – 以ASCII码存储,即以整数表示,占1字节,用7个二进 制位 Page 18 基 本 数 据 类 型 和 表 达 式
C++语言程序设计 数据类型 字符烈数据(二) 基本数据类型和表 字符数据的使用方法 字符数据和整型数据之间可以运算。 字符数据与整型数据可以互相赋值 字符串常量 例:" CHINA" CHINAIO ao 式所以: char c
C++语言程序设计 19 ⚫ 字符数据的使用方法 – 字符数据和整型数据之间可以运算。 – 字符数据与整型数据可以互相赋值。 ⚫ 字符串常量 例:"CHINA" "a" 'a ' 所以:char c; c="a"; C H I N A \0 a \0 a Page 19 数据类型 基 ——字符型数据(二) 本 数 据 类 型 和 表 达 式
C++语言程序设计 数据类型 布尔型数据 基本数据类型和表达式 ●布尔型变量的说明 例: bool flag; 布尔型数据的取值: 只有 false和true两个值 x
C++语言程序设计 20 数据类型 ——布尔型数据 ⚫ 布尔型变量的说明: 例:bool flag; ⚫ 布尔型数据的取值: 只有 false 和 true 两个值 基 本 数 据 类 型 和 表 达 式