Ticker

6/recent/ticker-posts

C++ VARIABLES | LITERALS OR CONSTANTS

VARIABLES 

A variable is the most fundamental aspect of any computer language. It is a location in the computer memory which can store data and is given a symbolic name for easy reference. The variables can be used to hold different values at different values at different times during the execution of a program. 

To understand more clearly, let us take following example: 

Total = 20.00                     (i) 

Net = Total - 12.00             (ii) 

In equation (i), a value 20.00 has been stored in a memory location Total. The variable Total is used in statement (ii) for the calculation of another variable Net. The point worth noting is that the variable Total is used in statement (ii) by its name not by its value. Before a variable is used in a program, it has to be defined. This activity enables the compiler to make available the appropriate type of location in the memory. The definition of a variable consists of the type name followed by the name of the variable. 

Declaration of variables: 

In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float, etc.) followed by a valid variable identifier. 

For example: 

int a; 

float mynumber; 

If you are going to declare more than one variable of the same type, you can declare all of them in a single statement by separating their identifiers with commas. 

For example: 

int a,b,c; 

Examples of some valid variable declarations are: 

(i) int count; 

(ii) int i, j, k; 

(iii) char ch, first; 

(iv) float total, Net; 

(v) long int sal; 

LITERALS OR CONSTANTS

A number which does not charge its value during execution of a program is known as a constant or literals. Any attempt to change the value of a constant will result in an error message. A keyword const is added to the declaration of an identifier to make that identifier constant. A constant in C++ can be of any of the basic data types. Let us consider the following C++ expression: 

const float Pi = 3.1215; 

The above declaration means that Pi is a constant of float types having a value: 3.1415. 

Examples of some valid constant declarations are: 

const int rate = 50; 

const float Pi = 3.1415; 

const char ch = „A‟; 

Scope of variables: 

A variable can be either of global or local scope. A global variable is a variable declared in the main body of the C++ source code, outside all the functions. Global variables can be called from anywhere in the code, even inside functions, whenever it is after its declaration. 

The local variable is one declared within the body of a function or a block. To illustrate the scope of global variable and local variable, let us look at the figure 4. The scope of local variables is limited to the block enclosed in braces ({}) where they are declared. For example, if they are declared at the beginning of the body of a function (like in function main), their scope is between its declaration point and the end of that function. 

ALSO SEARCH:

"Keyword"

"C++ literals"

"C++ variable types"

"constants in C++ example"

"literals in C++ example"

"define constants in C++"

"types of constants in C++"

"C++ variables literals or constants in c++"

"C++ variables literals or constants example"

"c variables literals or constants in C++""