Wu Yuxiong - born natural C ++ language study notes: C ++ constants

A constant is a fixed value, it does not change during program execution. These fixed values, also called literal. 
Constants may be any of the basic data types can be divided into an integer number, floating point numbers, characters, strings, and Boolean values. 
Just like regular variables constant, but the value of a constant can not be changed after the definition.
Integer constants can be decimal, octal or hexadecimal constants. Prefix specified radix: 0x or 0X for hexadecimal, 0 for octal, without the prefix, the default decimal. 
May be an integer constant with a suffix suffixes U and L are combined, U denotes unsigned integer (unsigned), long integer L ( Long ). Suffix can be uppercase, lowercase may be, U and L in any order. 
Here are a few examples of integer constants: 
212          // legal 
215U         // legal 
0xFeeL       // legitimate 
078          // illegal: 8 is not octal numbers 
032UU        // illegal: not repeat suffix 
The following are types of examples of integer constants:
 85          // decimal 
0213        // octal 
0x4b        // hexadecimal 
30          // integer 
30U         // unsigned integer 
30L         // long
30ul        // unsigned long integer
Floating constant consists of an integer part, a decimal point, a fractional part and exponent part. You can use decimal floating point or exponential notation to represent constants. 
When expressed using decimal, integer part must contain, fractional part, or both. When expressed using the exponential form, it must contain a decimal point, index, or both. Unsigned exponent e or E is introduced. 
Here are a few examples of floating-point constants: 
3.14159        // legal 
314159E- 5L     // legal 
510E           // illegal: Incomplete index 
210f           // illegal: no decimal or exponential 
.e55           // illegal: Missing integer or fractional
There are two Boolean constants, they are standard C ++ keyword:
 to true value for true.
false value represents false. 
Should not be true value as 1 , the false value as 0 .
Character constants are enclosed in single quotes. If the constants L (only when capitalized) beginning indicates that it is a wide character constants (e.g. L ' X ' ), at which time it must be stored in a variable of type wchar_t. Otherwise, it is a narrow character constant (e.g. ' X ' ), where it can be stored in char simple variable type. 
A character constant is an ordinary character (e.g. ' X ' ), an escape sequence (e.g., ' \ T ' ), or a universal character (e.g. ' \ u02C0 ' ). 
In C ++ , there are some special characters, when they are preceded by a backslash, they have a special meaning, such as is used to indicate newline (\ n) or tabs (\ t) and the like. The following table lists some of these escape sequences code: 
\\ \ character 
\ '     ' character 
\ "     " character 
\ ?? Character 
\ a alarm bell 
\ b backspace
\ f feed character
\ n newline 
\ r carriage return 
\ t horizontal tab 
\ v vertical tab 
\ OOO one to three octal number 
\ xhh... a hexadecimal number or a plurality of digits 
following example shows Some escape sequence characters: 
#include <the iostream>
 the using  namespace STD; 
 
int main () 
{ 
   COUT << " the Hello \ tWorld \ n-\ n- " ;
    return  0 ; 
} 
when the above code is compiled and executed, it will generate the following results: 
the Hello World
Or a string literal constant is enclosed in double quotation marks "" in. Similar to the character string contains a character constants: ordinary characters, and universal characters escape sequences. 
You can use spaces delimiters, to a very long string constants branches. 
The following example shows the string constants. The following three forms string displayed is the same. 
" The Hello, Dear " 

" the Hello, \ 

Dear "
 
" the Hello, "  " d "  " the EAR "
Constants defined 
in C ++ , there are two constants defined in a simple manner: 
using #define preprocessor. 
Use const keyword.
#define preprocessor 
Here is #define preprocessor constants defined in the form:
 #define identifier value 
specifically, see the following example: 
#include <the iostream>
 the using  namespace STD; 
 
#define the LENGTH 10   
 #define WIDTH. 5
 #define NEWLINE ' \ n-' int main () 
{ int Area;   
   Area = the LENGTH * WIDTH; 
   COUT << Area; 
   COUT <<
 

 
   
   NEWLINE;
    return  0 ; 
} 
when the above code is compiled and executed, it produces the following results: 
50 

const keyword 
can be used const prefix declaration specifies the type of the constants as follows:
 const type variable = value; 
DETAILED see below examples: 
#include <the iostream>
 the using  namespace STD; 
 
int main () 
{ 
   const  int   the LENGTH = 10 ;
    const  int   WIDTH = . 5 ;
    const  char NEWLINE = ' \ n- ' ;
    int Area;   
   
   AreaThe LENGTH * = WIDTH; 
   COUT << Area; 
   COUT << NEWLINE;
    return  0 ; 
} 
when the above code is compiled and executed, it produces the following results: 
50 
Note that the constant is defined as uppercase letters, it is a very good programming practice.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12142875.html