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

The type of variable in C+


May 11, 2021 C++


Table of contents


The type of variable in C+

A variable is really just the name of a store that a program can operate on. Each variable in C+ has a specified type that determines the size and layout of the variable store, 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. Capital 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 that will be explained in the next chapter:

type describe
bool Store TRUE or FALSE.
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.
wchar_t Wide character type.

It also allows you to define various other types of variables, such as enumerations, pointers, arrays, references, data structures, classes, and so on, which will be covered in subsequent chapters.

Below we'll show you how to define, declare, and use various types of variables.

The definition of a variable 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, can be a 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.

The 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 C++ 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 <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, 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+:

  • 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.
  • 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;