141k views
1 vote
True or Flase

Within a C++ program, all variables regardless of their type go thru a "life-cycle" which includes declaration, initialization, usage and finally dying off when the variable falls out of scope.

User Montgomery
by
5.7k points

1 Answer

7 votes

Answer:

True

Step-by-step explanation:

The c++ program to explain the variable life cycle is given below.

#include <iostream>

using namespace std;

int main() {

char c;

int i;

double d;

string s;

c = 'B';

i = 1111;

d = 33.0123456789;

s = "Name";

cout << " Char " << c << endl;

cout << " Integer " << i << endl;

cout << " Double " << d << endl;

cout << " String " << s << endl;

cout << " Program ends. Variables out of scope, variables die out... " << endl;

return 0;

}

OUTPUT

Char B

Integer 1111

Double 33.0123

String Name

Program ends. Variables out of scope, variables die out...

1. Program begins with declaring variables of type character, integer, double and string.

char c;

int i;

double d;

string s;

2. Next, all these variables are initialized.

c = 'B';

i = 1111;

d = 33.0123456789;

s = "Name";

3. This program only displays the values of the variables. No operation or modification is done to the variables. Hence, in this program, usage of variables is limited to printing the value of the variables.

cout << " Char " << c << endl;

cout << " Integer " << i << endl;

cout << " Double " << d << endl;

cout << " String " << s << endl;

4. In this program, the scope of the variables is the scope of the main() function itself. Once the program ends, the main() function also ends and the variables die out.

5. The life cycle of the variable ends with the termination of the program. Hence, the message is printed.

6. These variables can be used again only when the program is executed again or the same variables are used in another program. These variables can not be used directly without following the life cycle – declaration, initialization, usage, termination.

User Samarasa
by
4.8k points