Defined Variables and Constants

Variables

A variable is a named memory location or memory cell. It is used to store program's input data and its results during execution. The value of a variable may change during the execution of program but the name of variable can change. 

Defined Variables and Constants

The variables are created in RAM that is a temporary memory. That is why the data stored in variables is also temporary. It can only be used and processed during the execution of program. The data stored in a variable is automatically removed when the program ends.

Name of variable

It refers to an identifier that represents the memory location.

Address of variable

It refers to the memory location of the variable.

Contents of variable

It refers to the value stored in memory location referred by variable.

Variables and Constants

Variables Declaration

The process of specifying the variable name and its type is called variable declaration. A program can have as many variables as needed. All variables must be declared before they are used in the program. A variable can be declared anywhere in the program before its use.

The variable declaration provides information to the compiler about the variable. The compiler uses the declaration to determine how much memory is needed for each variable. Different types of data require different amount of memory. The required number of bytes are allocated when a variable is declared. 

For example, an integer variable requires 2 bytes whereas a character variable requires 1 byte. The memory bytes are used to store the value of the variable. Once a variable is declared, its data type cannot be changed during program execution. However, the value of variable can be changed during execution.

Syntax

The syntax of declaring variables is as follows:

data_type var;

data_type

It indicates type of data that can be stored in variable.

var

It refers to the memory location of the variable.

Examples

Different types of variables are declared as follows:

  • int marks:
  • float average;
  • char grade;
  • double salary;

In the above examples, int float, char and double are keywords that indicate data types. The words marks, average, grade and salary are variable names.

Many variables of same data type can also be declared in single line:

In this case, each variable is separated by comma as follows:

int a, b, c,

Rules for Declaring Variables

Different rules for naming variables in C++ language are as follows:

  • The first character of variable name must be an alphabet or underscore. For example, the variables 9minute, #home and 2kg are invalid variable names.
  • The characters allowed in variable name include uppercase letters (A to Z), lowercase letters (a to z), digits (0 to 9) and underscore (). The uppercase and lowercase letters are considered different in C++. For example, MARKS is different from marks. The underscore is typically used to improve readability. For example, total marks is more readable than totalmarks.
  • The special symbols such as, $, % are not allowed in variable names. For example, $cost and tot.al are invalid variable names,
  • The blank spaces are not allowed in variable names. For example, my var and your car are invalid variable names.
  • The reserved word of C++ cannot be used as variable name. For example, int, void and while are invalid variable names.

Examples:

Some examples of valid variables are as follows:

Marks, sub1, _test, f_name, Integer

Variable

Variable Initialization

The process of assigning a value to a variable at the time of declaration is known as variable initialization. The equal symbol = is used to initialize a variable. The variable name is written on the left side and the value is written on the right side of equal symbol= .

C++ compiler automatically allocates the required memory for the variable when it is declared. The memory location may contain meaningless data known as garbage value may produce unexpected results in some computations. All variables should be initialized t avoid this problem.

Syntax

The syntax of initializing a variable is as follows:

data_type var = value;

data_types

It indicates the data type of the variable to be initialized.

var

It is name of the variable to be initialized.

=

It is the assignment operator used to initialize a variable.

value

It is the value to initialize a variable.


Examples

Some examples of variable initialization are as follows:

  • int n = 100;
  • int x = 50, y = 75;
  • float average = 50.73;
  • char grade = 'A';

Constants

Constant is a quantity that cannot be changed during program execution. 
Two types of constants in C++ are as follows:

  • Literal constant
  • Symbolic constant

Literal Constant

A literal constant is a value that is typed directly in a program. It appears in the program wherever it is needed.

Example

The following statement contains string literal constant Hello World:

cout<<"Hello World";

The following statement contains an integer variable age and a literal constant 19:

int age = 19;

Types of Literal Constants

Different types of literal constants are as follows:

  • Integer constant
  • Floating point constant
  • Character constant
  • String constant

Integer Constants

Integer constants are numeric values without fraction or decimal point. Both positive and negative integer constant are used. The minus sign is used for negative integer constants. The value is positive by default if no sign is used.

Examples

Some examples of integer constants are as follows:

87, 45, -10, -5
The use of L at the end of integer indicates that it is a long integer:
65000L, -55000L

Floating Point Constants

Floating point constants are the numeric values with fraction or decimal point. Both positive and negative floating point constants are used. The minus sign is used for negative floating point constants. The value is positive by default if no sign is used. The use of For f at the end of real value indicates that the value is float.

Examples

Some examples of floating point constants are as follows:

50.75F, 10 22F

The value is considered double if F or f is not used:

15.32, 42 79

The use of L at the end of real value indicates that it is a long double:

52000.33L

Character Constants

Any character written in single quotes is known as character constant. All alphabetic characters, digits and special symbols can be used as character constants.

Examples:

Some examples of character constants are as follows: 

'A', 'n', '9', '=', '&'

Constants

String Constants

A set of characters written in double quotations is called string or string constant. It may consist of any alphabetic characters, digits and special symbols.

Examples:

Some examples of sting constants are as follows:

"Pakistan", "123", "99-Mall Road, Lahore"

Symbolic Constants

A symbolic constant is a name given to the values that cannot be changed. A symbolic constant must be initialized. The value of a symbolic constant cannot change during program execution. The symbolic constants are used to represent a value that is frequently used in a program.

A symbolic constant PI can be used to indicate the value of Pi which is 3. 141593. PI can be written in the program wherever its value is required.

Symbolic constants can be declared in two ways:

  • const Qualifier
  • define Directive

const Qualifier

const qualifier is used to define a constant. The constant is declared by specifying its. name and data type. The constant must be initialized with some value. The initialized value cannot be changed during program execution.

Syntax

The syntax of declaring constants with const qualifier is as follows:

const_data_type identifier = value;

const

It is a keyword used to define a constant.

data_types

It indicates the data type of the constant.

identifier

It represents the name of the constant.

value

It represents the value with which the constant is initialized.

Example:

An example of constant declaration is as follows:

const int N= 100;

The above example defines a constant N that is initialized with 100. The value 100 cannot be changed during the execution of program.

Program that inputs the radius of a circle and displays the circumference by using formula 2nR. Store the value of in a constant.

#include <iostream.h>

#include <conio.h>

void main()

{

const float Pl = 3.141;

float r, cir;

clrscr()

cout<<"Enter radius in centimeters";

cin>>r;

cir=2.0*Pl*г;

cout<<"Circumference = "<<cir;

getch();

}


define Directive

define directive is also used to define a constant. The difference between const qualifier and define directive is that define directive does not specify the data type of the constant. define directive starts with the symbol #. It is not terminated with semicolon.

Syntax

The syntax of define directive is as follows:

#define identifier value

#

It indicates the start of preprocessor directive.

define

It is used to define a constant.

identifier

It is the name of the constant.

value

It represents the value associated with the identifier.

The preprocessor directive replaces all occurrences of the identifier with the value. The identifier is typically written in uppercase.

Example:

#define Pi 3.141593

Program that inputs the radius of a circle and displays the circumference by using formula 2R. The program should use define directive to store the value of n in Pl.

#include <iostream.h>

#include <conio.h>

#define PI 3.141

void main()

{

float r, area;

clrscr()

cout<<"Enter radius ";

cin>>r;

area=2.0*Pl*г;

cout<<"Circumference = "<<area;

getch();

}


Expression

A statement that evaluates to a value is called an expression. An expression gives a single value. An expression consists of operators and operands. An operator is a symbol that performs some operation. Operand is the value on which the operator performs some operation. Operand can be a constant, variable or combination of both. An expression may consist of any number of operators and operands.

Examples:

Some examples of expressions are as follows:

  • A+B;
  • m/n;
  • x+100;

In the expression A+B, + is the operator. A and B are variables used as operands in the expression.

Post a Comment

0 Comments