(3) Dearths from zero single row "C + + Primer" Important basic types

Source: Internet
Author: User

From zero single row "C + + Primer"

--(3) Important basic types

arithmetic type (arithmetic Types)
C + + arithmetic types
Type meaning Minimum Size
bool Boolean NA
Char Character 8 bits
wchar_t Wide character + Bits
char16_t Unicode character + Bits
char32_t Unicode character Bits
Short Short integer + Bits
Int Integer + Bits
Long Long integer Bits
Long Long Long integer + Bits
Float Single-precision floating-point 6 significant digits
Double Double-precision floating-point Ten significant digits
Long double Extended-precision floating-point Ten significant digits


wchar_t is used to ensure that there is enough size to save the extended character set. char16_t and char32_t language Unicode character sets.

In general, the short type is half a machine word (word) long, the int type is a machine length, and the long type is one or two machine words (in a 32-bit machine, the length of the long and int types are usually the same).

Generally, the float type is a machine Zichang, and the double type is two machine Zichang. Long double type is three or four machine word length. Float can represent 7 significant digits, and double can represent 16 significant digits.

Except for the bool and extended character types (wchar_t), the intergral type (Int,short,long,long long) can be signed and unsigned. Unsigned is represented by the unsigned type. For example, unsigned int.


Type Usage Recommendations:

1) If the value cannot be negative, use the unsigned type.

2) Use int or long long (if the range represented by int is exceeded) for shaping calculations.

3) do not use plain char or bool for arithmetic operations. Use them only to save values. Depending on the compiler, the plain char may be signed or unsigned. Char has three types (plain) char,signed char,unsigned Char.

4) In general, use double to calculate floating-point numbers.

Ask :

What's the difference between Int,long,long Long,short? What's the difference between unsigned,signed? What is the difference between a float and a double?

For:

The short type is a half-machine word (word) long, the int type is a machine-length, and the long type is one or two machine words (in a 32-bit machine, the length of the long type and the int type are usually the same).

Unsigned is an unsigned number and the signed is a signed number. Indicates a different range.

Float type is a machine Zichang, double type is two machine Zichang. The valid data represented is different. Float is 6 bits and double is 10 bits.

Ask:

What type would you use in order to calculate the payment, interest, principal and payment amount of the freight forwarder?

Answer: Use double.


type conversion When assigning a value

BOOL B = 42; B is True

int i = b; I has value 1

i = 3.14; I has value 3

Double pi = i;//PI has value 3.0

unsigned char c =-1; Assunming 8-bit Chars,c has value 255

Signed char c2 = 256; Assuming 8-bit chars,the value of C2 is undefined


1) If you assign a value to bool, the bit is true if the assignment is 0,bool to flase.

2) when BOOL is assigned to other arithmetic types, true is 1,false 0.

3) When assigning a floating-point type to the integral, the part that precedes the decimal point is saved.

4) If the integral type part is assigned to a floating-point type, the fractional part is 0.

5) When we assign an unsigned type a value that exceeds the range it represents, the result is the remainder of the value that is modeled for the total number of numeric values that the unsigned type can represent. For example,a 8 -bit size unsigned char can represent 0 to 255, with a total number of zeros. So The result of assigning 1 to it is 255. (In fact, 1 is stored in memory in the complement of 11111111.) When it is copied to an unsigned type, no changes are made, but the values are read in different ways depending on the type. is unsigned, all bits are numeric, exactly 255)

6) when we assign a value to a signed type that exceeds the range it represents, the result is undefined.


unsigned type operation

When arithmetic is performed using unsigned and int, int is automatically converted to unsigned.

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

Whether it is one or two unsigned numbers, subtract to ensure that the result is greater than 0.

unsigned = u= 10;std::cout<<u1-u2 <<std::endl;//ok result is 32std::cout<<u2-21 <<std::end L OK but the result would wrap around

Wrong:u can never is less than 0;for (unsigned u = Ten; u>=0;--u)    std::cout<< u << std::endl;

Therefore, it is best not to mix unsigned numbers with signed numbers.

Job: P38

Exercise 2.3: #include <iostream>int main () {unsigned u = ten, U2 = 42;std::cout<<u2-u <<std::endl;  32STD::COUT<<U-U2 <<std::endl;  <span style= "font-family:arial, Helvetica, Sans-serif;" >4294967264</span>int i = ten, i2 =;            std::cout<< i2-i <<std::endl; 32std::cout<< i-i2<<std::endl;  -32std::cout<< i-u <<std::endl;  0std::cout << u-i<<std::endl;  0return 0;}


Literal value

Like 42, it is the literal value, each literal has a type.

Integer and floating-point literals

Integer

You can use decimal, octal, and hexadecimal notation.

     */* decimal   */024/   * octal      */0x14/* hexadecimal */

floating point type:

3.14159 3.14159E0 0. 0e0.001

Character and string literals:

' A '//character literal "Hello world!" string literal
The compiler appends a null character ('% ') after each string literal.

The string can be connected as follows:


Escape characters:

1)

NewLine \ horizontal tab \ t alert (Bell) \a vertical tab \v backspace \b Double quote \ "

backslash \ question mark \? Single quote \ ' carriage return \ r formfeed \f

2)

Numeric representation

\12 (newline) \40 (blank) \x4d (' M ')

\ followed by a maximum of 3 8 binary digits, otherwise it will be split. such as \1234 will be divided into \123 and \4


Literal types can override

Character and Character String literals
Prefix Meaning Type
U Unicode character char16_t
U Unicode character char32_t
L Wide character wchar_t
U8 Utf-8 (string literals only) Char

Integer literals
Suffix Minimum Type
U or U Unsigned
L or L Long
ll or LL Long Long

Floating-point literals
Suffix Type
F or F Float
L or L Long double

Bool Literals

True,false


(3) Dearths from zero single row "C + + Primer" Important basic types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.