C ++ 2. basic built-in types: arithmetic types, literals

Foreword

Self "C ++ Primer Fifth Edition," which contains the characteristics of C ++ 11.
C ++ 98 standard used to be, until 2011, the C ++ 11 standard, once every three years the standard, now has C ++ 11, C ++ 14, C ++ 17 ...

Due to himself, to do the main Android development. Develop programs certainly have to consider compatibility. The first two years of the program, at least to be considered compatible with Android 4.x (api level 14 ~ 20) , or even earlier; now compiler Android Studio in the new project, will prompt the minimum api support, compatible to What percentage of the equipment.
Charles would have to go under the official document, which Android version supports cpp11, which version supports cpp14, finding out the results stunned. Andrews ndk document, can now be found, just say, ndk r18b began to support cpp17. While ndk can only be compiled, and android api level specific run-time, there is no requirement for cpp version of this temporarily unclear. Only have the opportunity to high version of the code cpp low version real test whether the normal operation.
In any case, it is now 2019, support cpp11 equipment, will more and more.


What are the basic built-in types

The book said that the basic built-in types, reflecting the capacity of most computer hardware itself possess.
Feeling, just like java basic data types the same. Is plastic, floating point, character.
Basic built-in types, including the types and arithmetic literals


Arithmetic type

Arithmetic type, the type that can be used for the calculation. Plastic, floating point, character can be used to calculate

bool flag = true; //or false. 可用于计算,结果就是 1或0 参与计算
char c = 'c'; //8bit。 占一个字节。
wchar_t w = 'c'; //宽字符型 16bit
char16_t c16 = 'c'; //unicode 字符 16bit
char32_t c32 = 'c'; //unicode 字符 32bit
short s = 77; //16bit
int i = 12222; //16bit
long l = 33333333; //32bit
long long ll = 888888888888; //64bit   //cpp11 add
float f = 88.012345678901; //单精度浮点型 6位有效数字
double d = 88.012345678901; //双精度  10位有效数字
long double ld = 88.012345678901; //扩展精度  10位有效数字

c ++ predetermined type of Bytes longlong> = long> = int> = short

Signed and unsigned types

Bool addition of unicode character and other non-floating type may be unsigned unsigned type.
. eg unsigned int a = 10;
There are three ways declaration:

int a = 10;
signed int a = 10;
unsigned int a = 10;

int a = 10;The default is seen as a symbol. But and signed int a = 10;are not the same type.
Usually we do not tend to add signeda keyword to indicate a signed, too much trouble

Unsigned type is> = 0 value. Even if a negative assignment, will automatically be converted into a> = 0 value.

Arithmetic type data range

Assumed that the number of bytes occupied by each type on the target device, the above code is described in the comments.
With short, unsigned short, for example:
short itself is 16bit;
range short of: 2 8 2 8 1 -2^8 \sim 2^8-1
unsigned Short range: 0 2 16 1 0\sim2^{16}-1

Arithmetic type, has a range of values ​​of the symbols unsigned, and which is occupied by the relevant byte, bit set number N,

  • When signed, the negative range accounts for half of the bit, that is, { x 2 n / 2 x < 0 } \lbrace x| -2^{n/2} \leq x <0 \rbrace ; 0 and positive half of the bit, i.e. { x 0 x 2 n / 2 1 } \ Lbrace x | 0 \ leq x \ leq 2 ^ {n / 2} -1 \ rbrace
  • When unsigned, starting at 0, maximum fill all bit range: { x 0 x 2 N 1 } \ Lbrace x | 0 \ leq x \ leq 2 ^ N -1 \ rbrace

Built-in function sizeof(type), you can obtain the number of bytes occupied by the parameter type. eg.sizeof(unsigned int)

Unsigned type, assign a value of range

unsigned char c0 = 256; //=0
unsigned char c1 = -514; //=254= 2^8-2
unsigned char c2 = 514; //=2

A simple way conversion rule:
char range => [0, 255], when the value is negative, the left side takes a value 0, because there is no left, then from 255 to move forward. Therefore, the value -1 corresponds to 255.
Conversely, when a positive number, the right to take the value 255, because there is no right side, then move rearwardly from 0. Therefore, the value 256 corresponding to 0.

  • When a literal (or absolute) is too large, a modulo operation.
    unsigned char c1 = -514;=> 256-514%256=> 254;

  • Conversely, when negative:
    unsigned char c1 = -514;=> 514%256=> 2;

When calculating the larger type, or the type of conversion, the rule applies.


Literals

It is to see that value. As 30 、'a'such values.
Each literals corresponds to a data type.

Plastic literal

十进制: 100
八进制:076    以数字零开头
十六进制; 0xabc   以0x 或0X 开头

Floating-point literals

eg. 3.14

Scientific notation

Scientific notation: n-th power of a floating point number x 10, denoted by: xen, or xEn;
EG.

3.14e5;  => 3.14*10^5
3.14e-5; => 3.14*10^-5

Character and string literals

eg.

`a`   字符
"abc" 字符串

The actual string is an array of a plurality of characters; compiler automatically adds a null character at the end of the array.
eg.

"abc" =>  ['a', 'b', 'c', '\0'];

Escape Sequences

Backslash. common

\r  回车符
\n  换行
\\   反斜杠
\t  横向制表。 类似横向留空白
\v  纵向制表
\?  问号
\"  双引号
\'  单引号
\0  空字符
\u  unicode字符      eg. const char32_t ccxx = U'\u4e2d'; cout << (U'中' == ccxx') << endl;

Add a prefix or suffix to specify the type of literal

对字符或字符串面值的前缀: u U L u8
cout << "1: " << sizeof('a') << endl; //char
cout << "2: " << sizeof(L'a') << endl; //L:wchar_t;  u:char16_t; U:char32_t;
cout << "3: " << sizeof(u"中国") << endl; //与 char16_t[]
cout << "4: " << sizeof(u8"中国") << endl; //utf8,  与 sizeof("中国")一样,是char[]

对整型或浮点型的后缀: u/U  l/L  ll/LL   f/F  
cout << "5: " << sizeof(100) << endl; //int
cout << "6: " << sizeof(100ULL) << endl; // ll/LL: unsigned long long;  u/U: unsigned; l/L: long
cout << "7: " << sizeof(1E-3) << endl; //double
cout << "8: " << sizeof(1E-3F) << endl; // f/F: float
cout << "9: " << sizeof(3.14) << endl; //double
cout << "10: " << sizeof(3.14L) << endl; // l/L: long double

Boolean literals and literal pointer

  • true or false is a Boolean literals
  • nullptr pointer literal. A pointer that is null
Published 400 original articles · won praise 364 · Views 1.62 million +

Guess you like

Origin blog.csdn.net/jjwwmlp456/article/details/89604022