《C++ primer 5》 chapter 2.1
来源:互联网 发布:java怎么判断文件大小 编辑:程序博客网 时间:2024/06/03 19:30
note
C++ defines a set of primitive types that include thearithmetic types and aspecial type named void.
The arithmetic types are divided into two categories:integral types (which include character and boolean types) andfloating-point types.
Test int in computer
a char is the same size as a single machine byte.
A char is guaranteed to be big enough to hold numeric values corresponding to the characters in the machine’s basic character set.
The wchar_t type is guaranteed to be large enough to hold any character in the machine’s largest extended character set.
The types char16_t and char32_t are intended for Unicode characters. (Unicode is a standard for representing characters used in essentially any natural language.)
Unlike the other integer types, there are three distinct basic character types: char, signed char, and unsigned char.
In particular, char is not the same type as signed char.Which of the other two character representations is equivalent to char depends on the compiler.
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.
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.
bool b=42; //b=1int i=b; //i=1i=3.14; //i=3double pi = i;//pi=3.0 unsigned char c = -1; // assuming 8-bit chars, c has value 255 signed char c2 = 256; // assuming 8-bit chars, the value of c2 is undefined
If we assign an out-of-range value to an object of unsigned type, the result is the remainder of the valuemodulothe number of values the target type can hold.
For example, an 8-bit unsigned char can hold values from 0 through 255, inclusive. If we assign a value outside this range, the compiler assigns the remainder of that value modulo 256. Therefore, assigning –1 to an 8-bit unsigned char gives that object the value 255.
If we assign an out-of-range value to an object of signed type, the result is undefined. The program might appear to work, it might crash, or it might produce garbage values.
Avoid Undefined and Implementation-Defined Behavior
Programs that contain undefined behavior can appear to execute correctly in some circumstances and/or on some compilers. There is no guarantee that the same program, compiled under a different compiler or even a subsequent release of the same compiler, will continue to run correctly.
Nor is there any guarantee that what works with one set of inputs will work with another. programs usually should avoid implementation-defined behavior, such as assuming that the size of an int is a fixed and known value. Such programs are said to be nonportable. When the program is moved to another machine, code that relied on implementation-defined behavior may fail.
We can write an integer literal using decimal, octal, or hexadecimal notation. Integer literals that begin with 0 (zero) are interpreted as octal. Those that begin with either 0x or 0X are interpreted as hexadecimal.
The value of a decimal literal is never a negative number.
If we write what appears to be a negative decimal literal, for example, -42, the minus sign is not part of the literal. The minus sign is an operator that negates the value of its (literal) operand.
Floating-point literals include either a decimal point or an exponent specified using scientific notation.
Using scientific notation, the exponent is indicated by either E or e
The compiler appends a null character (’\0’) to every string literal.
For example, the literal 'A' represents the single character A, whereas the string literal "A" represents an array of two characters, the letter A and the null character.
Some characters, such as backspace or control characters, have no visible image. Such characters arenonprintable.
Other characters (single and double quotation marks, question mark, and backslash) have special meaning in the language.
Our programs cannot use any of these characters directly. Instead, we use an escape sequence to represent such characters.
An escape sequence begins with a backslash.
We use an escape sequence as if it were a single character:
If a \ is followed by more than three octal digits, only the first three are associated with the \.
For example, "\1234" represents two characters: the character represented by the octal value 123 and the character 4.
In contrast, \x uses up all the hex digits following it;
"\x1234" represents a single, 16-bit character composed from the bits corresponding to these four hexadecimal digits.
exercise
exercise 2.1
#include<iostream>//#include"Sales_item.h"int main(){std::cout<<sizeof(int)<<std::endl;std::cout<<sizeof(long)<<std::endl;std::cout<<sizeof(long long)<<std::endl;std::cout<<sizeof(short)<<std::endl;std::cout<<sizeof(unsigned int)<<std::endl;std::cout<<sizeof(signed int)<<std::endl;std::cout<<sizeof(char)<<std::endl;std::cout<<sizeof(unsigned char)<<std::endl;std::cout<<sizeof(signed char)<<std::endl;std::cout<<sizeof(float)<<std::endl;std::cout<<sizeof(double)<<std::endl;system("pause");return 0;}
Int will be at least as large as short.
A long will be at least as large as int.
a long long will be at least as large as long.
Except for bool and extended character types, the integral types may be signed or unsigned.
A signed type represents negative or positive numbers. An unsigned type represents only values greater than or equal to zero.
Typically, floats are represented in one word. Doubles are represented in two words.
exercise 2.2
rate:double
principle: double
payment:double
exercise 2.3
#include<iostream>//#include"Sales_item.h"int main(){unsigned u1=10,u2=4;std::cout<<u2-u1<<std::endl;std::cout<<u1-u2<<std::endl;int i1=10,i2=42;std::cout<<i2-i1<<std::endl;std::cout<<i1-i2<<std::endl;std::cout<<i1-u1<<std::endl;std::cout<<u1-i1<<std::endl;system("pause");return 0;}4294967290
6
32
-32
0
0
exercise 2.4
exercise 2.5
(a)
(d)
10 integer literal
10u unsigned integer literal
10. floating-point literal, type is double
10e-2 floating-point literal, type is double
exercise 2.6
exercise 2.7
exercise 2.8
- 《C++ primer 5》 chapter 2.1
- C++Primer-Function chapter
- C primer plus -- Chapter 2
- [primer]Chapter-5 Expressions
- C++ Primer Chapter 5
- 《C++ primer 5》 chapter 1.2
- 《C++ primer 5》 chapter 1.3
- 《C++ primer 5》 chapter 1.4
- 《C++ primer 5》 chapter 2.2
- C++ Primer Chapter 10-5
- C++ Primer Chapter 13-5
- C++ Primer Chapter 14-5
- C++ Primer Chapter 15-5
- c primer plus Chapter 14 p383
- 《C primer plus》笔记--Chapter 1
- C++Primer Chapter One学习笔记
- 加减乘除计算器——C primer plus 5edith chapter 8
- C++ Primer 5e chapter 5
- cocos2d-x常用工具
- urlEncode
- Ruby基础知识-times , upto , downto , each ,step
- 【Android】资源id(0xPPTTSSSS): PackageId+TypeId+SeqNum
- 80端口被屏蔽的解决方法,及外网访问内网P2P穿透软件实现
- 《C++ primer 5》 chapter 2.1
- 发现个好东西啊,计算圆周率
- mac上安装node
- Ruby基础知识-求50以内的素数-改良版
- IOS TableView<一>
- 1018. 锤子剪刀布 (20)
- POJ1716 Integer Intervals
- AutoCAD的版本与出版时间
- Tokyo Tyrant(TTServer)系列(三)-Memcache协议