C + + learning (vi) Getting started--Identifying constant types

Source: Internet
Author: User
Tags setf

The program declaration tells the C + + compiler The type of an integer variable, but how does the compiler know about the constant type?

Unless there is a reason (such as using a special suffix, or if the value is too large to be stored as an int), it will be stored as an int type

Suffix is the letter placed after the numeric constant

L, l means long constant

U, u represents unsigned int

UL (UL) or LU (LU) represents unsigned int

ll, LL is a long long

Ull, Ull, ull, ull for unsigned long long

For a decimal integer without a suffix, it is represented by the smallest type that can store the number: int, long, or long long.

For 86 or 16 binary numbers without suffixes, the smallest type that can store the number is represented by: int, unsigned int, long, unsigned long, long long, and unsigned long

Because hexadecimal is commonly used to represent addresses, there are no symbols, so unsigned long is more appropriate than long

Char is designed for storing characters (letters and numbers)

Many systems support no more than 128 characters, then a single byte can represent all symbols

Char is smaller than short

//Chartype.cpp-The char type#include <iostream>intMain () {using namespacestd; Charch; cout<<"Enter a charcter:"<<Endl; CIN>>ch; Cin.Get(); cout<<"hola!"; cout<<"Thank for the"<< CH <<"character."<<Endl; Cin.Get();}

When input, CIN converts the keyboard input m to 77, and when output, cout converts 77 to the character m to be displayed

//Morechar.cpp-the char type and int type contrasted#include <iostream>intMain () {using namespacestd; CharCH ='M'; inti =ch; cout<<"The ASCII code for"<< CH <<" is"<< I <<Endl; cout<<"Add one to the character code:"<<Endl; CH= ch +1; I=ch; cout<<"The ASCII code for"<< CH <<" is"<< I <<Endl; cout<<"Displaying Char ch using cout.put (CH):";    Cout.put (CH); Cout.put ('!'); cout<< Endl <<" Done"<<Endl; Cin.Get();}

' m ' denotes the character encoding of M

Char ch;cin>>ch;

If input 5 presses ENTER, the 5 corresponding character encoding "53" is stored in ch.

int n;cin>>n;

If you enter 5, the value 5 is stored in N.

1. member function Cout.put ()

The class defines how the characters are represented and controlled, the member functions are categorized all, and the methods that manipulate the data are described.

such as Class Ostream has a put () member function, which is used to output characters and can only be used by a specific object of the class to use member functions.

A period is called a member operator, and Cout.put () represents the use of a function put () through a class object.

Because historically, before release 2.0 of C + +, cout the character variable as a character and displayed a string constant as a number. (e.g. ' M ', ' N ')

Cout<<ch Get M

cout << ' M ' get 77

But now we can handle the characters correctly.

2.char literal value

The simplest way to do this is to enclose the character in single quotes and write the characters

However, some characters cannot be entered into the program through the keyboard, so a special method-escape sequence

tr> tr>
encoding of C + + escape sequences
character name ASCII symbol C + + code decimal ASCII hex ascii code
line Break NL (LF) \ n Ten 0xA
horizontal tab HT \ t 9 0x9
Vertical tab VT

\v

one 0xB
backspace bs \b 8 0x8
carriage return CR \ r all 0 XD
ringing BEL \a 7 0x7
Backslash \ \ \ 0x5C
question mark ? \? 0x3F
single quotes ' \ ' 0x27
double quotes " \" 0x22

When you use them as character constants, enclose them in a single quotation mark, place them in a string, not using single quotes

When displaying numbers, it is easier to use endl than \ n, but it is easier to display strings, such as

cout<<x<<endl;

cout<< "dr.x.\n";

All three of the following are newline characters:

cout<<endl;

cout<< ' \ n ';

cout<< "n";

You should use a sequence of symbols when you use a numeric escape sequence or a symbol escape sequence. Numbers are related to a particular encoding, and the symbols are suitable for any encoding, and are also highly readable.

Program Listing 3.7 Bondini.cpp
//Bondini.cpp--using escape sequences#include <iostream>intMain () {using namespacestd; cout<<"\aoperation \ "Hyperhype\" is now activated!\n"; cout<<"Enter your agent code:________\b\b\b\b\b\b\b\b"; LongCode; CIN>>Code; Cin.Get(); cout<<"\ayou entered"<< Code <<". ... \ n"; cout<<"\acode verified! Proceed with plan z3!\n"; Cin.Get();}

3. Universal Character Name

Such as:

int k\u00f6rper;

cout<< "Let them eat g\u00e2teau.\n"

The ISO 10646 code point is 00f6, and A's code point is 00E2.

The above variable is named K?rper and the output is let them eat gateau.

The use of universal character names is similar to escape characters, beginning with \u and \u, \u followed by 8 hexadecimal digits, and \u followed by 16 hexadecimal digits.

Unicode provides a standard numeric code for a large number of characters and symbols, assigning a number-a code point, such as u-222b-to each character. The ASCII code is a subset of it.

4.signed Char and unsigned char

Unlike int, Char is neither unsigned nor signed by default, which is what developers can do to match this type and hardware attributes to the fullest extent possible.

5.wchar_t

The character set that the program needs to handle may not be represented by a 8-bit byte

1. If the large character set is the basic character set of the implementation, you can define char as a 16-bit or longer byte

2. One implementation can support both a Small Basic character set (char representation) and a large extended character set (wchar_t representation)

wchar_t bob=l'P'; wcout<<l"tall"<< Endl

Wcin and wcout are used to handle wchar_t streams, plus prefix l to indicate wide character constants and wide strings.

Added char16_t and char32_t.

The former is 16 bits, the latter 32 bits, are unsigned

char16_t matches/u00f6 Universal character name, char32_t matches/u0000222b

char16_t ch1=u'q'; char32_t ch2=u'\u0000222b';

6.bool type

Interprets a non-0 value as true, which interprets 0 as false

bool is_ready=true; int ans=true;           // Ans=1 int promise=false;    // promise=0 bool start=-;      // start=true bool stop=0;            // Stop=false

7.const Qualifier

Const int months=;

After the constant months is initialized, its value is fixed and the compiler will not allow the value of the constant to be modified.

create a common format for constants  Const type name=value; constint  toes;toes=Ten should be initialized in the declaration If you do not provide a value when declaring a constant, the constant value will be indeterminate and cannot be modified. 

8. Floating point number

The storage floating-point number is composed of two parts

Part represents a value, part means to enlarge and reduce the value

such as numbers 34.1245 and 34124.5, the first number can be represented as 0.341245 and 100, the second represented as 0.341245 and 10000

Floating-point numbers are written in two ways

One is the decimal point method, the other is 3.45E6, equivalent to the 3.45*10 6, if negative means dividing by 10.

Floating-point types:

The requirements for the number of significant digits in C + + are:

32≤float≤double≤long Double

and double≥48

Generally float is 32 bits, double is 64 bits,

A long double is 80, 96, or 128 bits

Program Listing 3.8Floatnum.cpp//Floatnum.cpp--floating-point types#include <iostream>intMain () {using namespacestd; COUT.SETF (ios_base::fixed, Ios_base::floatfield); floatTub =10.0/3.0; DoubleMint =10.0/3.0; Const floatmillion =1.0e6; cout<<"tub ="<<tub; cout<<", a million tubs ="<< million*tub; cout<<", \nand ten million tubs ="; cout<<Ten* million*tub<<Endl; cout<<"mint ="<< Mint <<"And a million mints ="; cout<< Million*mint <<Endl; Cin.Get();}

The SETF () call forces the output to use fixed-point notation to better understand the accuracy, prevent the program from switching large values to e notation, and display the program to 6 digits after the decimal point.

Usually cout will delete the end of 0, with COUT.SETF () will override this behavior, because cout print 6 decimal places, tub and mint are accurate, but after 1 million, there is an error, indicating that float has 7 significant digits, and double at least 13 bits is valid

Default floating-point constants are stored as double types

1.234f//f or F suffix float type

2.45e20f

2.345324E28//double

2.2L//l or L suffix long double type

Floating point Two The advantage is that it represents a value between integers, which indicates a large range. The disadvantage is that the operation is slow and the accuracy is reduced.

Program Listing 3.9Fltadd.cpp//Fltadd.cpp--precision problems with float#include <iostream>intMain () {using namespacestd; floatA =2.34e+22f; floatb = A +1.0f; cout<<"a="<< a <<Endl; cout<<"b-a="<< b-a <<Endl; Cin.Get();}

a=2.34e+022

B-a=0

Because float can only represent the first 6 or 7 digits in a number, modifying the value of the 23rd bit will have no effect

C + + learning (vi) Getting started--Identifying constant 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.