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

Cob 2202: Computer Programming 2: Facilitator: Mr. Kyambille G

This document discusses constants and literals in C++ programming. It defines literals as symbolic constants used for readability. C++ supports constants using #define, enumerated data types, and the const keyword. Variables declared with const cannot be modified once initialized. The document also summarizes different data types in C++ like integer, floating point, character, and void and provides examples of declaring variables of each type.

Uploaded by

Luke Reuz
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)
47 views

Cob 2202: Computer Programming 2: Facilitator: Mr. Kyambille G

This document discusses constants and literals in C++ programming. It defines literals as symbolic constants used for readability. C++ supports constants using #define, enumerated data types, and the const keyword. Variables declared with const cannot be modified once initialized. The document also summarizes different data types in C++ like integer, floating point, character, and void and provides examples of declaring variables of each type.

Uploaded by

Luke Reuz
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/ 26

COB 2202: COMPUTER

PROGRAMMING 2

FACILITATOR: MR. KYAMBILLE G

1
LITERAL – CONSTANT QUALIFIERS
Literals are constants to which symbolic names are associated for
the purpose of readability and ease of handling standard constant
values. C++ provides the following three ways of defining constants:
#define preprocessor directive
enumerated data types
const keyword

The variables in C can be created and initialized with a constant


value at the point of its definition. For instance, the statement

float PI = 3.1452;

2
Defines a variable named PI, and is assigned with the
floating-point numeric constant value 3.1452. It is known
that the constant value does not change. In the above case,
the variable PI is considered as a constant, whose value
does not change throughout the life of the program
(complete execution-time).

However, an accidental change of the value of the variable


PI is not restricted by C. C++ overcomes this by supporting a
new constant qualifier for defining a variable, whose value
cannot be changed once it is assigned with a value at the
time of variables definition. The qualifier used in C++ to
define such variable is the const qualifier. The syntax of
defining variables with the constant qualifier is shown below
figure 1 below. Note that if Data Type is omitted; it is
3
considered as int by default.
4
The following examples illustrate the declaration of the constant
variables:

const float PI = 3.1452;

const int TRUE = 1;

const char *book_name = “OOPs with C++”;

Example 1.8 is the program, area.cpp that illustrates the


declaration and the use of constant variables.

5
EXAMPLE 1.8 

// AREA.CPP: AREA OF A CIRCLE

#INCLUDE <IOSTREAM.H>

CONST FLOAT PI = 3.1452;

MAIN()

FLOAT RADIUS;

FLOAT AREA;

COUT << “ENTER RADIUS OF CIRCLE:”;

CIN >> RADIUS;

AREA = PI * RADIUS * RADIUS;

COUT << “AREA OF CIRCLE = “ << AREA;

6
ENTERING THE VALUE OF RADIUS AS 2 AND RUNNING THE PROGRAM, THE OUTPUT WILL BE AS
FOLLOWS:
 
ENTER RADIUS OF CIRCLE: 2
AREA OF CIRCLE = 12.5808
 
IN THE ABOVE PROGRAM, THE USE OF THE STATEMENT SUCH AS
 
PI = 2.3;
 
TO MODIFY A CONSTANT TYPE VARIABLE LEADS TO THE COMPILATION ERROR: CANNOT MODIFY A
CONST OBJECT.
7
DATA TYPES, OPERATORS AND EXPRESSIONS IN C++

Data types

The kind of data that variables may hold in a programming


language is called data types. Data types in C++ can be broadly
classified in three categories depicted in figure 2.

User-defined data type

Built-in data type

Derived data type

8
Figure 2: Hierarchy of C++ data types

9
THE USE-DEFINED DATA TYPE ENABLES THE PROGRAMMER TO INVENT HIS/HER
OWN DATA TYPES AND DEFINES WHAT VALUES IT CAN TAKE ON.
 
DERIVED DATA TYPES ARE BUILT FROM THE BASIC INTEGER AND FLOATING-
POINT DATA TYPES. THE ARRAY DATA TYPE IS ONE EXAMPLE OF DERIVED DATA
TYPES. AN ARRAY CAN HOLD SEVERAL VALUES OF THE SAME TYPE, UNDER ONE
VARIABLE NAME.

BUILT-IN DATA TYPE

THERE ARE THREE BUILT-IN DATA TYPES AVAILABLE IN C++


INTEGRAL TYPE
FLOATING TYPE
VOID

10
INTEGRAL TYPE
 

THIS CAN BE FURTHER CLASSIFIED INTO


INT
CHAR 
INT IS THE BASIC DATA TYPE. IT IS TREATED AS AN INTEGER IN THAT CANNOT HOLD FRACTIONAL
VALUES. 
CHAR IS A DATA TYPE WHICH CAN HOLD BOTH THE CHARACTER DATA OR THE INTEGER DATA. FOR
EXAMPLE AFTER MAKING THE DECLARATION  
CHAR C; 
YOU CAN MAKE EITHER OF THE FOLLOWING ASSIGNMENTS: 
C = ‘A’;
C = 65;

11
IN BOTH CASES THE CHARACTER A IS LOADED INTO THE CHARACTER VARIABLE C.

ON THE OTHER HAND AFTER MAKING THE DECLARATION

INT C;

YOU CAN MAKE EITHER OF THE FOLLOWING

C = ‘A’;

C = 65;

ON BOTH CASES THE DECIMAL VALUE 65 IS LOADED INTO THE INTEGER VARIABLE C.

12
FLOATING TYPE
Floating type can further be classified into:
float
double 
float means floating point data-type and represent fractions such as
0.356
0.000001
To declare a variable capable of holding one of these values you use float
or double keywords. 
double stands for double precision. It is capable of holding a real number
ranging from 1.7 x 10 –308 to 1.7 x 10 308 which gives twice as much
precision as presented by a float. The precision refers to the number of
decimal places that can be represented.

13
VOID TYPE

 
VOID DATA TYPE HAS TWO IMPORTANT PURPOSES:

TO INDICATE THAT THE FUNCTION DOES NOT RETURN A VALUE

TO DECLARE A GENERIC POINTER VARIABLE

FOR EXAMPLE YOU MAY SEE A FUNCTION DEFINITION SUCH AS:

VOID FUNC(A, B) 

THIS INDICATES THAT A FUNCTION DOES NOT RETURN ANY USEFUL VALUE. LIKEWISE, ON THE CALLING SIDE YOU
WOULD DECLARE A FUNC() AS:

EXTERN VOID FUNC();

THIS INFORMS THE COMPILER THAT ANY ATTEMPT TO USE THE RETURNED VALUE FROM FUNC() IS A MISTAKE AND
SHOULD BE FLAGGED AS AN ERROR. FOE EXAMPLE, YOU COULD INVOKE FUNC() AS FOLLOWS:

FUNC(X, Y); 

BUT YOU CANNOT ASSIGN THE RETURNED VALUE TO A VARIABLE.

14
VARIABLE DECLARATIONS
Every variable must be declared before being used. Declaration provides
the compiler with about how many bytes should be allocated and how
those bytes should be represented. Usually declarations of the same
data trype are grouped together. For example
 
int j, k;
float x, y, z;

The word int and float are reserved words specifying the integer data
type and real data type. There are nine reserved words for data types in
C++ as given below:

int char float double short signed void long unsigned


15
Typical range and size of these data types are given in table 1.
TYPICAL RANGE AND SIZE OF BASIC
Type
DATA TYPES.
Range Byte Represents

  From To    

char/short -128 128 1 characters

unsigned char 0 225 1 characters

Int -32,768 32,768 2 whole numbers

unsigned int 0 65,535 2 whole numbers

long -2,147,438,648 2,147,438,648 4 whole numbers

unsigned long 0 4,294,967,295 4 whole numbers

Float 3.4 x 10-38 3.4 x 1038 4 fractional numbers

double 1.7 x 10-308 1.7 x 10308 8 fractional numbers

long double 3.4 x 10-4932 3.4 x 104932 10 fractional numbers

16
To declare j as short int and k as long int we write

short int j;
long int k;

If you wish to store integer values less than –32,768 or grater that 32,
768, you should use declaration
long int.

If you need to store integer values in the range of –32,768 and 32,768,
then you can use short int.

17
UNSIGNED INTEGERS
In a situation where a variable is to hold non-negative values such as
counting things, such variables are restricted to non-negative
numbers (or unsigned), thereby doubling its positive range. Looking at
table 1 we note that signed short int has a range of -32,767 to
32,767 whereas an unsigned short int has a range from 0 to 65,535.

To declare an integer variables as being non-negative only, use the


unsigned qualifier as shown below:

unsigned int k;

unsigned short k;

unsigned long n; 18
CHARACTERS AND INTEGERS
C++ makes a distinction between numeric and character data. The
data type char can be used to hold either characters or numbers. For
example, after you make the declaration

char = c;
You can make either of the following assignments:

c = ‘A’;
or
c = 65;

Note that character constants are enclosed in single quotes. The


quotes tell the compiler to get the numeric code (ASCII code value) of
the character. 19
CONSTANTS
C++ supports three types of character constants namely:

• Integer constants
• Floating-point constants
• String constants

Integer constants
 Integer constants are values, which are mostly used by programmers
and computer users. A computer can also uses octal and hexadecimal
constants. Octal constants are written by preceding the octal value
with the digit zero. A hexadecimal constant is written by preceding the
value with zero and an x or X. Table 2 illustrates integer constants in
octal and hexadecimal equivalences.
20
 
TABLE 2: INTEGER CONSTANTS
Decimal Octal Hexadecimal

3 003 0x3

8 010 0x8

15 017 0xf

16 020 0x10

21 025 0x15

-87 -0127 -0x57

21
FLOATING-POINT CONSTANTS
Because an integer data type is inadequate for presenting very large

or very small numbers, floating point data types are therefore

necessary. Table 3 illustrates valid and invalid floating-point constants.

22
TABLE 3: VALID AND INVALID FLOATING-
Floating point
Constants
POINT
Comment Remarks

CONSTANTS
3.1429 Valid  

.4444444 Valid  

0.4 Valid  

3e2 Valid  

5E-2 Valid  

3.7e12 Valid  

35 Invalid No decimal point or exponent

3,500.25 Invalid Commas are illegal

6E Invalid The exponent must be followed by a number

3e2.5 Invalid The exponent value must be an integer


23
STRING CONSTANTS
A sequence of zero or more characters enclosed by double quotes is
called a string constant. An example of a string is:

“My name is Juma”


 
A blank string constant is simply represented by “ “.

C++ support one more special character constant called Backslash


character constant

The backslash character (\) alters the meaning of the character that
follows it. Thus the character ‘n’ when typed after the backslash, i.e. \n,
will mean to print a new line. Table 4 gives a list of backslash
character strings and the action they produce. 24
TABLE 4: TYPICAL RANGE AND SIZE OF
BASIC DATA TYPES
Backslash Character Meaning

\a (alert) Produce an alert (or a bell)

\b (backspace) Move the cursor back one space

\f (form feed) Move the cursor to the next page

\n (new line) Print a new line

\r (carriage return) Print a carriage return

\t (horizontal tab) Prints a horizontal tab

\v (vertical tab) Prints a vertical tab


25
Thank you for Listening!

26

You might also like