Lec-7-Variable Define Constant
Lec-7-Variable Define Constant
IT2-Lecture
A variable definition tells the compiler where and how much storage to create
for the variable. A variable definition specifies a data type, and contains a list
of one or more variables of that type as follows −
type variable_list;
Here, type must be a valid C++ data type including char, w_char, int, float,
double, bool or any user-defined object, etc., and variable_list may consist of
one or more identifier names separated by commas. Some valid declarations
are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; both declares and defines the variables i, j and k; which
instructs the compiler to create variables named i, j and k of type int.
Page 1 of 7
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
For definition without an initializer: variables with static storage duration are
implicitly initialized with NULL (all bytes have the value 0); the initial value of all
other variables is undefined.
Example
Try the following example where a variable has been declared at the top, but it
has been defined inside the main function −
#include <iostream>
using namespace std;
int main () {
// Variable declaration:
int a, b;
int c;
float f;
// actual initialization
a = 10;
Page 2 of 7
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
b = 20;
c = a + b;
f = 70.0/3.0;
cout << f << endl ;
return 0;}
When the above code is compiled and executed, it produces the following
result −
30
23.3333
Variable Scope
A scope is a region of the program and broadly speaking there are three
places, where variables can be declared −
Local Variables
Variables that are declared inside a function or block are local variables. 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 −
Page 3 of 7
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
// 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;
Page 4 of 7
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
// 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
Page 5 of 7
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
Defining Constants
int main() {
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;}
When the above code is compiled and executed, it produces the following
result −
50
Page 6 of 7
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
You can use const prefix to declare constants with a specific type as follows −
When the above code is compiled and executed, it produces the following
result −
50
Page 7 of 7