C++ - SPLessons

C++ Variables

Chapter 9

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Variables

C++ Variables

shape Introduction

A name that identifies a “Variable, Function, Class, Module, or any other user-defined item” and starts with “Capital / Small Letters, Underscore (_) , and digits”. That name is called as Identifiers.

Variable

shape Description

Any identifier assigns a particular memory location with respect to the datatype allocated to it and it is called as “Variable. They can be assigned values which is a variable initialization done before the execution of program.

CPP Variables acts like a container of data. The variable can be changed as per the requirement of the user and can be represented by any datatype like int, char, float, etc.Variables in C++ are categorized into three groups.

C++ Variable Naming Rules

shape Keywords

and and_eq asm auto bitand bitor
bool break case catch char class
compl const const_cast continue defualt delete
do double dynamic_cast else enum explicit
export extern FALSE float for friend
goto if inline int long mutable
namespace new not not_eq operator or
or_eq private protected public register reinterpret_cast
return short signed sizeof static static_cast
struct switch template this throw TRUE
try typedef typeid typename union unsigned
using virtual void volatile wchar_t while
xor xor_eq alignas alignof char16_t char32_t
constexpr decltype noexcept nullptr static_assert thread_local
final override

Declaring Variables in C++

shape Description

Allocates memory of defined data type and no value is assigned to the variable. Multiple variables can be declared at a time.While declaration, memory space is not allotted for the variables.

Syntax:

datatype variable_list;

shape Example

int a;
float b,c,d;       //multiple variable declaration

Initializing Variables in C++

shape Description

The values are assigned to the variables.

  • Initialization allots the memory space as per the datatype given.
  • Value is declared only once.

Syntax:

datatype variable_name=value;

shape Example

int a=1,d=4;    //multiple variable initialization
char c='B';
float b=3.14;

Variable initialization that can be done at run-time also is called Dynamic Initialization.

int i;
i=sum+1;

Variable Types

shape Description

Every variable declared should be of specific type. Variable types in C++ are:

shape Example

#include<iostream>
using namespace std;
int main()
{
	char student='r';
	int age=23;
	float marks;
	marks=89.6;
	bool tf=true;
	cout<< student <<endl <<age << endl << marks << endl << tf << endl;
}

Output:

r
23
89.6
1

Formal parameters

shape Description

The formal parameters can be used in functions.

  • These parameters appear in function definition section.
  • The formal parameters are local to the definition section and can be accessed by the main function of the program.

Local Variables

shape Description

The variables declared in a block of code are called as “Local Variables”. They are not accessed by other blocks.

{
int x=10;
}

Static variable

shape Description

If function calls are applied on local variables the value may change on exit of block. Use “static” keyword to prevent it.

int main()
{
static int x=10;
}

Final variables

shape Description

The final variables once fixed cannot be changed throughout the program.

int main()
{
final int x=10;
}

Global Variables

shape Description

The variables declared inside a program and outside the functions are called as “Global Variables”.

int a=10;
int main()
{
int a;
}

Register Variables

shape Description

The variables stored in CPU registers instead of storing them in a memory to increase the speed of access are called as “Register Variables”. They save the processor time but register variables can be used only temporarily.

#include<iostream>
using namespace std;
int c = 1;             //global variable

void test();

void test()
{
static int b = 5; //static variable
cout<< b <<endl;
}

int main()
{
int b = 5;           // local variable
cout<< b <<endl;
cout<< c <<endl;
test();
return 0;
}

Output:

5
1
5

Scope of CPP Variables

shape Description

A part in a program where a variable has its existence is called as Variable Scope. If the scope rules are not followed they are not accessible. To declare the C++- variables:

1) The variables inside the function definition section are also called as Formal Parameters.

2) The variables inside the block or a function are called as Local Variables.


3) The variables that are declared in the main body of source code outside any function is called as Global Variables.

4) The value of static variable remains the same throughout the program but it can change as per the function call.

5) The variables cannot be changed throughout the program even in the local block.

Lvalue and Rvalue:

shape Description

Lvalue is the variable present on the left of the = operator.

Rvalue is the value present on the right of = operator.

int a=3;    // a is lvalue and 3 is rvalue

Summary

shape Key Points

The chapter CPP Variables draws out following important points.

  • CPP Variables stores the data
  • “Int, Float, Char and Boolean” are variable types
  • “lvalue and rvalue” are associated with the equal to( = ) operator
  • Global variables can be declared anywhere
  • Local variables cannot be used outside the scope
  • Every variable has to be initialized
  • Static Variables declared globally can be modified locally and does not have effect during function calls.
  • Final Variables are always fixed
  • Register Variables are stored in registers

shape Programming
Tips

Every variable must be declared with a type at the beginning of the program.