第一看变量”部分的材料上。

本文介绍了C++中的基本数据类型,包括整数类型以及其他常见类型。详细解释了计算机如何通过内存地址来存储和读取不同数据类型的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一课直接在“1.3 -第一看变量”部分的材料上。

zhèzhíjiēzài 1 . 3 -kànbiànliàng fendecáiliàoshàng

In the previous lesson on variables, we talked about the fact that variables are names for a piece of memory that can be used to store information. To recap briefly, computers have random access memory (RAM) that is available for programs to use. When a variable is defined, a piece of that memory is set aside for that variable.

在上一节课上,我们谈到了一个事实,即变量是一段记忆的名字,可以用来存储信息。简要概括起来,计算机的随机存取存储器(RAM),可供程序使用。当一个变量被定义时,一块内存被放置到该变量的旁边。

zàishàngjiéshàng mentándàoleshìshí biànliàngshìduàndemíngzi yòngláicúnchǔxìn jiǎnyàogàikuòlái suàndesuícúncúnchǔ R A M gōngchéngshǐ使yòng dāngbiànliàngbèidìngshí kuàinèicúnbèifàngzhìdàogāibiànliàngdepángbiān

The smallest unit of memory is a binary digit (bit), which can hold a value of 0 or 1. You can think of a bit as being like a traditional light switch -- either the light is off (0), or it is on (1). There is no in-between. If you were to look at a random segment of memory, all you would see is …011010100101010… or some combination thereof. Memory is organized into sequential units called addresses. Similar to how a street address can be used to find a given house on a street, the memory address allows us to find and access the contents of memory at a particular location. Perhaps surprisingly, in modern computers, each bit does not get its own address. The smallest addressable unit of memory is a group of 8 bits known as a byte.

最小单位的内存是一个二进制数字(位),它可以容纳一个值为1或0。你能想到的有点像一个传统的光开关-无论是光是关闭(0),或它是在(1)。没有在之间。如果你想看一段随机的记忆,你会看到的是……011010100101010……或者是一些组合。内存被组织成顺序的单位称为地址。类似于街道地址可以用来在街道上找到一个给定的房子,内存地址让我们找到和访问的内容,在一个特定的位置。也许令人惊讶的是,在现代的计算机中,每一位都没有得到它自己的地址。最小的可寻址存储器单元是一组称为字节的8位。

zuìxiǎodānwèidenèicúnshìèrjìnzhìshù wèi róngzhíwéi 1huò 0 néngxiǎngdàodeyǒudiǎnxiàngchuántǒngdeguāngkāiguān -lùnshìguāngshìguān 0 huòshìzài 1 méiyǒuzàizhījiān guǒxiǎngkànduànsuíde huìkàndàodeshì 0 1 1 0 1 0 1 0 0 1 0 1 0 1 0 huòzhěshìxiē nèicúnbèizhīchéngshùndedānwèichēngwéizhǐ lèijiēdàozhǐyòngláizàijiēdàoshàngzhǎodàogěidìngdefángzi nèicúnzhǐràngmenzhǎodàofǎng访wèndenèiróng zàidìngdewèizhi lìngrénjīngdeshì zàixiàndàidesuànzhōng měiwèidōuméiyǒudàodezhǐ zuìxiǎodexúnzhǐcúnchǔdānyuánshìchēngwéijiéde 8wèi

The following picture shows some sequential memory addresses, along with the corresponding byte of data:

下图显示了一些顺序的内存地址,以及相应的数据字节:

1
2
3
4
5
bool bValue;
char chValue;
int nValue;
float fValue;
double dValue;

因为计算机上的所有数据只是一个位序列,我们使用数据类型来告诉我们一些有意义的方式来解释内存的内容。你已经看到了数据类型的一个例子:整数。当我们将一个变量作为一个整型变量时,我们告诉编译器:这个变量的地址将被解释为一个整数,这是一个整型变量。

yīnwèisuànshàngdesuǒyǒushùzhǐshìwèiliè menshǐ使yòngshùlèixíngláigàomenxiēyǒudefāngshìláijiěshìnèicúndenèiróng jīngkàndàoleshùlèixíngdezi zhěngshù dāngmenjiāngbiànliàngzuòwéizhěngxíngbiànliàngshí mengàobiān zhègebiànliàngdezhǐjiāngbèijiěshìwéizhěngshù zhèshìzhěngxíngbiànliàng

When you assign a value to a data type, the compiler and CPU take care of the details of encoding your value into the appropriate sequence of bits for that data type. When you ask for your value back, your number is “reconstituted” from the sequence of bits in memory.

当你将一个值赋给一个数据类型时,编译器和处理器会把你的值编码为该数据类型的适当的位序列的细节。当你要求你的价值回归,你的数字是“重组”从内存中的位序列。

dāngjiāngzhígěishùlèixíngshí biānchǔhuìdezhíbiānwéigāishùlèixíngdeshìdàngdewèilièdejié dāngyāoqiúdejiàzhíhuíguī deshùshì chóng cóngnèicúnzhōngdewèiliè

There are many other data types in C++ besides the integer, most of which we will cover shortly. As shorthand, we typically refer to a variable’s “data type” as its “type”.

还有很多其他的数据类型的C++除了整数,其中我们将不久。作为速记,我们通常将变量的“数据类型”称为“类型”。

háiyǒuhěnduōdeshùlèixíngde C + +chúlezhěngshù zhōngmenjiāngjiǔ zuòwéi mentōngchángjiāngbiànliàngde shùlèixíng chēngwéi lèixíng

Fundamental data types

基本数据类型

běnshùlèixíng

C++ comes with built-in support for certain data types. These are called fundamental data types (in the C++ specification), but are often informally called basic types, primitive types, or built-in types.

C++有一定的数据类型的内置支持。这些被称为基本数据类型(在C++规范),但往往是非正式地称为基本类型,原始类型或内置类型。

C + +yǒudìngdeshùlèixíngdenèizhìzhīchí zhèxiēbèichēngwéiběnshùlèixíng zài C + +guīfàn dànwǎngwǎngshìfēizhèngshìdechēngwéiběnlèixíng yuánshǐlèixínghuònèizhìlèixíng

Here is a list of the fundamental data types, some of which you have already seen:

下面是一个基本数据类型的列表,其中一些你已经看到:

1
2
3
4
5
6
7
int a, double b; // wrong (compiler error)
 
int a; double b; // correct (but not recommended)
 
// correct and recommended (easier to read)
int a;
double b;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值