Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C variable


May 11, 2021 C


Table of contents


The C variable

A variable is really just the name of a store that a program can operate on. Each variable in C has a specific type, which determines the size and layout of the variable store, and the values in that range can be stored in memory and the operator can be applied to the variable.

The name of a variable can consist of letters, numbers, and underscore characters. I t must begin with a letter or underscore. C apital letters are different from lowercase letters because C is case sensitive. Based on the basic types explained in the previous chapter, there are several basic variable types:

type describe
char Usually an eight-bit byte (one byte).This is an integer type.
int For the machine, the most natural size of the integer.
float Single precision floating point value.
double Double precision floating point value.
void Indicates the lack of type.

The C language also allows you to define various other types of variables, such as enumerators, pointers, arrays, structures, common bodies, and so on, which will be covered in a later section, where we'll start with basic variable types.

The variable definition in C

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

type variable_list;

Here, type must be a valid C data type, which can be char, w_char, int, float, double, bool, or any user-defined object, variable_list can consist of one or more identifier names, separated by commas between multiple identifiers. Here are a few valid claims:

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

Lines int i, j, k; The variables i, j, and k are declared and defined, which indicates that the compiler creates a variable named i, j, and k of type int.

Variables can be initialized at the time of declaration (specifying an initial value). The initializer consists of an equal sign, followed by a constant expression, as follows:

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'

Definition without initialization: Variables with a static storage duration are implicitly initialized to NULL (the value of all bytes is 0), and the initial value of all other variables is undefined.

Variable declaration in C

The variable declaration assures the compiler that the variable exists at a given type and name, so that the compiler can continue to compile further without knowing the full details of the variable. Variable declarations only have their meaning at compile time, and the compiler needs actual variable declarations when the program connects.

Variable declarations are useful when you use multiple files and define variables in only one of them (files that define variables are available when the program is connected). Y ou can declare a variable anywhere using the extern keyword. Although you can declare a variable multiple times in a program, a variable can only be defined once in a file, function, or block of code.

Try the following example, where variables are declared at the head, but they are defined and initialized within 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

Similarly, when a function is declared, a function name is provided, and the actual definition of the function can be made anywhere. For example:

// 函数声明
int func();

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

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

Lvalues and right values (Rvalues) in C

There are two types of expressions in C:

  1. Lvalue: An expression that points to a memory location is called a left value (lvalue) expression. The left value can appear to the left or right of the assignment number.
  2. Right value (rvalue): The term right value (rvalue) refers to values stored at certain addresses in memory. The right value is an expression that cannot be assigned, that is, the right value can appear to the right of the assignment number, but not to the left of the assignment number.

The variable is a left value, so it can appear to the left of the assignment number. T he literal value of the numeric type is the right value, so it cannot be assigned and cannot appear to the left of the assignment number. Here's a valid statement:

int g = 20;

But this is not a valid statement and produces a compile-time error:

10 = 20;