CPlusPlus[Tutorials]:变量,类型,及初始化

一:标识符

  • 变量需要一个名字,称为标识符
  1. 标识符,一般是由字母,数字,字符组成,一般标识符首位是字母,也可以是下划线 _ ,但绝对不可以是数字
    # age 就是变量的标识符
    int age = 18;

  2. 关键字--即从c++保留的标识符,不能用于作为变量的名字
    alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch,
    
     char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, 
    
    decltype, default, delete, do, double, dynamic_cast, else, enum, explicit, export,
     
    extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace,
    
     new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected,
    
     public, register, reinterpret_cast, return, short, signed, sizeof, static, 
    
    static_assert, static_cast, struct, switch, template, this, thread_local, throw, 
    
    true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, 
    
    volatile, wchar_t, while, xor, xor_eq

 

二:基本数据类型

  • 基本数据类型分为算数类型和复合数据类型

其中基本数据类型有:

1. 字符类型,2.整数类型(无符号与有符号),3.浮点类型 , 4.布尔类型 , 5.空类型, 6.空指针

  • 其中只有字符类型的char类型是明确的1字节(8位),其余的数据类型位数只有下限,具体多少位要看操作系统和编译器(可移植性更强),具体看下图:
GroupType names*Notes on size / precision
Character typescharExactly one byte in size. At least 8 bits.
char16_tNot smaller than char. At least 16 bits.
char32_tNot smaller than char16_t. At least 32 bits.
wchar_tCan represent the largest supported character set.
Integer types (signed)signed charSame size as char. At least 8 bits.
signed short intNot smaller than char. At least 16 bits.
signed intNot smaller than short. At least 16 bits.
signed long intNot smaller than int. At least 32 bits.
signed long long intNot smaller than long. At least 64 bits.
Integer types (unsigned)unsigned char(same size as their signed counterparts)
unsigned short int
unsigned int
unsigned long int
unsigned long long int
Floating-point typesfloat
doublePrecision not less than float
long doublePrecision not less than double
Boolean typebool
Void typevoidno storage
Null pointerdecltype(nullptr)

三:初始化

  • 初始化即在声明变量时就给变量一个特定的值

c++ 初始化有三种方式:

1.像c语言一样的初始化(c-like initialization)

int age = 18;         // cpp语言要求:标识符age前要指定数据类型;因为继承自c语言

2.构造初始化(constructor initialization)

int age(18);         // cpp 语言引入的初始化方式

3.统一初始化(uniform initialization)

int age {18};        // C++ standard 2011 引入的一种初始化方式

 参考链接

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值