Learning C ++ variables and constants use (notes)

A variable

1. understanding of the meaning of the variables:

  Variables like after a certain container processing factory capacity. When a variable is defined, as the role of the plant system, the space allocated for the corresponding variables by type. When the definition is complete variable value of the corresponding type can be stored, storing the variable value is greater than the acceptable range of variables can overflow. As the name suggests, typically the value in the variable can be modified, replication can be embodied in the value of the variable storage region.

2.C ++ variable declaration:

  C ++ syntax definition of variables:

      variable_type (variable type) variable_name (variable name);

      or

      variable_type (variable type) variable_name (variable name) = initial_value (initial value);

Such as:

int v1;
char v2='a';

  Declare multiple variables of the same type:

      variable_type (variable type) variable_name (variable name), variable_name (variable name), ..., variable_name (variable names);

      variable_type (variable type) variable_name (variable name) = initial_value (initial value), ..., variable_name (variable name) = initial_value (initial value); 

      variable_type (variable type) variable_name (variable name), ..., variable_name (variable name) = initial_value (initial value);

Such as:

int a,b,c,d;
int e=0,f=0,g=0;
int h,i=0;

3. understand variable scope:

  Each variable has scope, the scope of a variable determines the scope of the definition of variables to take effect, just as the same law, the general legal provisions in different countries only act on their own, only its scope variables to take effect. Within the scope of the variables for the function defined within a function, the global scope of global variables. Variables defined in different function is regarded as an independent entity, when the function completes variables will release the memory occupied space, even if the variable names defined in the different functions the same will not affect each other.

  Global variable definitions Example:

#include<iostream>
using std::cout;
using std::endl;
int a=20;
void Print()
{
    cout<<a<<endl;    
}
int main()
{
   Print(); cout
<<a<<endl; }

  Example define local variables:

#include<iostream>
using std::cout;
using std::endl;
int a=20;
void Print()
{
    int b=30;
    cout<<b<<endl;  
    cout<<a<<endl;    
}
int main()
{
    Print();
    cout<<a;
    //cout<<b;
}

  Note: main function of comment lines "// cout << b;" if uncomment be similar to 'b' was not declared in this scope of the error message. Because the variable "b" is defined only in the Print function is not defined in the main function.

4.C ++ common variable types and ranges:

  4.1 bool variable

  Variable types in C ++ designed for storing Boolean values ​​true and false and created value bool variable is true or false.

  Boolean variable definitions Example:

bool running=false;
  4.2 char variable

  char variable is used to store a single character. The value is 256 char character value, the character represented by the ASCII 7-bit binary code can represent 128 different values ​​of characters; extended ASCII code extension bit to do with a high one byte, the remaining 7 represent 128 different characters

  Plus high of a total of eight characters can represent 256 values.

  

char INPUT_CHAR = ' the Y ' ; // define a variable named INPUT_CHAR, and the initial value is set Y.
  4.3 unsigned int and int variables

  There unsigned int and unsigned short int unsigned long int, unsigned short int binary 16 indicates a range of 0 ~ 65535, unsigned long int binary 32 indicates a range of 0 to 4,294,967,295. In most C ++ compilers are considered int

  When a total of 4 bytes has a 32-bit, unsigned int which was used directly in the range of 0 to 4294967295 Most of the time, it will partially compiler. Unsigned long long int range is twice that of unsigned long int. There are int and long int and short int

  long long int, which represents the highest binary bit is the sign bit, the short range of -32768 to 32767 int, int, long range -2147483648 to 2147483648, the range is long long long int base extended twice.

  4.4 floating-point types float and double

  Floating-point number is a real number may be positive or may be negative, may further comprise a small value. To store a decimal floating-point type can be declared variables, such as: float Pi = 3.1416; statement double precision floating point (double) variables such as: double Pi = 3.14159265, attention want to deposit

  When two reservoirs resultant decimal integer division, the division can not directly, but should be replaced integers like "22.0" This fractional.

5. typedef type substitution variables

  C ++ allows us to easily replace the variable type or name of a more descriptive as we believe, for this purpose can use the keyword typedef. For example: typedef unsigned int Integral_type; unsigned int to an alias Integral_type.

Second, the constant

1. Constant definition:

  Amount is not constant is called artificially changed, in other words the constant value can not be modified. If we want to define a variable and constant with the same name have, we can use const to define, you can use an enum declaration enumeration constants. Added In C ++ version 11

  Constant expressions, use constexpr declaration. #Define is not recommended to use the definition of variables, because the definition of macro constants, just replace the character, the compiler does not care about the type of constants, can not check grammar. const safer and more compact than #define efficient; #define better compatibility.

2. The enumeration defines constants:

  Enum is provided using a particular set of values ​​as a variable.

    For example enumeration constants contain colors of the rainbow:

enum RainbowColors
{
    Violot = 0,
    Indigo,
    Blue,
    Green,
    Yellow,
    Orange,
    Red
};

  Enumeration constant declaration, compiler will convert it to an integer, which is the default starting value is 0, we can be given an initial value in the example above given explicitly initial value is 0, we can give each enumerator Specifies the constant value assigned (each value than a big one before the default value is, of course, in addition

  The first one).

Note: The variable names to conform to specifications, to stand in someone else's point of view, to make the variable or constant names descriptive.

Guess you like

Origin www.cnblogs.com/dulm/p/11218781.html