在本章中,我们将研究以下主题:
- C ++变量
- C ++数据类型
- C ++枚举
- C ++变量范围
- C ++存储类
- C ++转义序列
1. C ++变量
变量是赋予存储位置的名称。它们用于存储值。每个变量都与一个数据类型相关联,并且它包含使用其声明的类型的值。
变量声明:在使用变量之前,应先对其进行声明。声明将帮助编译器了解三件事。 1.变量名称。 2.变量的类型。 3.要分配的空间。
变量声明语法:
<data-type> <variable-name>;
可变初始化 声明变量后,将为该变量分配适当的内存。最初,它将具有无效值或垃圾值。因此,应将其初始化,否则输出将是意外的。
变量初始化示例:
int var = 0; // declaration and initialization
2. C ++数据类型
像大多数编程语言一样,C ++中有不同的数据类型。数据类型可帮助编译器分配适当的空间来存储变量。 C ++中的每个变量都与一个数据类型相关联。以下是可用的数据类型的类型。
1.原始数据类型:它们是编译器提供的基本数据类型。
int Ex: int a = 10; float Ex: float b = 10.23; char Ex: char c = 'a'; bool Ex bool d = true; double Ex double e = 12349.543; void Ex void *ptr = NULL; /*void means empty 数据类型 */
2.用户定义的数据类型:
structure class union enumerations
3.派生数据类型:
arrays function pointer reference
4.修饰符:
我们使用带有原始数据类型的修饰符来扩展变量的存储。
signed unsigned short long
注意:
- 并非所有修饰符都适用于所有数据类型。
- 所有4个修饰符都可以应用于int。
- 有符号和无符号均可应用于char。
- 长可以和双用。
- 一些修饰符可以组合。
例:
unsigned char a = 'a'; unsigned short int b = 10; unsigned long int c = 3456;
有符号和无符号之间的区别是,无符号变量将不存储数字的符号。要存储符号,将需要1个字节的数据。
下面的例子我们使用“sizeof”操作员知道每种变量类型占用了多少内存。
#include<iostream> using namespace std; int main() { cout<<"The size of int is: "<<sizeof(int) <<endl; cout<<"The size of char is: "<<sizeof(char) <<endl; cout<<"The size of short int is: "<<sizeof(short int) <<endl; cout<<"The size of long int is: "<<sizeof(long int) <<endl; cout<<"The size of float is: "<<sizeof(float) <<endl; cout<<"The size of double is: "<<sizeof(double) <<endl; return 0; }
输出:
The size of int is: 4 The size of char is: 1 The size of short int is: 2 The size of long int is: 8 The size of float is: 4 The size of double is: 8
3. C ++枚举:
C ++中的枚举用于限制变量的选项。假设我们有一个存储星期名称的数据类型。我们知道一周有7天。因此,我们可以用一个枚举来限制它。
句法:
enum <enum-variable-name> { list of names} variable-list;
例:
enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday} weekOfDay;
要么
week myWeek;
有两种声明枚举变量的方法。
方法1:
在枚举声明期间声明一个枚举变量。
#include<iostream> using namespace std; enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday} weekOfDay; int main() { weekOfDay = sunday; cout<<weekOfDay<<endl; weekOfDay = monday; cout<<weekOfDay<<endl; weekOfDay = tuesday; cout<<weekOfDay<<endl; return 0; }
输出:
0 1 2
方法2:
将枚举变量声明为普通变量。
#include<iostream> using namespace std; enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday}; int main() { week weekOfDay = wednesday; cout<<weekOfDay<<endl; weekOfDay = thursday; cout<<weekOfDay<<endl; weekOfDay = friday; cout<<weekOfDay<<endl; return 0; }
输出:
3 4 5
所有枚举变量将从0开始。
您可以通过在枚举声明期间分配一个值来更改默认值,如下所示。
后续选项将采用下一个数字。
#include<iostream> using namespace std; enum week {sunday =10 , monday, tuesday, wednesday, thursday, friday, saturday}; int main() { week weekOfDay = sunday; cout<<weekOfDay<<endl; weekOfDay = monday; cout<<weekOfDay<<endl; weekOfDay = friday; cout<<weekOfDay<<endl; return 0; }
输出量:
10 11 15
4. C ++变量范围:
- 局部变量。
- 全局变量。
1.局部变量:
- 它们在函数内部定义。
- 生命周期直到该功能的执行。
- 函数参数中的变量称为形式参数。
例:
#include<iostream> using namespace std; int main() { int i; //Declaration of local variables float f; i = 10; //initialisation of local variables f = 34.56; return 0; }
2.全局变量:
- 它们在函数外部定义。
- 他们的生命周期一直到程序执行为止。
- 它们可用于该文件或项目中的所有功能。
- 如果存在同名的局部变量,则该函数的局部变量将具有优先权。
例:
#include<iostream> using namespace std; int global = 30; int i = 50; int main() { int i; //Declaration of local variables float f; i = 10; //initialization of local variables f = 34.56; cout<<"The value of global is : "<<global<<endl; cout<<"The value of i is : "<<i<<endl; return 0; }
输出:
The value of global is : 30 The value of i is : 10
5. C ++存储类:
CPP中有5个存储类。
汽车 register static extern mutable
存储类有助于变量的可见性和范围。
汽车
- 声明局部变量时,它将被自动视为自动。
- 范围直到功能执行为止。
- 变量的生命周期直到函数执行为止。
- 初始值为垃圾值。
- 我们使用关键字“auto” to define a 汽车matic variable.
例:
#include<iostream> using namespace std; int main() { int i = 10; cout<<"The value of i is : "<<i<<endl; return 0; }
输出:
The value of i is : 10
寄存器:
- We can 上 ly declare local variables as 寄存器.
- 范围直到功能执行为止。
- 变量的生命周期直到函数执行为止。
- 初始值为垃圾值。
- 要求编译器将变量存储在计算机寄存器中。不保证它将能够做到。
- 我们使用关键字“register” to define a 寄存器 variable.
例:
寄存器 int i = 10;
静态的
- 当您将局部变量声明为静态变量时,不会为每个函数调用创建该变量的新实例。
- 范围直到功能执行为止。
- 变量的生命周期直到函数执行为止。
- 初始值为零。
- 如果将全局变量声明为静态变量,则该变量仅对该文件可见。
- 我们使用关键字“static” to define a 静态的 variable.
例:
#include<iostream> using namespace std; int func() { 静态的 int i = 1; int j = 1; i++; j++; cout<<"The value of i = "<<i<<endl; cout<<"The value of j = "<<j<<endl; return 0; } int main() { func(); func(); func(); func(); func(); return 0; }
输出:
The value of i = 2 The value of j = 2 The value of i = 3 The value of j = 2 The value of i = 4 The value of j = 2 The value of i = 5 The value of j = 2 The value of i = 6 The value of j = 2
如您所见,对于每个函数调用,i的值都被存储并递增,但是对于每个函数调用,j的值均被重置。
外部
- 我们使用extern变量从另一个文件访问变量。
- 该另一个文件应属于同一项目。
- 我们使用关键字“extern” to define an 外部 variable.
1.c
#include<iostream> using namespace std; extern int i; int main() { cout<<"The i value is :" <<i <<endl; return 0; }
2.c
#include<iostream> using namespace std; int i = 10;
输出:
g++ 2.cpp 1.cpp The i value is :10
可变的
可变存储类仅适用于对象。我们将在以后的章节中进行讨论。
6. C ++转义序列
\' Single quote \0 Null \\ Backslash \a Audible bell \" Double quote \b Backspace \? Question mark \f Form feed - new page \n New line \r Carriage return \t Horizontal tab \ooo Octal number
该网站上可用的教程列表:
C编程20+章 | C ++编程80+章 |
100多个编码问题 | 数据结构和算法85+章 |
系统设计20+章 | Shell脚本编写12章 |
4g LTE 60+章节 | 最常见的编码问题 |
5G NR 50+章 | Linux系统编程20+章 |