c++primer 第2章 练习题答案 (尚未完善 陆续补充中 基本上已经完成)

本文详细解析了C++中数据类型如short、int、long long的字节数,强调在32/64位系统下int通常为4个字节。讨论了字符、宽字符、字符串及其区别,并列举了十进制、无符号、长整型等字面量。还探讨了浮点数精度、引用和指针的区别以及初始化规则,包括const引用和指针的使用。并提供了练习题答案,涵盖各种数据类型的字面量、引用、指针、常量指针等概念。
摘要由CSDN通过智能技术生成

short int 2个字节

int 2/4字节

long 4/8字节

long long 8字节

其实这个吧 意义不是太大 基本上现在大多数都32/64位机器 所以int基本上最小就都4个字节了  而且还和操作系统受限有关系 winodws linux 32位 64位 所以啊 就忽略吧  无符号就是都是正数 最高位在二进制里1不代表负 而是代表正的 该是几是几  

 

上面是我的白话 下面是GitHub上 大神给的答案 

C++ guarantees short and int is at least 16 bits, long at least 32 bits, long long at least 64 bits.

The signed can represent positive numbers, negative numbers and zero, while unsigned can only represent numbers no less than zero.

The C and C++ standards do not specify the representation of float, double and long double. It is possible that all three implemented as IEEE double-precision. Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float is indeed a IEEE single-precision floating point number (binary32), and double is a IEEE double-precision floating point number (binary64).

Usage:

Use int for integer arithmetic. short is usually too small and, in practice,long often has the same size as int. If your data values are larger than the minimum guaranteed size of an int, then use long long. (In a word: short < int < long < long long)

Use an unsigned type when you know that the values cannot be negative. (In a word: no negative, unsigned.)

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 usually is unnecessary and often entails considerable run-time cost. (In a word: float < double < long double)

Reference:

What are the criteria for choosing between short / int / long data types?
Difference between float and double
Advice: Deciding which Type to Use(This book.)

c++保证短,int至少是16位,长至少32位,长至少64位。
符号可以表示正数、负数和零,而无符号只能表示不小于零的数字。
C和c++标准没有指定float、double和long double的表示形式。这三种方法都可以实现为IEEE双精度。然而,对于大多数体系结构(gcc、MSVC;x86、x64、ARM) float确实是IEEE单精度浮点数(binary32), double是IEEE双精度浮点数(binary64)。
用法:
整数运算使用整型数。short通常太小,实际上long的大小与int相同,如果您的数据值大于int的最小保证值,那么使用long long。(一句话:短< int < long < long long)
当您知道值不能为负数时,使用无符号类型。(一句话:没有否定,没有符号。)
浮点运算使用双精度浮点运算;浮点数通常没有足够的精度,双精度计算与单精度计算的成本可以忽略不计。事实上,在某些机器上,双精度操作比单精度操作要快。long double提供的精度通常是不必要的,并且通常需要相当大的运行时成本。(一句话:浮动< double < long double)
参考:
选择短/ int /长数据类型的标准是什么?
浮点数和双精度数的区别
建议:决定使用哪一种(这本书)。

上面那个中文翻译是 有道词典的 感谢有道词典 打个广告。。。

2.2 按照上面说的 单精度和双精度的损耗 可以忽略不计

To calculate a mortgage payment, what types would you use for the rate, principal, and payment? Explain why you selected each type.
use double, or also float.
The rate most like that: 4.50 % per year.
The principal most like that: $854.36
The payment most like that: $1,142.36
Reference:
mortgage-calculator
What's in a Mortgage Payment?

要计算抵押贷款付款,您将使用什么类型的利率、本金和付款?解释为什么选择每种类型。
使用double或者float。
最像的是:每年4.50%。
最像这样的本金:854.36美元
最类似的付款方式是:1142.36美元
参考:
抵押计算器
什么是按揭付款?

 

#include<iostream>

using namespace std;

int main(){


        unsigned u = 10,u2=42;
        cout <<u2-u<<endl;
        cout <<u-u2<<endl;
        int i=10,i2=42;

        cout <<i2-i<<endl;
        cout <<i-i2<<endl;
        cout << i -u <<endl;
        cout << u - i <<endl;

        return 0;

}
~        

 

 

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

(a): character literal, wide character literal, string literal, string wide character literal.

(b): decimal, unsigned decimal, long decimal, unsigned long decimal, octal, hexadecimal.

(c): double, float, long double.

(d): decimal, unsigned decimal, double, double.

 

上面参考一下github大神代码

下面是有道翻译后的结果

(a):字符文字,宽字符文字,字符串文字,字符串宽字符文字。

(b):十进制、无符号十进制、长十进制、无符号长十进制、八进制、十六进制。

(c):双,浮动,长双。

(d):十进制,无符号十进制,双,双。

记住下面这个图就好了

如果有的话,下列定义之间的区别是什么?

 

int month = 9, day = 7;

int month = 09, day = 07;

第一行的整数是小数。

第二行:

int month = 09无效,因为八进制没有数字9。

 

2.7

(a): Who goes with Fergus?(new line) "string"

(b): 31.4 "long double"

(c): 1024 "float"

(d): 3.14 "long double"

 

上面参考其他的答案

练习2.5 指出下述字面值的数据类型并说明每一组内几种字面值的区别:

(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
解答:
(a)第一个是单char字符,第二个是单wchar_t宽字符,第三个是单个字符串,在内存中存储时,末尾隐含一个'\0'空字符,第四个是单个wchar_t类型的宽字符串,在内存中存储时,末尾也隐含一个'\0'空字符。
(b)第一个是一个int整型,第二个是无符号整型,第三个是long int型,第四个是unsigned long型,第五个是八进制的int型,最后一个是16进制的int型
(c)第一个是double类型,第二个是float类型,第三个是long double类型
(d)第一个是整型,第二个是无符号整型,第三个是double类型,最后一个是double类型的科学记数法表示。
 

练习2.6 下面两组定义是否有区别,如果有,请叙述之:

int month = 9, day = 7;
int month = 09, day = 07;
解答:有区别,第一行是十进制整型,第二行是八进制整型,但是没有09这一表示。

练习2.7 下述字面值表示何种含义?它们各自的数据类型是什么&#x
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值