0% found this document useful (0 votes)
6 views10 pages

5 - Constants in C++

Uploaded by

saeedafghan01234
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)
6 views10 pages

5 - Constants in C++

Uploaded by

saeedafghan01234
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/ 10

Constants in C++

Instructor: Samiullah Ehsas

1
Constant:
an entity that can not be changed during program execution.

Constants refer to fixed values that the program may not alter
and they are called literals.

Constants can be divided into two major categories


1. Primary Constant( integer constant, Real constant,
character constant)
2. Secondary Constant(Array, Pointer, Structure, Union,
Enum)
2
Rules for constructing Integer Constants
1. An integer constant must have at least one digit
2. It must not have a decimal point
3. It can be either positive or negative
4. If no sign precedes an integer constant, it is assumed to be positive
5. No commas or blanks are allowed within an integer constant
The allowable range for integer constant is -32768 to 32767
Example: 25, +134, 0, -600

3
Rules for constructing Real Constants
1. Real constants are often called Floating Point constants.
2. The real constants could be written in two forms—Fractional form
and Exponential form.
Rules for constructing Real Constants(fractional)
1. A real constant must have at least one digit
2. It must have a decimal point
3. It can be either positive or negative
4. Default sign is positive
5. No commas or blanks are allowed within a real constant
Example: 0.3, -890.7, +98.33, 0.08
4
Rules for constructing Character Constants

A character constant is a single alphabet, a single digit or a single special


symbol enclosed within single quotes

The maximum length of a character constant can be 1 character


Example:
‘A’, ‘l’, ‘5’, ‘=’, ‘ ’, ‘.’

5
Defining Constants:
There are two simple ways in C++ to define constants:

Using const keyword.


Using #define preprocessor.

1. The #define Preprocessor:

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


#define identifier_value

6
Following example explains it in detail:
Execute:
#include <iostream>
using namespace std;

#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
When the above code is compiled and executed, it
int main()
{
produces the following result:
50
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
7
return 0;
}
2. The const Keyword:
You can use const prefix to declare constants with a specific type as follows:

const type variable = value;

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


Following example explains it in detail:

8
#include <iostream>
using namespace std;
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
When the above code is compiled and executed, it
int area; produces the following result:

50
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0; 9
}
Thanks

10

You might also like