c ++ data types Review

  c ++ basic basic types in the following table (all types and lengths in the range subject to the following table 32-bit processors)

 

Type Name Length (bytes) Ranges
bool 1 flase,true
char 1 -128~127
signed char 1 -128~127
unsigned char 1 0~255
short(signed short) 2 -32768~32767
unsigned short  2 0~65535
int(signed int) 4 -2147483648~2147483647
unsigned int 4 0~4294967295
long(signed long) 4 -2147483648~2147483647-
unsigned long 4 0~4294967295
float 4 3.4*10-38~3.4*1038
double 8 1.7*10-308~1.7*10308
long double 8 1.7*10-308~1.7*10308

 

 

 

 

 

 

 

 

 

 

 

Wherein there is a wide character, it is by way of the following.

typedef short int wchar_t;

Can be seen from the table, the basic data types are c ++ bool, char, int, float, double, etc. type. In addition bool type, it can be divided into two categories: integer and floating point. Because the char type can essentially be considered to be shaping a length of 1, to store the ASCII. Where the keyword unsigned, signed, short, long, are called modifiers.

  Int wherein when modified with short, short int represents a short integer, 2 bytes. At this time, it may be omitted int, long int and represents long integer, 4 bytes may be omitted int.

  iso c ++ standard does not specify the number of bytes of each data type and value ranges. Different compilers have different implementations of this. For 32-bit processors will typically long int and the two data types represented by 4 bytes, but for some of the 64-bit processor, is represented by a 4-byte int, long represented by 8 bytes. Thus, (unsigned) int, and unsigned long, while having the same range in the table, but still are two different types. 
  In general, if the number of bytes occupied by the range and no special requirements on an integer, using (unsigned) int type is appropriate, because he has the highest processing efficiency.
note

1, typedef type

   You can use  typedef  take a new name for an existing type. Here is typedef to define a new type of syntax:

typedef type name

    For example, the following statement will tell the compiler, feet is another name for an int:

typedef int feet;

      Now, the following statement is perfectly legal, it creates an integer variable distance:

feet distance;

2, enumerated types

  Enumerated type (Enumeration) is a derived data type in C ++, which is a collection of several enum constant defined by the user. If a variable is only a few possible values ​​can be defined as an enumeration (enumeration) type. Within the scope of the so-called "enumeration" refers to the value of the variable list them out, the values ​​of variables can only be enumerated values.

Creating enumeration, use the keyword  enum . The general form of enumerated types are:

enum enum {name 
     identifier [ = integer constant], 
     an identifier [ = integer constant], 
... 
    Identifier [ = integral constant] 
} enumeration;

If enumeration has not been initialized, i.e. omitted "= integer constant", beginning from the first identifier.

For example, the following code defines a color enumeration, the variable c of type color. Finally, c is assigned the value "blue".

 

enum color { red, green, blue } c;
c = blue;

 

By default, the value is 1, the name of the third value is 0, the second name first name is 2, and so on. However, you can also assign a specific value to the name, just add an initial value. For example, in the following enumeration, 5 Green value.

 

enum color { red, green=5, blue };

Here, Blue  is 6, because by default, than it will be in front of each name in the name of a big one, but the value 0 is still red.

3, Constant

  Constant refers to the amount in the whole process running its value is always immutable, that is, the direct use of notation. Which can be divided into integer constant, real constants, character constants, string constants, Boolean constant. Note that the real constant, which may be in the form of index .123E-1,12.e2,1.e-3, but not, for this e-3 format ( the number of e is not case sensitive and the back of e must be an integer )

The default value of the real constants type double, as if there is a suffix
   12.3f 
the number of type float

4, variable

  Declare a variable just information about the variable name identifier report said compiler, the compiler know the identifier, but the statement does not necessarily lead to the allocated memory. Means that a defined variable is assigned to the variable memory space for storing data corresponding to the type, the variable name is the name of the corresponding memory unit. In c ++ in most cases, the variable declaration is variable definitions, exceptions only declare external variables.

  

auto storage type: the memory space allocated using a stack manner, is temporarily stored, the memory space which can be used to cover a plurality of times. 
register storage class: stored in a general register. 
extern storage types: all are referenced in blocks and functions. 
static storage types: fixed address is stored in, and are valid across the entire program from running in memory.

Use const keyword to create symbolic constants, the constant is created and its value is fixed, and the compiler will not allow modify the values ​​of the constants.

const int a = 20;

Note should be when you declare a const initialization, if no value when you declare a constant, the value of this constant is undefined, and can not be modified.

5, the operator

Operator is a symbol tells the compiler to perform a specific mathematical or logical operations. C ++ built a wealth of operators, and provides the following types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Miscellaneous Operators

          Operator Precedence

后缀  () [] -> . ++ - -   从左到右 
一元  + - ! ~ ++ - - (type)* & sizeof  从右到左 
乘除  * / %  从左到右 
加减  + -  从左到右 
移位  << >>  从左到右 
关系  < <= > >=  从左到右 
相等  == !=  从左到右 
位与 AND  从左到右 
位异或 XOR  从左到右 
位或 OR  从左到右 
逻辑与 AND  &&  从左到右 
逻辑或 OR  ||  从左到右 
条件  ?:  从右到左 
赋值  = += -= *= /= %=>>= <<= &= ^= |=  从右到左 
逗号  从左到右 

--摘自《C语言程序设计实用问答》       
    问题:如何记住运算符的15种优先级和结合性?    
    解答:C语言中运算符种类比较繁多,优先级有15种,结合性有两种。    
    如何记忆两种结合性和15种优先级?下面讲述一种记忆方法。    
    结合性有两种,一种是自左至右,另一种是自右至左,大部分运算符的结合性是自左至右,只有单目运算符、三目运算符的赋值运算符的结合性自右至左。    
    优先级有15种。记忆方法如下:    
    记住一个最高的:构造类型的元素或成员以及小括号。    
    记住一个最低的:逗号运算符。    
    剩余的是一、二、三、赋值。    
    意思是单目、双目、三目和赋值运算符。    
    在诸多运算符中,又分为:    
    算术、关系、逻辑。    
    两种位操作运算符中,移位运算符在算术运算符后边,逻辑位运算符在逻辑运算符的前面。再细分如下:    
    算术运算符分     *,/,%高于+,-。    
    关系运算符中,〉,〉=,<,<=高于==,!=。    
    逻辑运算符中,除了逻辑求反(!)是单目外,逻辑与(&&)高于逻辑或(||)。    
    逻辑位运算符中,除了逻辑按位求反(~)外,按位与(&)高于按位半加(^),高于按位或(|)。    
    这样就将15种优先级都记住了,再将记忆方法总结如下:    
    去掉一个最高的,去掉一个最低的,剩下的是一、二、三、赋值。双目运算符中,顺序为算术、关系和逻辑,移位和逻辑位插入其中。

 

Guess you like

Origin www.cnblogs.com/yc555/p/11263250.html