c ++基本数据类型
Data types help the associated variables understand the type of data they can store into them.
数据类型可帮助关联的变量了解它们可以存储到其中的数据类型。
![C++ Data Types](https://i-blog.csdnimg.cn/blog_migrate/8fb1d4b55a4cf697848743851e1eda74.png)
C ++中的用户定义数据类型 (User-Defined Data Types in C++)
C++ Language provides us with User-Defined Data types that are the data types created by the user/programmer.
C ++语言为我们提供了用户定义的数据类型,即用户/程序员创建的数据类型 。
Following are the types of user-defined types in C++:
以下是C ++中用户定义类型的类型:
- Structure 结构体
- Union 联盟
- Enumeration 枚举
- Class 类
1.结构 (1. Structure)
Structure type, groups elements of different data types under a custom data type (structure) and is represented by a single structure name.
结构类型, 将不同数据类型的元素分组到自定义数据类型 (结构)下,并由单个结构名称表示。
Syntax:
句法:
struct Structure_Name
{
Datatype data_member1;
Datatype data_member2;
.
.
Datatype data_memberN;
};
Example:
例:
struct Student_info
{
char name[100];
char address[100];
char division[50];
int roll_num;
};
2.联盟 (2. Union)
Union types serve the same functionality as that of the Structures.
联合类型具有与结构相同的功能。
The only difference is that in Unions, all the data members share the same space of memory which is equivalent to the size of the largest variable, during the execution of the program.
唯一的区别是,在联合程序中, 所有数据成员在程序执行期间共享相同的内存空间,该空间等于最大变量的大小 。
Syntax:
句法:
Union Union_Name
{
Datatype data_member1;
Datatype data_member2;
.
.
Datatype data_memberN;
};
Example:
例:
union Student_info
{
char name[100];
char address[100];
char division[50];
int roll_num;
};
In the above piece of code, name and address are largest among all the data members declared because we specified their size to 100.
在上面的代码中, 名称和地址在声明的所有数据成员中最大,因为我们将其大小指定为100。
So, the compiler will allocate the size of the largest variable i.e. name or address, to the memory storage to accommodate them and all the variables will share the same memory space (100) and address.
因此, 编译器会将最大变量的大小(即name或address )分配给内存存储以容纳它们,并且所有变量将共享相同的内存空间(100)和地址。
3.枚举 (3. Enumeration)
Enumeration types help increase the readability of the code. It assigns names to the integers from the program.
枚举类型有助于提高代码的可读性。 它将名称分配给程序中的整数。
These types are indexed from zero resembling the indexing fashion of arrays.
这些类型从零开始索引,类似于数组的索引方式。
Syntax:
句法:
enum enumeration_type_name{value1, value2,..valueN};
Example:
例:
#include <iostream>
using namespace std;
enum Days { Mon,
Tue,
Wed,
};
int main()
{
for (int i = Mon; i <= Wed; i++)
cout << i << " ";
return 0;
}
Output:
输出:
0 1 2
C ++中的派生数据类型 (Derived Data Types in C++)
The Derived types are derived or formed from the built-in/primitive data types.
派生类型是从内置/原始数据类型派生或形成的 。
Following are the types of derived data-types:
以下是派生数据类型的类型:
- Arrays 数组
- Function 功能
- Pointers 指针
1.数组 (1. Array)
An array is a linear data structure that stores the elements in contiguous memory locations in a linear/sequential manner. The elements are indexed from zero.
数组是线性数据结构 , 以线性/顺序方式将元素存储在连续的内存位置中 。 元素从零开始索引。
Syntax:
句法:
Data_type array_Name[size];
Example:
例:
#include <iostream>
using namespace std;
int main()
{
// Array Derived Type
int arr[4];
int sum=0;
cout<<"Enter the elements..\n";
for(int i =0; i<4; i++)
{
cin>>arr[i];
}
for(int i=0; i<4; i++)
{
sum+=arr[i];
}
cout<<"Addition of elements of array:\n ";
cout<<sum;
return 0;
}
Output:
输出:
Enter the elements..
10
20
30
40
Addition of elements of array:
100
2.功能 (2. Function)
Functions are a block of statements that particularly perform a set of tasks under it. They make the code more efficient and readable.
函数是一个语句块,特别是在其下执行一组任务。 它们使代码更加有效和可读。
Syntax:
句法:
Data_type function_Name(Arguments)
Example:
例:
#include <iostream>
using namespace std;
int sum(int a, int b) // function definition
{ int sum=0;
sum = a+b;
return sum;
}
int main()
{
int a = 20, b = 40;
int result = sum(a, b); // Calling the function
cout <<"Result of addition:\n"<< result;
return 0;
}
In the above snippet of code, we have performed an addition of two numbers using a function.
在上面的代码片段中,我们使用一个函数对两个数字进行了加法运算。
Output:
输出:
Result of addition:
60
3.指针 (3. Pointers)
Pointer types represent the address of the data members. They hold the address of another data member to which they point.
指针类型代表数据成员的地址。 它们保存了指向的另一个数据成员的地址。
Syntax:
句法:
data_type *variable;
Example:
例:
#include <iostream>
using namespace std;
int main()
{ int val = 10;
int* p;
p = &val;
cout<<"p = "<<p<<"\n"; //address of val
cout<<"val = "<<val<<"\n";
cout<<"*p = "<<*p<<"\n";
}
Output:
输出:
p = 0x7ffd328d4a34
val = 10
*p = 10
C ++中的原始数据类型 (Primitive Data Types in C++)
Primitive data types are the built-in data types directly available for the user to set out the operations.
基本数据类型是内置的数据类型,用户可以直接使用它们来设置操作 。
1.整数(int) (1. Integer (int))
Integer data type holds 2 bytes of memory. It ranges from 2147483648 to 2147483647.
整数数据类型保存2个字节的内存。 范围从2147483648到2147483647。
Syntax:
句法:
int variable = value;
2.字符(字符) (2. Character (char))
Character data types are used to store character values. Character types hold 1 byte of memory space and range from 128 to 127 or 0 to 255.
字符数据类型用于存储字符值 。 字符类型保存1个字节的内存空间,范围从128到127或0到255。
Syntax:
句法:
char variable = 'val';
3.浮点数(float) (3. Float (float))
Float data types are used to store single-precision data values i.e. decimal values. It holds 4 bytes of memory space.
浮点数据类型用于存储单精度数据值,即十进制值。 它拥有4个字节的存储空间。
Syntax:
句法:
float variable = val;
4.双(双) (4. Double (double))
Double data types are used to store double-precision floating-point data values. It holds 8 bytes of memory space.
双精度数据类型用于存储双精度浮点数据值 。 它拥有8个字节的存储空间。
Syntax:
句法:
double variable = val;
5.布尔值(布尔) (5. Boolean (bool))
Boolean types store and represent logical values. They represent the result in the form of True or False.
布尔类型存储并表示逻辑值 。 它们以True或False的形式表示结果。
bool variable = true/false;
6.虚空 (6. Void)
Void types represent entities without any value. They are used as a data type for functions that do not return any value.
无效类型表示没有任何值的实体。 它们用作不返回任何值的函数的数据类型。
结论 (Conclusion)
Thus, in this article, we have understood the data types in the C++ language.
因此,在本文中,我们已经了解了C ++语言中的数据类型。
参考资料 (References)
翻译自: https://www.journaldev.com/35012/data-types-in-c-plus-plus
c ++基本数据类型