c ++基本数据类型

Data types help the associated variables understand the type of data they can store into them.

数据类型可帮助关联的变量了解它们可以存储到其中的数据类型。

C++ Data Types C ++数据类型


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.

因此, 编译器会将最大变量的大小(nameaddress )分配给内存存储以容纳它们,并且所有变量将共享相同的内存空间(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 functioncout <<"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 valcout<<"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.

布尔类型存储并表示逻辑值 。 它们以TrueFalse的形式表示结果。


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)

  • C++ Data Types DocumentationC ++数据类型文档
  • C++ Built-In Data types DocumentationC ++内置数据类型文档

翻译自: https://www.journaldev.com/35012/data-types-in-c-plus-plus

c ++基本数据类型

c ++基本数据类型_C ++中的数据类型相关推荐

  1. c#访问修饰符修饰无效_C中的数据类型和修饰符

    c#访问修饰符修饰无效 Data Types In C C中的数据类型 C中的数据类型 (Data Types in C) C has various data types to store data ...

  2. c语言中浮点数和整数转换_C中的数据类型-整数,浮点数和空隙说明

    c语言中浮点数和整数转换 C中的数据类型 (Data Types in C) There are several different ways to store data in C, and they ...

  3. js 数据类型_js中检测数据类型的方法汇总

    回顾一下js中数据类型分为两大类,基础类型和引用数据类型,主要有: 基本数据类型 Number String Boolean Null Undefined Symbol 复杂数据类型 Array Fu ...

  4. python中什么是主要数据类型_python中基本数据类型是什么

    python中基本数据类型是什么,变量,整型,类型,赋值,等号 python中基本数据类型是什么 易采站长站,站长之家为您整理了python中基本数据类型是什么的相关内容. python中基本数据类型 ...

  5. r如何查询mysql中的数据类型_MySQL-mysql中的数据类型

    mysql中的数据类型 数值类型 严格数值数据类型(INTEGER.SMALLINT.DECIMAL和NUMERIC), 关键字INT是INTEGER的同义词,关键字DEC是DECIMAL的同义词. ...

  6. python语言数据类型_Python中的数据类型 | 萧小寒

    摘要 Python3中提供有六个标准的数据类型:Number(数字).String(字符串).List(列表).Tuple(元组).Sets(集合).Dictionary(字典). 在这些数据类型中, ...

  7. java异或运算结果数据类型_java中的数据类型和运算符的总结归类。

    首先学习java肯定先要了解java的发展史,以及java的特点,常见的dos命令,jdk的安装,如何开发java程序等等一下概念行的东西,这里面我都不一一说了. 今天这一章主要想总结一下java中的 ...

  8. python查看各列数据类型_pandas中查看数据类型的几种方式

    在获得数据之后.分析数据之前,我们一般需要对数据总体进行一个概览,如有哪些字段,每个字段的类型,值是否缺失等,以下列出了几种方法,供我们方便快捷的查看dataframe的数据类型. 1.维度查看:df ...

  9. c语言数据类型_C语言基础数据类型

    点击上方"C语言中文社区",选择"设为星标★" 技术干货第一时间送达! 基本类型 基本类型就是我们在使用C语言时最基础的数据类型,包括整形(短整型,基本整型,长 ...

最新文章

  1. 云计算究竟是什么呢?“汇新杯”新兴科技成果专项赛之——云计算
  2. 冒泡排序汇总(整数,实数,字符,字符串)
  3. 【时间管理】从零开始GTD——GTD原则
  4. boost::hana模块实现算术的测试程序
  5. Hadoop运维记录系列(十四)
  6. mysql导入dat文件_MySql导入和抽取大数量级文件数据
  7. Linux 技巧: 从命令行创建像素标尺
  8. 快手内测10分钟长视频 以吸引更多MCN、用户入驻
  9. C# 取电信公网IP并发送邮件
  10. 权威解读 GitHub、Apache 疑云:主流开源软件究竟是否会被闭源?| CSDN 独家
  11. 计算机组成原理——存储器容量扩充
  12. DER论文详解DER: Dynamically Expandable Representation for Class Incremental Learning, CVPR 2021
  13. 海美迪h7四代刷Linux,海美迪H7四代怎么安装第三方软件看电视直播
  14. java+SpringBoot+HTML+Mysq基于微信小程序的大咖读书系统的设计与实现
  15. ISCC部分pwn题解
  16. 一年期早过:Windows 10免费升级漏洞仍然敞开着
  17. 吴恩达Coursera深度学习课程 deeplearning.ai (4-4) 神经风格转换--编程作业
  18. CH55x使用Arduino编程记录(三)Could not get xxx consecutive bytes in internal RAM for area OSEG
  19. IP网络设备的测试方法
  20. 非常不错的点餐系统应用源码完整版

热门文章

  1. shell 中的expect 用法
  2. Go语言中的字符和字符串
  3. 回调函数 EnumFontFamProc
  4. 考研数学一之高数上册学习计划
  5. C语言进阶——使用C语言与gnuplot结合画一些波形
  6. Django之form组件加cookie,session
  7. POJ1088(记忆搜索加dp)
  8. PHP 利用curl 模拟get post 请求
  9. JavaScript使用button提交表单
  10. sql server 小技巧(8) visual studio 2013里使用Sql server compact 4.0及发布问题处理