C++. The concept of variable. The declaration of variable. Local and global variables. Memory allocation for variables. Constants




The concept of variable. The declaration of variable. Local and global variables. Operation expansion of scope :: . Memory allocation for variables. Constants


Content


Search other websites:

1. What is called a variable?

A variable in a program is a named memory cell that is designed to store a certain value. The stored value can be the result of evaluating an expression, a numeric or string constant, and so on.

When developing a program, the programmer, at his own discretion, gives names to variables. The variable name is the name of the identifier. This name must match the C/C++ syntax. For more information about the names of variables, see the topic:

To solve the problem, the programmer can use any number of variables in the program. Most often, variables are used in expressions when performing calculations.

It is desirable that the name of the variable corresponds to its intended purpose in the program. For example, if a variable is used to store a certain maximum value, then its name can be Max (for example).

In C/C++, each variable must have its own type. For more details about the types of C/C ++ data is described in the article:

2. What is the general form of a variable declaration in C/C ++?

General view of the variable description:

type variables_list;

where

  • type – the data type corresponding to variables from the variables_list;
  • variables_list – list of variables of a given type, separated by a comma.

A variable can have up to 1024 characters of any name.

An example of declaring variables of different types.

int a, b, c;
char sym;
bool f_is;
float x, y;
double z;

3. Where in the program can I declare variables? How variables are classified according to the placement in the program?

Variables can be declared:

  • inside of functions;
  • in the definition of the parameters of functions;
  • beyond all functions.

Depending on the declaration in the program, the variables are classified as follows:

  • local variables (declared inside functions);
  • formal parameters (are parameters of functions);
  • global variables (declared outside of functions).





4. What are local variables? Where in the program are local variables declared?

Local variables are variables that are declared (described) inside functions. Only operators that are used in the function body have access to such variables.

Local variables are created when the function is called. As soon as the function is completed, the local variables are destroyed.

Example. Let the function GetMax3 (), which defines the maximum value between three numbers, be given. The function describes the local variable res. The scope of the variable is defined by braces { }.

...

int GetMax3(int a, int b, int c)
{
    int res; // res - local variable
    res = a;
    if (res<b) res = b;
    if (res<c) res = c;
    return res;
}

...

5. What does the destruction of variable mean?

Local variables are created when the function is called. As soon as the function is completed, local variables are destroyed.

The term “destruction of a variable” means: the destruction of a variable in the memory after the completion of the function in which this variable was declared.

6. What does the term “formal parameter” mean?

A formal parameter is a variable, which is the input parameter of a function. When a function is called, arguments are passed to it. The value of these arguments is copied into the formal parameters of the function in the order in which they are called.

Formal parameters are declared after the function name inside the parentheses ( ).

Example. Let the declaration of the function GetMax3() be given, which finds the maximum value between three integers.

...

// Declaring a function GetMax3()
int GetMax3(int a, int b, int c)
{
    // a, b, c - formal parameters
    int res; // res - local variable
    res = a;
    if (res<b) res = b;
    if (res<c) res = c;
    return res;
}

...

// Calling the GetMax3() function from another program code
max = GetMax3(8, 5, 11); // the numbers 8, 5, 11 are arguments of the function

...

In this example, the function GetMax3() has 3 formal parameters with the names a, b, c. When a function is called from another program code, it receives 3 numbers (an arguments) that are copied into formal parameters. The number 8 is copied into the formal parameter a. The number 5 is copied into the formal parameter b. The number 11 is copied into the formal parameter c.

7. What are global variables? What is the difference between global and local variables?

A global variable is a variable that all functions in the program have access to. Global variables save their values for the duration of the program. This time is called the lifetime of the global variable.

A global variable is declared outside of all functions.

Example. In this example, the global variable max and the two functions GetMax2() and GetMax3() are declared.

...

int max; // max - global variable

// Declaring the function GetMax2()
int GetMax2(int a, int b)
{
    // a, b - formal parameters
    max = a; // using of global variable max in the function GetMax2()
    if (max<b) max = b;
    return max;
}

// Declaring the function GetMax3()
int GetMax3(int a, int b, int c)
{
    // a, b, c - formal parameters
    max = a; // using of global variable max in the function GetMax3()
    if (max<b) max = b;
    if (max<c) max = c;
    return max;
}

8. Examples of declaring variables of different types
int a, b, c;
char sym;
bool f_is;
float x, y;
double z;

9. How memory is allocated for variables of integer types?

When a variable is declared, memory is allocated to it. The size of the memory that is allocated depends on the type of the variable. The size of the allocated memory affects the range of values that a variable can take.

For variables of integer types, memory is allocated according to the following tables.

For a 16-bit environment.

Type Bit size Values range
int 16 -32768 … 32767
unsigned int 16 0 … 65535
signed int 16

-32768 … 32767

(is similar to type int)

short int 16

-32768 … 32767

(is similar to type int)

unsigned short int 16

0… 65535

(is similar to type unsigned int)

signed short int 16

-32768 … 32767

(is similar to type int)

long int 32 -2 147 483 648 … 2 147 483 647
unsigned long int 32 0 … 4 294 967 295
signed long int 32

-2 147 483 648 … 2 147 483 647

(is similar to type long int)

For a 32-bit environment.

Type Bit size Values range
int 32 -2 147 483 648 … 2 147 483 647
unsigned int 32 0 … 4 294 967 295
signed int 32

-2 147 483 648 … 2 147 483 647

(is similar to type int)

short int 16 -32768 … 32767
unsigned short int 16 0 … 65535
signed short int 16 -32768 … 32767
long int 32

-2 147 483 648 … 2 147 483 647

(is similar to type int)

unsigned long int 32

0…4 294 967 295

(is similar to type unsigned int)

signed long int 32

-2 147 483 648 … 2 147 483 647

(is similar to type signed int)

Starting with version C ++ 11, two new integer data types were introduced: long long и unsigned long long. When a variable of these types is declared, 64 bits (8 bytes) of memory are allocated.

Range of values for type long long is as follows

-9223372036854775808 ... 9223372036854775807

Range of values for type unsigned long long

0 ... 18446744073709551615

10. How is allocated memory for floating point variables?

For floating-point types, the table of allocated memory for a variable has the form shown below. The allocation of memory for variable floating point types does not depend on the environment (16 or 32 bits).

Type Bit size Values range
float 32 3.4E-38 … 3.4E+38
double 64 1.7E-308 … 1.7E+308
long double 80 3.4E-4932 … 3.4E+4932

11. How memory is allocated for the variables of character types?

For variables of character types memory is allocated based on the following table, regardless of the environment (16 or 32 bits).

Type Bit size Values range
char 8 -128 … 127
unsigned char 8 0 … 255
signed char 8 -128 … 127

12. What are constants in the C++ programming language? How to declare a constant?

A constant is a variable that can not change its value during the program’s lifetime. The value of the constant is assigned when it is declared. To declare a constant, use the keyword const.

General view of the constant declaration

const type constant_name = constant_value;

where

  • type – type that receives the constant;
  • constant_name – the name by which the constant value will be used.

Examples of declaration of constants.

const float Pi = 3.1415; // Pi
const int Max = 1000; // constant named Max
int const Min = 100; // constant named Min
const char New_Line = '\n';
const bool YES = true;

13. Access to global variables from functions. Operation expansion of scope :: . Example

When developing your own programs containing global variables, a situation may arise when a local variable with the same name is used in the function body. In this case, the local variable name overrides the global variable name. When referring to the name in the body of the function, the local variable is taken into account (the global variable is ignored). If such a situation arises, then to access the global variable, you can use the expanding operation, which is denoted by :: .

With the expansion of scope operation :: you can access not only to variables, but also to the constants and functions.

Example.

The example demonstrates:

  • using local and global variables;
  • using a function prototype;
  • use extension operation scope :: to access the global variable.

 

#include <iostream>
using namespace std;

// Global and local variables. Operation ::

// Declare global variable d.
// This variable is available in both functions: MultGlobal2 () and main()
int d = 15;

// Prototype of a function that multiplies the global variable d by 2
int MultGlobal2(void);

void main()
{
  // Declare local variable d in main() function
  int d;

  // 1. Display the value of local variable d
  d = 25;

  cout << "Local: d = " << d << endl; // d = 25 - local variable overrides global variable

  // 2. Print the value of global variable d,
  //    operation :: is used
  cout << "Global: d = " << ::d << endl;

  // 3. Print the doubled value of the global variable d
  cout << "Global: d*2 = " << MultGlobal2() << endl; // d*2 = 30

  // 4. Write new value 27 to local variable d
  d = 27;
  cout << "Local: d = " << d << endl;

  // 5. Write a new value 68 to the global variable d
  ::d = 68;
  cout << "Global: d = " << ::d << endl; // global variable d = 68

  // 6. Print the doubled value of a global variable ::d
  cout << "Global d * 2 = " << MultGlobal2() << endl; // will output 136
}

// A function that uses the value of the global variable d.
// This function does not have access to the local variable d in the main() function.
int MultGlobal2(void)
{
  // the global variable d is available here
  return d * 2;
}

The result of the program

Local: d = 25
Global: d = 15
Global: d*2 = 30
Local: d = 27
Global: d = 68
Global d * 2 = 136


Related topics