Latest web development tutorials

C ++ variable types

In fact, only variable is the name of the program operational store. C ++ for each variable has the specified type, the type of the variable determines the size and layout of the store, a value within the range can be stored in memory, the operator can be applied to the variables.

Variable name can consist of letters, numbers, and the underscore character. It must begin with a letter or an underscore. Uppercase and lowercase letters are different, because C ++ is case-sensitive.

Based on the previous chapter explain the basic types, there are several basic types of variables, will be explained in the next chapter:

类型描述
bool存储值 true 或 false。
char通常是一个八位字节(一个字节)。这是一个整数类型。
int对机器而言,整数的最自然的大小。
float单精度浮点值。
double双精度浮点值。
void表示类型的缺失。
wchar_t宽字符类型。

C ++ also allows you to define a variety of other types of variables, such asenumerating, pointers, arrays, references, data structures, classes, etc., which will be explained in later chapters.

Below we will explain how to define, declare and use of all types of variables.

Variable definition in C ++

Variable definition is to tell the compiler to create a variable to store where, and how to create storage variables. Variable definition specifies a data type, and contains a list of the type or more variables, as follows:

type variable_list;

Here, type must be a valid C ++ data types, which can be char, w_char, objects int, float, double, bool, or any user-defined,variable_list may consist of one or more identifiers name composed of a plurality of identifiers separated by commas. Here are a few valid statement:

int    i, j, k;
char   c, ch;
float  f, salary;
double d;

Rowint i, j, k; declare and define variables i, j and k, which instructs the compiler to create a variable of type int called i, j, k's.

Variables can be initialized when they are declared (specify an initial value). Initialization consists of an equal sign, followed by a constant expression, as shown below:

type variable_name = value;

Here are a few examples:

extern int d = 3, f = 5;    // d 和 f 的声明 
int d = 3, f = 5;           // 定义并初始化 d 和 f
byte z = 22;                // 定义并初始化 z
char x = 'x';               // 变量 x 的值为 'x'

Defined without initialization: variables with static storage duration will be implicitly initialized to NULL (all values ​​are 0 bytes), other initial values ​​of all variables are undefined.

C ++ variable declaration

Variable declarations assured the compiler variable to a given type and name exists, so the compiler without the need to know the complete details of the case of a variable can continue further compilation. Variable declaration has its significance only at compile time, when the compiler needs to connect the actual variable declarations.

When you use multiple file and only define a variable in one file (file defined variables during program connection is available), the variable declaration is very helpful. You can use theextern keyword to declare a variable anywhere.Although you can declare a variable multiple times in the C ++ program, but the variables can only be defined once in a file, function or code block.

Examples

Try the following examples, wherein the variables have been declared in the head, but they are defined and initialized in the main function:

#include <iostream>
using namespace std;

// 变量声明
extern int a, b;
extern int c;
extern float f;
  
int main ()
{
  // 变量定义
  int a, b;
  int c;
  float f;
 
  // 实际初始化
  a = 10;
  b = 20;
  c = a + b;
 
  cout << c << endl ;

  f = 70.0/3.0;
  cout << f << endl ;
 
  return 0;
}

When the above code is compiled and executed, it produces the following results:

30
23.3333

Similarly, in the function declaration, a function name, and the actual definition of the function can be performed anywhere. E.g:

// 函数声明
int func();

int main()
{
    // 函数调用
    int i = func();
}

// 函数定义
int func()
{
    return 0;
}

In C ++ lvalue (Lvalues) and right value (Rvalues)

C ++ There are two types of expressions:

  • Left value (lvalue): pointing to a memory location is called the left expression value (lvalue) expression.Lvalue may appear in the left or right of an assignment.
  • Rvalue (rvalue): The term rvalue (rvalue) refers to the value of some of the addresses stored in the memory.Its value is not the right expression assigned, that is to say, the right values ​​can appear on the right side of an assignment, but can not appear on the left side of an assignment.

Variable value is left, it can appear on the left side of an assignment. Numeric literals is the right value, and therefore can not be assigned, it can not appear on the left side of an assignment. The following is a valid statement:

int g = 20;

But the following is not a valid statement, it will generate a compile-time error:

10 = 20;