《C++新经典》第2章 数据类型、运算符与表达式

92 篇文章 19 订阅
15 篇文章 2 订阅

2.1 常量、变量、整型、实型和字符型

2.1.2 C语言数据类型

  • 基本类型
      数值类型
        整型
          短整型(short)
          整型(int)
          长整型(long)
        浮点型
          单精度型(float)
          双精度型(double)
      字符类型(char)
  • 构造类型
      数组
      结构体(struct)
      共用体(union)
      枚举类型(enum)
  • 指针类型
  • void空类型
数据类型64位系统
sizeof(char)1
sizeof(short)2
sizeof(int)4
sizeof(long)4
sizeof(long long)8
sizeof(float)4
sizeof(double)8
sizeof(long double)16
sizeof(void *)8
sizeof(char *)8
#include <iostream>
using namespace std;

int main() {
    cout <<"sizeof(char): " <<sizeof(char) <<endl;
    cout <<"sizeof(short): " <<sizeof(short) <<endl;
    cout <<"sizeof(int): " <<sizeof(int) <<endl;
    cout <<"sizeof(long): " <<sizeof(long) <<endl;
    cout <<"sizeof(long long): " <<sizeof(long long) <<endl;
    cout <<"sizeof(float): " <<sizeof(float) <<endl;
    cout <<"sizeof(double): " <<sizeof(double) <<endl;
    cout <<"sizeof(long double): " <<sizeof(long double) <<endl;
    cout <<"sizeof(void *): " <<sizeof(void *) <<endl;
    cout <<"sizeof(char *): " <<sizeof(char *) <<endl;
    return 0;
}

2.1.3 常量和变量

标识符:字母、数字、下划线组成,非数字开头。
保留字:系统保留,特殊用途。
and
asm
auto
bool
break
case
catch
char
class
const
const_cast
continue
default
delete
do
extern
flase
float
for
friend
goto
if
inline
int
long
mutable
namespace
new
not
operator
or
private
protected
public
register
reinterpret_cast
return
short
signed
struct
sizeof
static
static_cast
throw
switch
template
this
typeid
true
try
typedef
using
typename
union
unsigned
virtual
void
volatile
while
xor

常量

值不能被改变的量,一般字面值。

  • 整型常量
    150
  • 浮点型常量(实型常量)
    12.3
  • 字符常量
    ‘a’

变量

值可以被改变的量,肯定有变量名(就是标识符),占据内存存储空间。

类型名 变量名[=变量初值];
int iMemberCount; //i表示int

2.1.4 整型数据

  • 十进制数
    -456
  • 八进制数
    012
  • 十六进制数
    0x2f
    0X3B
//基本型、短整型、长整型、无符号型
//[unsigned] short | int | long | long long
//定义变量时初始化,非赋值语句
long test3 = 189L;//189l
int a = 23.12F;//23.12f
unsigned abc = 23U;//23U
//一般L、F、U等无意义,实际类型取决于变量类型

2.1.5 实型数据

  • 十进制数表示
    0.12
    3.14159
  • 指数形式
    16E2
    1.63e-5
//float,double

2.1.6 字符型数据

  • 字符常量
'a'
  • 转义字符
    特殊字符,\开头的字符序列,将\后面字符转变成另外的含义。
\n
\t
\\
\'
\"
\ddd,八进制
\xhh,十六进制
\0,空标志
  • 字符变量
char c = '\'';

ASCII码,0~127间数字。
32,空格
33,!
34, "
35,#
36,$
48,0
49,1
65,A
66,B
97,a
98,b

char c = 98;
printf("c = %c, c = %d\n", c, c);

c += 4;
printf("c = %c, c = %d\n", c, c);

2.1.7 字符串变量

一对双引号包含起来的一堆字符。
‘a’,字符常量,1个字节。
“a”,字符串常量,2个字节,'\0’字符串结束标记。

2.1.8 变量赋初值

变量定义的同时赋值。

int a, c = 6;

2.1.9 数字型变量间的混合运算

不同类型数据在一起运算,系统尝试将变量类型统一(转换为最大数值类型的变量类型),然后运算。

  • 类型从低到高。
    char
    unsigned char
    short
    unsigned short
    int
    unsigned
    long
    unsigned long
    long long
    unsigned long long
    float
    double
    long double

  • char,unsigned char,short,unsigned short,int运算强制转换为int类型(即使类型相同数据运算),然后再运算。

  • 其他类型间运算,转换为高类型后再运算。

#include <iostream>
#include <typeinfo>
using namespace std;

int main() {
    char c;
    unsigned char uc;
    short s;
    unsigned short us;
    int i;
    unsigned u;
    long l;
    unsigned long ul;
    long long ll;
    unsigned long long ull;
    float f;
    double d;
    long double ld;

    cout <<"typeid:" <<endl;
    cout <<"typeid(char): " <<typeid(c).name() <<endl;
    cout <<"typeid(unsigned char): " <<typeid(uc).name() <<endl;
    cout <<"typeid(short): " <<typeid(s).name() <<endl;
    cout <<"typeid(unsigned short): " <<typeid(us).name() <<endl;
    cout <<"typeid(int): " <<typeid(i).name() <<endl;
    cout <<"typeid(unsigned): " <<typeid(u).name() <<endl;
    cout <<"typeid(long): " <<typeid(l).name() <<endl;
    cout <<"typeid(unsigned long): " <<typeid(ul).name() <<endl;
    cout <<"typeid(long long): " <<typeid(ll).name() <<endl;
    cout <<"typeid(unsigned long long): " <<typeid(ull).name() <<endl;
    cout <<"typeid(float): " <<typeid(f).name() <<endl;
    cout <<"typeid(double): " <<typeid(d).name() <<endl;
    cout <<"typeid(long double): " <<typeid(ld).name() <<endl;
    cout <<endl;

    cout <<"typeid(*+unsigned char):" <<endl;
    cout <<"typeid(unsigned char+unsigned char): " <<typeid(uc+uc).name() <<endl;
    cout <<"typeid(short+unsigned char): " <<typeid(s+uc).name() <<endl;
    cout <<"typeid(unsigned short+unsigned char): " <<typeid(us+uc).name() <<endl;
    cout <<"typeid(int+unsigned char): " <<typeid(i+uc).name() <<endl;
    cout <<"typeid(unsigned+unsigned char): " <<typeid(u+uc).name() <<endl;
    cout <<"typeid(long+unsigned char): " <<typeid(l+uc).name() <<endl;
    cout <<"typeid(unsigned long+unsigned char): " <<typeid(ul+uc).name() <<endl;
    cout <<"typeid(long long+unsigned char): " <<typeid(ll+uc).name() <<endl;
    cout <<"typeid(unsigned long long+unsigned char): " <<typeid(ull+uc).name() <<endl;
    cout <<"typeid(float+unsigned char): " <<typeid(f+uc).name() <<endl;
    cout <<"typeid(double+unsigned char): " <<typeid(d+uc).name() <<endl;
    cout <<"typeid(long double+unsigned char): " <<typeid(ld+uc).name() <<endl;
    cout <<endl;

    cout <<"typeid(*+char):" <<endl;
    cout <<"typeid(char+char): " <<typeid(c+c).name() <<endl;
    cout <<"typeid(unsigned char+char): " <<typeid(uc+c).name() <<endl;
    cout <<"typeid(short+char): " <<typeid(s+c).name() <<endl;
    cout <<"typeid(unsigned short+char): " <<typeid(us+c).name() <<endl;
    cout <<"typeid(int+char): " <<typeid(i+c).name() <<endl;
    cout <<"typeid(unsigned+char): " <<typeid(u+c).name() <<endl;
    cout <<"typeid(long+char): " <<typeid(l+c).name() <<endl;
    cout <<"typeid(unsigned long+char): " <<typeid(ul+c).name() <<endl;
    cout <<"typeid(long long+char): " <<typeid(ll+c).name() <<endl;
    cout <<"typeid(unsigned long long+char): " <<typeid(ull+c).name() <<endl;
    cout <<"typeid(float+char): " <<typeid(f+c).name() <<endl;
    cout <<"typeid(double+char): " <<typeid(d+c).name() <<endl;
    cout <<"typeid(long double+char): " <<typeid(ld+c).name() <<endl;
    cout <<endl;

    cout <<"typeid(*+long long):" <<endl;
    cout <<"typeid(long long+long long): " <<typeid(ll+ll).name() <<endl;
    cout <<"typeid(unsigned long long+long long): " <<typeid(ull+ll).name() <<endl;
    cout <<"typeid(float+long long): " <<typeid(f+ll).name() <<endl;
    cout <<"typeid(double+long long): " <<typeid(d+ll).name() <<endl;
    cout <<"typeid(long double+long long): " <<typeid(ld+ll).name() <<endl;
    cout <<endl;

    cout <<"typeid(*+unsigned long long):" <<endl;
    cout <<"typeid(unsigned long long+unsigned long long): " <<typeid(ull+ull).name() <<endl;
    cout <<"typeid(float+unsigned long long): " <<typeid(f+ull).name() <<endl;
    cout <<"typeid(double+unsigned long long): " <<typeid(d+ull).name() <<endl;
    cout <<"typeid(long double+unsigned long long): " <<typeid(ld+ull).name() <<endl;
    cout <<endl;

    cout <<"typeid(*+float):" <<endl;
    cout <<"typeid(float+float): " <<typeid(f+f).name() <<endl;
    cout <<"typeid(double+float): " <<typeid(d+f).name() <<endl;
    cout <<"typeid(long double+float): " <<typeid(ld+f).name() <<endl;
    cout <<endl;

    cout <<"typeid(*+double):" <<endl;
    cout <<"typeid(double+double): " <<typeid(d+d).name() <<endl;
    cout <<"typeid(long double+double): " <<typeid(ld+d).name() <<endl;
    cout <<endl;

    cout <<"typeid(int+unsigned): " <<typeid(i+u).name() <<endl;
    cout <<"typeid(char+char): " <<typeid(c+c).name() <<endl;

    return 0;
}

2.2 算术运算符和表达式

2.2.1 C语言的运算符

  1. 算术运算符
+ - * / %
  1. 关系运算符
< = >= <= == !=
  1. 逻辑运算符
! && ||
  1. 位运算符
<< >> ~ & | ^
  1. 赋值运算符
= += -= *= /= != <<=
  1. 条件运算符
?:
  1. 逗号运算符
,
  1. 指针运算符
* &
  1. 求占字节数运算符
sizeof()
  1. 强制类型转换运算符
(类型名)
  1. 成员变量运算符
. ->
  1. 下标运算符
[]
  1. 其他

2.2.2 算术运算符和算术表达式

+ - * / %
5/3,整数相除,舍弃小数部分
%,求余,要求两侧为整数

2.2.3 运算符优先级问题

从高到低:

  1. () [] -> .
  2. ! ~ ++ – (类型) * & sizeof() (从右到左)
  3. * / %
  4. - +
  5. << >>
  6. < <= > >=
  7. == !=
  8. &
  9. ^
  10. |
  11. &&
  12. ||
  13. ?: (从右到左)
  14. = += -= *= /= %= … (从右到左)
  15. ,

2.2.4 强制类型转换运算符

(类型名)(表达式名)

2.2.5 自增和自减运算符

++
--

2.3 赋值运算符和逗号运算符

2.3.1 赋值运算符和赋值表达式

  1. 赋值运算符
    “=”,将等号右边的值给等号左边的变量。

定义时初始化(赋初值)语句,非赋值语句。

char a = 100;
  1. 复合的赋值运算符
int a;
a = 100;

a += 3;

int x=2, y=4;
x *= y+8;
  1. 赋值表达式的值
    等号右侧的值。
int a=b=c=3;

a += a -= a*a;
printf("a=%d\n", a);

/*
a*a == 9;
a += a -= 9;

a = a-9 = -6;
a += -6;

a = a+(-6) = -6+(-6) = -12;
*/

2.3.2 逗号运算符和逗号表达式

逗号运算符“,”,优先级最低的运算符。
逗号表达式,“,”将多个表达式连接起来的更长的一个表达式。

int x, a;
int result = (x=a=3, 6*a);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值