0% found this document useful (0 votes)
11 views

Variables Notes

The document discusses variables in C++. It defines variables as names that are associated with memory locations used to store values. It describes different variable types like int, char, float, and double. It also explains two types of variable scope - local variables declared within functions and global variables declared outside functions that are accessible anywhere.

Uploaded by

chipetakay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Variables Notes

The document discusses variables in C++. It defines variables as names that are associated with memory locations used to store values. It describes different variable types like int, char, float, and double. It also explains two types of variable scope - local variables declared within functions and global variables declared outside functions that are accessible anywhere.

Uploaded by

chipetakay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Variables are the names you give to computer memory locations which are used to store

values in a computer program.


A variable provides us with named storage that our programs can manipulate.
Each variable in C++ has a specific type, which determines the size and layout of
the variable's memory; the range of values that can be stored within that memory; and the
set of operations that can be applied to the variable.
For example, assume you want to store two values 10 and 20 in your program and at a later
stage, you want to use these two values. Let's see how you will do it. Here are the following
three simple steps −

 Create variables with appropriate names.


 Store your values in those two variables.
 Retrieve and use the stored values from the variables.

Types of Variable Scope In C++


local and global variables.

Local Variables

These Variables declared inside a function or block. They can be used only by statements
that are inside that function or block of code. Local variables are not known to functions
outside their own. Following is the example using local variables −
#include <iostream>
using namespace std;

int main () {
// Local variable declaration:
int a, b;
int c;

// actual initialization
a = 10;
b = 20;
c = a + b;

cout << c;

return 0;
}

Global Variables

Global variables are defined outside of all the functions, usually on top of the program. The
global variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for
use throughout your entire program after its declaration. Following is the example using
global and local variables –

#include <iostream>
using namespace std;

// Global variable declaration:


int g;

int main () {
// Local variable declaration:
int a, b;

// actual initialization
a = 10;
b = 20;
g = a + b;

cout << g;

return 0;
}
A program can have same name for local and global variables but value of local variable
inside a function will take preference. For example −
#include <iostream>
using namespace std;

// Global variable declaration:


int g = 20;

int main () {
// Local variable declaration:
int g = 10;

cout << g;

return 0;
}
When the above code is compiled and executed, it produces the following result −10

Initializing Local and Global Variables


When a local variable is defined, it is not initialized by the system, you must initialize it
yourself. Global variables are initialized automatically by the system when you define them
as follows –

Data Type Initializer

int 0

char '\0'

float 0

double 0

pointer NULL

It is a good programming practice to initialize variables properly, otherwise sometimes


program would produce unexpected result

///////////////////////////////////////////////////////////////////////////////

A variable is a name which is associated with a value that can be changed. For example when
I write int num=20; here variable name is num which is associated with value 20, int is a data
type that represents that this variable can hold integer values. We will cover the data types in
the next tutorial. In this tutorial, we will discuss about variables.

Syntax of declaring a variable in C++

data_type variable1_name = value1, variable2_name = value2;


For example:

int num1=20, num2=100;


We can also write it like this:

int num1,num2;
num1=20;
num2=100;
Types of variables
Variables can be categorised based on their data type. For example, in the above example we
have seen integer types variables. Following are the types of variables available in C++.

int: These type of of variables holds integer value.

char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.

bool: holds boolean value true or false.

double: double-precision floating point value.

float: Single-precision floating point value.

Types of variables based on their scope

Before going further lets discuss what is scope first. When we discussed the Hello World
Program, we have seen the curly braces in the program like this:

int main {

//Some code

}
Any variable declared inside these curly braces have scope limited within these curly braces,
if you declare a variable in main() function and try to use that variable outside main()
function then you will get compilation error.

Now that we have understood what is scope. Lets move on to the types of variables based on
the scope.

1. Global variable
2. Local variable

Global Variable

A variable declared outside of any function (including main as well) is called global variable.
Global variables have their scope throughout the program, they can be accessed anywhere in
the program, in the main, in the user defined function, anywhere.

Lets take an example to understand it:

Global variable example

Here we have a global variable myVar, that is declared outside of main. We have accessed
the variable twice in the main() function without any issues.
#include <iostream>
using namespace std;
// This is a global variable
char myVar = 'A';
int main()
{
cout <<"Value of myVar: "<< myVar<<endl;
myVar='Z';
cout <<"Value of myVar: "<< myVar;
return 0;
}
Output:

Value of myVar: A
Value of myVar: Z
Local variable

Local variables are declared inside the braces of any user defined function, main function,
loops or any control statements(if, if-else etc) and have their scope limited inside those
braces.

Local variable example

#include <iostream>
using namespace std;

char myFuncn() {
// This is a local variable
char myVar = 'A';
}
int main()
{
cout <<"Value of myVar: "<< myVar<<endl;
myVar='Z';
cout <<"Value of myVar: "<< myVar;
return 0;
}
Output:
Compile time error, because we are trying to access the variable myVar outside of its scope.
The scope of myVar is limited to the body of function myFuncn(), inside those braces.

Can global and local variable have same name in C++?

Lets see an example having same name global and local variable.

#include <iostream>
using namespace std;
// This is a global variable
char myVar = 'A';
char myFuncn() {
// This is a local variable
char myVar = 'B';
return myVar;
}
int main()
{
cout <<"Funcn call: "<< myFuncn()<<endl;
cout <<"Value of myVar: "<< myVar<<endl;
myVar='Z';
cout <<"Funcn call: "<< myFuncn()<<endl;
cout <<"Value of myVar: "<< myVar<<endl;
return 0;
}
Output:

Funcn call: B
Value of myVar: A
Funcn call: B
Value of myVar: Z

You might also like