C++Primer Section 2-1

2023-10-30

Section 2-1 Primitive Built-in Types

The set of primitive built-in types are arithmetic types and a special type void.

Section 2-1-1 Arithmetic Types

Arithmetic types include characters, integers, boolean values and floating-point numbers.

But it can be divided into two categories—integers types and floating-point types.

Size of different types depends on machines. But the table gives the minimum size of each type.

Type Meaning Minimum Size
bool boolean NA
char character 8 bits
wchar_t wide character 16 bits
char16_t Unicode character 16 bits
char32_t Unicode character 32 bits
short short integer 16 bits
int integer 16 bits
long long integer 32 bits
long long long integer 64 bits
float single-precision floating-point 6 significant digits
double double-precision floating-point 10 significant digits
long double extended-precision floating-point 10 significant digits
Differences in Integer Types

Integer types include bool,char,wchar_t,char16_t,char32_t,short,int,long,long long.

Differences between char, wchar_t, char16_t and char32_t

char is guaranteed to be big enough to hold the basic set of characters in a machine, while wchar_t is guaranteed to be big enough to hold any character in the machine’s largest extended character set.

char16_t and char32_t are intended for Unicode characters.

Note

Sometimes the compiler will replace char by wchar_t or char16_t, char32_t.

Differences between short, int, long and long long

These four types are all intended for integers. The only difference between them is the size of them and the range of the number that they can represent.

But in different machines, those sizes and ranges change. But basically, we have the following rule:

sizeof(short) `sizeof(int)` sizeof(long) sizeof(long long)

Differences between in Floating-point Types
Differences between float, double and long double

These three types are all intended for floating-point number.

They have different sizes, precisions and purposes. Usually, float are represented in 1 word(32 bits). double in two words(64 bits) and long double in 3 or 4 words(96 or 128 bits).

The float and double types typically yield about 7 and 16 significant digits, respectively.

The type long double is often used as a way to accommodate special-purpose floating-point hardware.

Signed and Unsigned Types

Except for bool and the extended character types,, the integral types may be signed or unsigned.

A signed value can represent both negative an positive numbers by using special method. But an unsigned value can represent only 0 and positive numbers. and all bits in it represent the value.

int,short,long, long long are all signed types. To get corresponding unsigned types, we add unsigned to the type like unsigned long long.

unsigned int can be abbreviated as unsigned.

For characters, it is a little tricky since there will be three types char, singed char and unsigned char. But there are only two ways of representation signed and unsigned. Which type of representation char will take depends on the compiler.

Some people say that unsigned types are used to precisely control the machine-level or circuit-level operations instead of to enlarge the range of numbers that can be represented.

Good Practices for Using Arithmetic Types
  • Use an unsigned type when you know that the values cannot be negative
  • Use int for integer arithmetic. short is usually too small and , in practice, long often has the same size as int. If your number is too large, use long long.
  • Do not use plain char or bool in arithmetic expressions. Use them only to hold characters or truth values. Computations using char are especially problematic because char is signed on some machines and unsigned on others.
  • Use double for floating-point computations; float usually does not have enough precision, and the cost of double-precision calculations versus single-precision is negligible. In fact, on some machines, double-precision operations are faster than single. The precision offered by long double is usually unnecessary and often entails considerable run-time cost.
Exercises
Exercise 2.1

What are the differences between int, long, long long and short? Between an unsigned and signed type? Between a float and double?

Solution

In part Differences in Integer Types and Differences in Floating-point Types

Exercise 2.2

To calculate a mortgage payment, what types would you use for the rate, principle, and payment? Explain why you selected each type.

Solution

All shall be double. All of them may be floating-point number and for floating-point computations we use double.

Section 2-1-2 Type Conversions In Assignment Statements

Type conversions take place wherever we expected one type of value while given another related type of value.

Basic Rules for Type Conversions in Assignment Statements
  • Assigning a nonbool arithmetic type to a bool type:

    result is false if the value is 0 and true otherwise

  • Assigning a bool to another arithmetic type of value:

    the result is 0 if the bool is false and 1 if the bool is true

  • Assigning a floating-point value to an object of integral type:

    The value will be truncated.

  • Assigning an integral value to an object of floating-point type

    Fraction part is 0. Precision may be lost if the integer has more bits than floating-point object can accommodate.

  • Assigning an out-of-range value to an object of unsigned type

    The result is the remainder of the value modulo the number of values the target type can hold. For example -1 to unsigned char will be 255

  • Assigning an out-of-range value to an object of signed type, the result is undefined.

Advice on how to use Type Conversions: Avoid undefined and implementation-Defined Behavior

Undefined behavior results from errors that the compiler is not required or able to detect, like the overflow of signed type. But this depends on the compiler, input and so on. So sometimes the program may work under a particular circumstance but fails under another.

Implementation-defined Behavior is about the portability of the program, meaning that when a program is moved from one platform to another, whether it can work as well as before. It has something to do with the features that vary between different platforms like the size of int.

A Typical Use of Type Conversion—Integer in Condition
int i = 0;

do something;

if (i) {
    do something;
}

In the block of code above. we have that the condition will evaluate as true is i0 . And the condition will evaluate as false if i=0 .

But it is worth noting that using bool in an arithmetic expression is highly NOT recommended.

Expressions Involving Unsigned Types
Arithmetic Expressions

If we use both an int and unsigned value in an arithmetic expression, the int will be converted to unsigned even if it is negative.

unsigned u = 10;
int i = -42;
std::cout << i + i << std::endl; // prints -84
std::cout << i + u << std::endl; // if 32-bit ints, prints 4294967264

So it is also important not to make the result of subtraction of unsigned negative.

unsigned u1 = 42, u2 = 10;
std::cout << u1 - u2 << std::endl;  // result is 32
std::cout << u2 - u1 << std::endl;  // result is -32 % MAX
Unsigned Type in Loop Condition
for (unsigned i = 10; i >= 0; i--) {    // intended to execute for 11 times but fails
    do something;
}

This loop shall never terminate because i is unsigned and can never fail the condition i0. But we can still do the following to compensate for it.

for (unsigned i = 11; i > 0; i++) {     // intended to execute for 11 times and succeed
    do something;
}
Tips On Using unsigned and signed

Do NOT mixed signed and unsigned Types!

signed value will be automatically converted to unsigned value. So mixing signed and unsigned type will yield surprising and even unpredictable results, especially when signed value is negative.

Exercise 2.3

What output will the following code produce?

unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl;
std::cout << u - u2 << std::endl;

int i = 10, i2 = 42;
std::cout << i2 - i << std::endl;
std::cout << i - i2 << std::endl;

std::cout << i - u << std::endl;
std::cout << u - i << std::endl;
Solution

The output should be

32

-32 % MAX

32

-32

0

0

MAX stands for the max number of unsigned + 1.

Exercise 2.4

Write a program to check whether your predictions were correct. If not, study this section until you understand what the problem is.

Solution

The solution to 2.3 is right. And 32 % MAX is 4294967264.

Section 2-1-3 Literals

A literal refers to a value like 42 whose value is self-evident. Every literal has a type. The form and value will determine its type.

And we have three tables for the form of literal and the type of literals.

Table 1: Character and Character String Literals
Prefix Meaning Type
u Unicode 16 character char16_t
U Unicode 32 character char32_t
L wide character wchar_t
u8 utf-8(string literals only) char
Table 2: Integer Literals
Suffix Minimum Type
u or U unsigned
l or L long
ll or LL long long
Table 3: Floating-Point Literals
Suffix Type
f or F float
l or L long double

Actually, there is no negative literals because although the literal -42 seems a negative literal, it is actually composed of an operator - and a positive literal 42.

Integer and Floating-Point Literals
Integer Literals

We can write integer literals using decimal, octal or hexadecimal notation.

  • Integer literals that begin with 0 are interpreted as octal
  • Those that begin with either 0x or 0X are interpreted as hexadecimal
  • Decimal is decimal

For example

12      // decimal
012     // octal
0x14    // hexadecimal
How to determine integer literals’ type
  • A decimal literal has the smallest type of int, long, long long in which the literal’s value fits.
  • A octal or hexadecimal literal has the smallest type of int, unsigned, long, unsigned long, long long, unsigned long long in which the literal’s values fits.
  • No integer type has the type short
  • Those with suffix u or U can only be unsigned type which starts from unsigned to unsigned long long.
  • Those with suffix l, L, ll or LL has a minimum type as in the Table 2.
Tips on using suffix of integer literals

Use L instead of l which can be easily mistaken for 1.

Floating-Point Literals

We can write floating-point literals in two forms—normal notation with a decimal point or scientific notation with an exponent indicated by e or E.

For example

3.1415926    0.0     0e0     3.1E-4     0.    .1
How to determine a floating-point literal’s type
  • By default, they have the type double.
  • Every floating-point literal with suffix have its type determined by suffix as in Table 3.
Character and Character String Literals
Character Literals

A character enclosed within single quotes is a character literal.

How to determine a character literal’s type
  • By default, it has the type char.
  • Every character literal with prefix have its type determined by prefix as in Table 1.
Character String Literals

Zero or more characters enclosed within double quotation marks is a string literal.

  • The size of a string literal is one more than its apparent size with additional invisible character '\0' at the end of the string literal.

  • 'A' is different from "A".

  • Two string literals that appear adjacent to one another and that are separated only by spaces, tabs, or newlines are concatenated into one literal.

    For example

    std::cout << "a really, really long string literal "
            "taht spans two lines" << std::endl;
Escape Sequence

Two kinds of characters are in need of escape sequence:

  1. nonprintable characters

  2. characters with special purpose in language.

    For example

    // nonprintable
    \n       // newline
    \t       // horizontal tab
    \a       // alert(bell)
    \b       // backspace     
    // special purpose in language
    \\       // backslash
    \"       // double quote
    \'       // single quote    

In code

std::cout << '\n';  // a newline
std::cout << "\tHi!\n";     // a tab followed by Hi! followed by a newline
Generalized Escape Sequence

We can use generalized escape sequence, which is \x followed by one or more hexadecimal digits or a \ followed by one, two or three octal digits. The value represents the numerical value(usually ASCLL) of the character.

For example

\7      // bell
\12     // newline
\40     // blank
\0      // null
\115    // 'M'
\x4d    // 'M'    

In code

std::cout << "Hi \x4dO\115!\n";     // prints    Hi MOM! followed by a newline
std::cout << '\115' << '\n';        // prints    M followed by a newline
Boolean and Pointer Literals
Boolean Literals

true and false

Pointer Literals

nullptr

Exercises
Exercise 2.5

Determine the type of each of the following literals. Explain the differences among the literals in each of the four examples:

(a). 'a', L'a', "a", L"a"

(b). 10, 10u, 10L, 10uL, 012, 0xC

(c). 3.14, 3.14f, 3.14L

(d). 10, 10u, 10., 10e-2

Solution

(a) The first two are character literals and the last two are character string literals with two characters.

'a'     // type char
L'a'    // type wchar_t
"a"     // type const char*
L"a"    // type const wchar_t *    

(b) The first four are decimal literals and 012 is octal literal and 0xC is hexadecimal literal.

10      // int
10u     // unsigned int
10L     // long
10uL    // unsigned long    
012     // int
0xC     // int    

(c) They are all floating-point literals

3.14    // double
3.14f   // float
3.14L   // long double   

(d) The first two are integer literals and the last two are floating-point literals

10      // int
10u     // unsigned
10.     // double
10e-2   // double    
***Exercise 2.6

What, if any, are the differences between the following definitions:

int month = 9, day = 7;
int month = 09, day = 07;
Solution

The 09 seems an octal literal but there ought to be no 9 in octal literal. And 07 , though it is an octal literal, it does not make any difference. I was a little confused so I wrote a program and executed it.

The result is

2-6.cpp:5:14: error: invalid digit “9” in octal constant
int month = 09;
^

So this kind of expression is actually illegal.

Exercise 2.7

What values do these literals represent? What type does each have?

(a). "Who goes with F\145rgus?\012"

(b). 3.14e1L

(c). 1024f

(d). 3.14L

Solution

(a). Who goes with Fergus? followed by a newline. const char * type

(b). 3.14 long double type

(c). 1024 float type

(d). 3.14 long double type

Exercise 2.8

Using escape sequences, write a program to print 2M followed by a newline. Modify the program to print 2, then a tab, then an M, followed by a newline.

Solution
#include<iostream>

int main()
{
    std::cout << "\62\115\12";
    std::cout << "\62\t\115\12";

    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++Primer Section 2-1 的相关文章

  • C/C++ stat()函数:获取文件状态

    相关函数 fstat lstat chmod chown readlink utime 头文件 include
  • 使用R语言绘制指数分布密度函数数据的可视化

    使用R语言绘制指数分布密度函数数据的可视化 指数分布是概率论和统计学中常见的连续概率分布之一 广泛应用于可靠性工程 风险分析和排队论等领域 在本文中 我们将使用R语言的plot函数来可视化指数分布密度函数的数据 首先 我们需要安装并加载R语
  • Pytorch并行计算(二): DistributedDataParallel介绍

    PyTorch并行计算 DistributedDataParallel 一 为什么要并行计算 二 基本概念 三 DistributedDataParallel的使用 1 torch distributed 2 torch multiproc
  • 三、Express

    目录 初识express Express 简介 Express 的基本使用 安装 基本使用 托管静态资源 express static 托管多个静态资源目录 挂载路径前缀 nodemon Express路由 路由的基本使用 为路由模块添加前
  • 使用容器搭建伪redis集群

    在一个主机上使用容器技术搭建一个redis集群 为什么说是伪集群 因为redis集群和分布式相互交叉 因为成本 在一台主机上部署一个三主三从的redis集群 redis版本 v 6 2 6 部署 运行六个节点 docker compose
  • 常见垃圾回收器

    CMS和G1是最重要的 新生代一般采用标记复制 老年代一般采用标记整理算法 Serial 垃圾回收线程只有一个 而且垃圾回收线程工作的时候其他用户线程要停下来 Parnew Serial的多线程版本 有多个垃圾回收线程 垃圾回收线程工作的时
  • 黎明觉醒火种测试服务器维护,黎明觉醒火种测试什么时候上线 黎明觉醒火种测试资格获取方式(图文)...

    黎明觉醒是腾讯旗下的多人开放世界生存手游 对标的就是网易旗下的明日之后 在之前的曙光测试之后 这款游戏长时间来都没有传出过新消息 下面game234就来介绍一下黎明觉醒最新的火种测试什么时候上线 怎么预约 game234将第一时间提供黎明觉
  • 区块链是怎么形成的,你究竟明白多少?

    区块链到底是啥 首先 不要把区块链想的很复杂 其实 区块链很简单 它本质上就是一套数据库存储系统 该系统分布在全球各地 并且能够协同运转 不过 与其他数据库存储系统不一样的是 这个系统的运行者可以是任何有能力架设服务器的人 过去 传统的数据
  • angular 12+NG-ZORRO -UI中使用Modal对话框时注意

    弹框的代码不能放在循环中不然就会出现黑屏了 当时我的代码是这样写的 当然这是我的错误写法 特此记录 div class pages div
  • 日志服务器搭建

    1 安装完系统后 配置网络 设置静态IP vi etc sysconfig network scripts ifcfg ens33 编辑模式下修改 i BOOTPROTO static 改为静态 ONBOOT YES IPADDR 192
  • DeFi新篇章

    随着原生去中心化中央限价订单簿 Central Limit Order Book CLOB DeepBook的推出 Sui上的DeFi开启了新篇章 DeepBook由一群Sui贡献者共同构建 为新一代DeFi应用提供了一个稳定的流动性层 通
  • win10无法访问smb共享文件夹的解决办法

    win10无法访问smb共享文件夹的解决办法 之前在linux的几个图形化界面都可以在文件夹中输入 smb ip share 直接访问Linux服务器上的共享文件夹 但是在win10上进行同样的操作会让我打开win10商店搜索应用程序 网上
  • java(条件分支语句)

    Java中的条件分支语句分两种 if else语句和switch语句 1 if 条件判断语句 代码A 当条件成立时执行代码A 如果条件不成立则不执行代码A 而是直接 执行if的下一句 if 条件 代码块1 else 代码块2 当条件成立时执
  • vscode git 源代码管理 无法自动更新显示变更

    最近vscode 远程写代码遇到问题 git的源代码管理不能自动罗列被修改的文件 原因 早期出现警告 Visual Studio Code is unable to watch for file changes in this large
  • 蓝牙HID说明

    蓝牙HID说明 本章主要围绕BLE的HOGH进行说明 网上很多文档讲到HID都要说到USB的HID 让初学者一开始既要看理解蓝牙GATT Service的概念 又要去理解USB的端点概念 实话来说本人刚去学习时也经常需要尝试去理解这两者的关
  • hystrix详述(2)- 配置

    一 hystrix在生产中的建议 1 保持timeout的默认值 1000ms 除非需要修改 其实通常会修改 2 保持threadpool的的线程数为10个 除非需要更多 3 依赖标准的报警和监控系统来捕获问题 4 通过dashboards
  • 快应用-华为市场快应用审核总是不通过,无法复现华为审核时的bug【经验贴】

    最近完成了一个快应用项目 在提交各个市场审核的时候 除了华为市场总是不过 其他市场 vivo oppo 小米 等 都很快通过了审核 最让人恼火的是 华为反馈的bug内容 我们尝试各种方法都无法复现 无法复现bug就很难定位修改 修改bug全
  • 微信小程序 一键授权 给第三方平台代开发管理(二,一键授权给第三方平台)

    不是重点 可以忽略 本人 七月的胜利 代表七月份我出生啦 嘻嘻 博客就是平常记录一些常用到的开发常用到的技术 方法等 看见好东西了就自己整理一下防止以后自己遇到了再找不到 如果有幸帮到你 欢迎点赞 评论 留言 Thank you 一 创建第
  • linux设置交换内存

    查看是否有交换空间 cat proc swaps free h 创建swapfile空间 sudo fallocate l 32G swapfile ls lh swapfile 设置空间权限 sudo chmod 600 swapfile

随机推荐