Cpp Tutorial: C++ Variables, Data Types, variable scope and Storage Classes

In this chapter we shall look at below topics:
1. C++ variables
2. C++ Data types
3. C++ Enums
4. C++ variable scope
5. C++ Storage Classes
6. C++ Escape Sequences

1. C++ variables

Variables are the name given to a memory location. They are used to store a value. Every variable is associated with a data type, and it holds the value of the type it is declared with.
Variable Declaration: Before a variable is used, it should be declared. Declaration will help the compiler to know about 3 things. 1. The name of variable. 2. The type of the variable. 3. Space to be allocated.
Variable declaration syntax:
<data-type> <variable-name>;
Variable Initialization: When a variable is declared, appropriate memory will be allocated for that variable. Initially it will have invalid value or garbage value. Hence it should be initialized, else the output will be unexpected.
Variable Initialization Example:
int var = 0; // declaration and initialization

2. C++ Data types

Like in most of the programming language, there are different data types in C++. Data-types helps the compiler to allocate appropriate space to store a variable. Every variable in C++ is associated with a data-type. Below are the types of data-types are available.
1. Primitive Data-types: They are the basic data-types provided by the compiler.
int Ex: int a = 10;
float Ex: float b = 10.23;
char Ex: char c = ‘a’;
bool Ex bool d = true;
double Ex double e = 12349.543;
void Ex void *ptr = NULL; /*void means empty data-type */
2. User-Defined Data-types:
structure
class
union
enumerations
3. Derived Data-types:
arrays
function
pointer
reference
Modifiers:
We use modifiers with primitive data-types to expand the storage of the variable.
signed
unsigned
short
long
Note:
1. Not all of the modifiers are applicable to all the data-types.
2. All 4 modifiers can be applied to int.
3. signed and unsigned can be applied to char.
4. long can be used with double.
5. Some of the modifiers can be combined.
Example:
unsigned char a = 'a';
unsigned short int b = 10;
unsigned long int c = 3456;

The difference between signed and unsigned are, unsigned variables will not store the sign of a number. To store the sign, it will take 1 byte of data.

Below example we use “sizeof” operator to know how much each variable type is taking the memory.

#include<iostream>
using namespace std;

int main()
{
	cout<<"The size of int is: "<<sizeof(int) <<endl;
	cout<<"The size of char is: "<<sizeof(char) <<endl;
	cout<<"The size of short int is: "<<sizeof(short int) <<endl;
	cout<<"The size of long int is: "<<sizeof(long int) <<endl;
	cout<<"The size of float is: "<<sizeof(float) <<endl;
	cout<<"The size of double is: "<<sizeof(double) <<endl;
	return 0;
}
Output:
The size of int is: 4
The size of char is: 1
The size of short int is: 2
The size of long int is: 8
The size of float is: 4
The size of double is: 8

3. C++ Enums:

Enums in C++ is used to limit the options of a variable. Suppose we have a data-type that stores week names. We know that there are 7 days in a week. Hence we can limit that with a enum.
Syntax:
enum <enum-variable-name> { list of names} variable-list;
Example:
enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday} weekOfDay;
or
week myWeek;
There are 2 ways to declare a enum variable.

Method 1:

Declare a enum variable during enum declaration.

#include<iostream>
using namespace std;
enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday} weekOfDay;


int main()
{
   weekOfDay = sunday;
   cout<<weekOfDay<<endl;
   
   weekOfDay = monday;
   cout<<weekOfDay<<endl;
   
   weekOfDay = tuesday;
   cout<<weekOfDay<<endl;
   
   return 0;
}

Output:

0
1
2

Method 2:

Declare a enum variable as a normal variable.

#include<iostream>
using namespace std;
enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday};

int main()
{
   week weekOfDay = wednesday;
   cout<<weekOfDay<<endl;
   
   weekOfDay = thursday;
   cout<<weekOfDay<<endl;
   
   weekOfDay = friday;
   cout<<weekOfDay<<endl;
   
   return 0;
}

Output:

3
4
5

All enum variables will start with the value of 0.

You can change the default value, by assigning a value during enum declaration as shown below.
The subsequent options will take the next number.

#include<iostream>
using namespace std;
enum week {sunday =10 , monday, tuesday, wednesday, thursday, friday, saturday};

int main()
{
   week weekOfDay = sunday;
   cout<<weekOfDay<<endl;
   
   weekOfDay = monday;
   cout<<weekOfDay<<endl;
   
   weekOfDay = friday;
   cout<<weekOfDay<<endl;
   
   return 0;
}

Output:

10
11
15

 

4. C++ scope of variable:

1. Local Variables.
2. Global Variables.

1. Local Variables:

1. They are defined inside a function.
2. There lifetime is till the execution of that function.
3. The variables that are in the function arguments are called as formal parameters.

Example:

#include<iostream>
using namespace std;

int main()
{
	int i; //Declaration of local variables
	float f;
	i = 10; //initialization of local variables
	f = 34.56;
	return 0;
}

2. Global variables:

1. They are defined outside of a function.
2. Their life time is till the execution of the program.
3. They are available for all the functions in that file or project.
4. If there is a local variable of the same name, then local variable to that function is given priority.

Example:

#include<iostream>
using namespace std;

int global = 30;
int i = 50;


int main()
{
	int i; //Declaration of local variables
	float f;

	i = 10; //initialization of local variables
	f = 34.56;

	cout<<"The value of global is : "<<global<<endl;

	cout<<"The value of i is : "<<i<<endl;

	return 0;
}

Output:

The value of global is : 30
The value of i is : 10

5. C++ Storage Classes:

There are 5 storage classes in CPP.

auto
register
static
extern
mutable

Storage classes helps in the visibility and scope of the variables.

auto

1. When you declare a local variable, it will be automatically taken as auto.
2. The scope is till the execution of the function.
3. The lifetime of the variable is till the execution of a function.
4. initial value will be garbage value.
5. We use the keyword “auto” to define a automatic variable.

Example:

#include<iostream>
using namespace std;

int main()
{
	int i = 10;

	cout<<"The value of i is : "<<i<<endl;

	return 0;
}

Output:

The value of i is : 10

 

register:

1. We can only declare local variables as register.
2. The scope is till the execution of the function.
3. The lifetime of the variable is till the execution of a function.
4. initial value will be garbage value.
5. It is a request to the compiler to store the variable in the computer register. It is not guaranteed that it will be able to do it.
6. We use the keyword “register” to define a register variable.

Example:

register int i = 10;

static

1. When you declare a local variable as static new instance of that variable is not created for each function call.
2. The scope is till the execution of the function.
3. The lifetime of the variable is till the execution of a function.
4. initial value will be zero.
5. If a global variable is declared as static, then that variable is visible to that file only.
6. We use the keyword “static” to define a static variable.

Example:

#include<iostream>
using namespace std;

int func()
{
	static int i = 1;
	       int j = 1;
	i++;
	j++;
	cout<<"The value of i = "<<i<<endl;
	cout<<"The value of j = "<<j<<endl;
return 0;

}

int main()
{
	
	func();
	func();
	func();
	func();
	func();
	return 0;
}

Output:

The value of i = 2
The value of j = 2
The value of i = 3
The value of j = 2
The value of i = 4
The value of j = 2
The value of i = 5
The value of j = 2
The value of i = 6
The value of j = 2

As you can see, value of i is stored and incremented for each function call, but value of j is reset for each function call.

extern

1. We use extern variable to access a variable from another file.
2. That another file should belong to the same project.
3. We use the keyword “extern” to define an extern variable.

1.c

#include<iostream>
using namespace std;

extern int i;

int main()
{

	cout<<"The i value is :" <<i <<endl;
	return 0;
}

2.c

#include<iostream>
using namespace std;

int i = 10;

Output:

g++ 2.cpp 1.cpp
The i value is :10

Mutable

Mutable storage class are applicable to objects only. We shall discuss in further chapters.

6. C++ Escape Sequences

\'	single quote
\0    	Null
\\	backslash
\a	audible bell
\"	double quote
\b	backspace
\?	question mark
\f	form feed - new page
\n	new line
\r	carriage return
\t	horizontal tab
\ooo  octal number
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *