Latest web development tutorials

C variable

In fact, only variable is the name of the program operational store. C each variable has a specific type, determines the type of variable to store the size and layout of the 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:

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

C language also allows you to define a variety of other types of variables, such as enumerating, pointers, arrays, structures, unions, etc., which will be explained in later chapters, this chapter we first explain the basic variable types.

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 type, 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 to the compiler variable to ensure the type and name of the existence of such a compiler variables without the need to know the complete details of the case can continue further compilation. Variable declaration has its significance only at compile time, when the compiler needs to connect the actual variable declarations.

Declare variables there are two cases:

  • 1, one is required to establish the storage space. For example: int a statement at the time had already established a storage space.
  • 2, the other is the need to establish the storage space by using the extern keyword to declare the variable name without defining it. For example: extern int a in which a variable can be defined in another file.
  • Extern keyword unless otherwise defined are variable.
extern int i; //声明,不是定义
int i; //声明,也是定义

Examples

Try the following examples, where the variable has been declared in the head, but the definition and initialization in the main function:

#include <stdio.h>

// 变量声明
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;
  printf("value of c : %d \n", c);

  f = 70.0/3.0;
  printf("value of f : %f \n", f);
 
  return 0;
}

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

value of c : 30
value of f : 23.333334

C in the left value (Lvalues) and right value (Rvalues)

There are two types of C expressions:

  1. 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.
  2. 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;