0% found this document useful (0 votes)
22 views7 pages

Lec-7-Variable Define Constant

Uploaded by

Lhovie Gayo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Lec-7-Variable Define Constant

Uploaded by

Lhovie Gayo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

POLYTECHNIC COLLEGE OF LA UNION

INFORMATION TECHNOLOGY DEPARTMENT

VARIABLES, DEFINE & CONSTANT

IT2-Lecture

Variable Definition in C++

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.

Variables can be initialized (assigned an initial value) in their declaration. The


initializer consists of an equal sign followed by a constant expression as follows −

type variable_name = value;

Some examples are −

int d = 3, f = 5; // declaration of d and f.

Page 1 of 7
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT

int d = 3, f = 5; // definition and initializing d and f.

byte z = 22; // definition and initializes z.

char x = 'x'; // the variable x has the value 'x'.

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.

Variable Declaration in C++

A variable declaration provides assurance to the compiler that there is one


variable existing with the given type and name so that compiler proceed for
further compilation without needing complete detail about the variable. A
variable declaration has its meaning at the time of compilation only, compiler
needs actual variable definition at the time of linking of the program.

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;

cout << c << endl ;

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 −

 Inside a function or a block which is called local variables,

 In the definition of function parameters which is called formal parameters.

 Outside of all functions which is called global variables.

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

#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;

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

There are two simple ways in C++ to define constants −

 Using #define preprocessor.

 Using const keyword.

The #define Preprocessor

Following is the form to use #define preprocessor to define a constant −

#define identifier value

Following example explains it in detail −

#include <iostream>using namespace std;


#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'

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

The const Keyword

You can use const prefix to declare constants with a specific type as follows −

const type variable = value;

Following example explains it in detail −

#include <iostream>using namespace std;


int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
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

Note that it is a good programming practice to define constants in CAPITALS.

Page 7 of 7

You might also like