《C++ primer 5》 chapter 2.1

note

C++ defines a set of primitive types that include thearithmetic types and a special type named void

The arithmetic types are divided into two categories:integral types (which include character and boolean types) and floating-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=1
int i=b; //i=1
i=3.14; //i=3
double 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 valuemodulo the 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)

'a' character
L'a' wide character literal, type is wchar_t
"a" string
L"a" wide string literal, type is wchar_t

(b)
10 integer literal
10u unsigned integer literal
10L integer literal, type is long
10uL unsigned integer literal, type is unsigned long
012 octal integer literal
0xC hexadecimal integer literal

(c)
3.14 double literal
3.14f floating-point literal, type is float
3.14L floating-point literal, type is long double

(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

store zero in the memory


exercise 2.7

exercise 2.8







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值