Cplusplus 简明教程
C++ Data Types
在任何语言中编写程序时,您需要使用各种变量来存储各种信息。变量不过是用于存储值的保留内存位置。这意味着,当您创建变量时,您就在内存中保留了一些空间。
While writing program in any language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
您可能想要存储各种数据类型的信息,如字符、宽字符、整数、浮点、双浮点、布尔值等。根据变量的数据类型,操作系统分配内存并确定可以在保留内存中存储什么。
You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Primitive Built-in Types
C 为程序员提供了各种内置数据类型以及用户自定义数据类型。下表列出了七个基本 C 数据类型:
C offers the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C data types −
Type |
Keyword |
Boolean |
bool |
Character |
char |
Integer |
int |
Floating point |
float |
Double floating point |
double |
Valueless |
void |
Wide character |
wchar_t |
可以使用这些类型修饰符中的一种或多种来修改几个基本类型:
Several of the basic types can be modified using one or more of these type modifiers −
-
signed
-
unsigned
-
short
-
long
下表显示变量类型、在内存中存储该值需要多少内存以及可以在此类变量中存储的最大值和最小值。
The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.
Type |
Typical Bit Width |
Typical Range |
char |
1byte |
-127 to 127 or 0 to 255 |
unsigned char |
1byte |
0 to 255 |
signed char |
1byte |
-127 to 127 |
int |
4bytes |
-2147483648 to 2147483647 |
unsigned int |
4bytes |
0 to 4294967295 |
signed int |
4bytes |
-2147483648 to 2147483647 |
short int |
2bytes |
-32768 to 32767 |
unsigned short int |
2bytes |
0 to 65,535 |
signed short int |
2bytes |
-32768 to 32767 |
long int |
8bytes |
-9223372036854775808 to 9223372036854775807 |
signed long int |
8bytes |
same as long int |
unsigned long int |
8bytes |
0 to 18446744073709551615 |
long long int |
8bytes |
-(2^63) to (2^63)-1 |
unsigned long long int |
8bytes |
0 to 18,446,744,073,709,551,615 |
float |
4bytes |
|
double |
8bytes |
|
long double |
12bytes |
|
wchar_t |
2 or 4 bytes |
1 wide character |
变量的大小可能不同于上表中显示的大小,具体取决于编译器和您使用的计算机。
The size of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.
以下是将在您的计算机上生成正确大小的各种数据类型的示例。
Following is the example, which will produce correct size of various data types on your computer.
#include <iostream>
using namespace std;
int main() {
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
此示例使用 endl ,它会在每行后插入换行符,并且 << 运算符用来将多个值传出到屏幕。我们还使用 sizeof() 运算符来获取各种数据类型的大小。
This example uses endl, which inserts a new-line character after every line and << operator is being used to pass multiple values out to the screen. We are also using sizeof() operator to get size of various data types.
当编译并执行上述代码时,它会生成以下结果,因机器而异 −
When the above code is compiled and executed, it produces the following result which can vary from machine to machine −
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4
以下是另一个示例:
Following is another example:
#include <iostream>
#include <limits>
using namespace std;
int main() {
std::cout << "Int Min " << std::numeric_limits<int>::min() << endl;
std::cout << "Int Max " << std::numeric_limits<int>::max() << endl;
std::cout << "Unsigned Int Min " << std::numeric_limits<unsigned int>::min() << endl;
std::cout << "Unsigned Int Max " << std::numeric_limits<unsigned int>::max() << endl;
std::cout << "Long Int Min " << std::numeric_limits<long int>::min() << endl;
std::cout << "Long Int Max " << std::numeric_limits<long int>::max() << endl;
std::cout << "Unsigned Long Int Min " << std::numeric_limits<unsigned long int>::min() <<endl;
std::cout << "Unsigned Long Int Max " << std::numeric_limits<unsigned long int>::max() << endl;
}
typedef Declarations
您可以使用 typedef 为现有类型创建新名称。以下是用 typedef 定义新类型的简单语法 −
You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef −
typedef type newname;
例如,以下内容告诉编译器 feet 是 int 的另一个名称 −
For example, the following tells the compiler that feet is another name for int −
typedef int feet;
现在,以下声明完全合法,并且创建了名为 distance 的整型变量 −
Now, the following declaration is perfectly legal and creates an integer variable called distance −
feet distance;
Enumerated Types
枚举类型声明一个可选的类型名称和一组零个或多个可用作该类型值的标识符。每个枚举器都是一个常数,其类型是枚举。
An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration.
创建枚举需要使用关键字 enum 。枚举类型的常规形式是 −
Creating an enumeration requires the use of the keyword enum. The general form of an enumeration type is −
enum enum-name { list of names } var-list;
此处,enum 名称是枚举的类型名称。名称列表以逗号分隔。
Here, the enum-name is the enumeration’s type name. The list of names is comma separated.
例如,以下代码定义一个名为 colors 的颜色的枚举和类型为 color 的变量 c。最后,为 c 分配值“蓝色”。
For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is assigned the value "blue".
enum color { red, green, blue } c;
c = blue;
默认情况下,第一个名称的值为 0,第二个名称的值为 1,第三个名称的值为 2,依此类推。但是,您可以通过添加初始化项来给名称一个特定值。例如,在以下枚举中, green 的值为 5。
By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. But you can give a name, a specific value by adding an initializer. For example, in the following enumeration, green will have the value 5.
enum color { red, green = 5, blue };
此处, blue 的值为 6,因为每个名称都比前一个名称大 1。
Here, blue will have a value of 6 because each name will be one greater than the one that precedes it.