Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Mbeya University of Science and Technology: EEB 3107: Computer Programming Academic Year

The document discusses constants and literals in C++ programming. It defines constants as fixed values that cannot be altered by the program. Constants can be of basic data types like integers, floats, characters, strings, and booleans. Integer literals can be decimal, octal, or hexadecimal, with optional suffixes. Floating-point literals have integer, decimal, and exponent parts. Character literals are enclosed in single quotes, while string literals are enclosed in double quotes. Constants can be defined using the #define preprocessor or the const keyword.

Uploaded by

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

Mbeya University of Science and Technology: EEB 3107: Computer Programming Academic Year

The document discusses constants and literals in C++ programming. It defines constants as fixed values that cannot be altered by the program. Constants can be of basic data types like integers, floats, characters, strings, and booleans. Integer literals can be decimal, octal, or hexadecimal, with optional suffixes. Floating-point literals have integer, decimal, and exponent parts. Character literals are enclosed in single quotes, while string literals are enclosed in double quotes. Constants can be defined using the #define preprocessor or the const keyword.

Uploaded by

Peter
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Mbeya University of Science and

Technology
EEB 3107: Computer Programming
Academic year :2018/2019

Facilitator: Mr. Kyambille G


Literal – Constant Qualifiers

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

Constants can be of any of the basic data types and can be


divided into Integer Numerals, Floating-Point Numerals,
Characters, Strings and Boolean Values.

Again, constants are treated just like regular variables except


that their values cannot be modified after their definition.

EEB 3107_MUST 2
Integer Literals

An integer literal can be a decimal, octal, or hexadecimal


constant. A prefix specifies the base or radix: 0x or 0X
for hexadecimal, 0 for octal, and nothing for decimal.

An integer literal can also have a suffix that is a


combination of U and L, for unsigned and long,
respectively. The suffix can be uppercase or lowercase
and can be in any order

EEB 3107_MUST 3
Floating-point Literals

A floating-point literal has an integer part, a decimal


point, a fractional part, and an exponent part. You can
represent floating point literals either in decimal form or
exponential form.

While representing using decimal form, you must include


the decimal point, the exponent, or both and while
representing using exponential form, you must include the
integer part, the fractional part, or both. The signed
exponent is introduced by e or E.

EEB 3107_MUST 4
Boolean Literals

There are two Boolean literals and they are part of


standard C++ keywords −

A value of true representing true.

A value of false representing false.

You should not consider the value of true equal to 1 and


value of false equal to 0.

EEB 3107_MUST 5
Character Literals

Character literals are enclosed in single quotes. If the


literal begins with L (uppercase only), it is a wide
character literal (e.g., L'x') and should be stored in
wchar_t type of variable . Otherwise, it is a narrow
character literal (e.g., 'x') and can be stored in a simple
variable of char type.

EEB 3107_MUST 6
Character Literals

#include <iostream>
using namespace std;

int main() {
cout << "Hello\tWorld\n\n";
return 0;
}
When the above code is compiled and executed, it
produces the following result −
Hello World

EEB 3107_MUST 7
String Literals

String literals are enclosed in double quotes. A string


contains characters that are similar to character literals:
plain characters, escape sequences, and universal
characters.

You can break a long line into multiple lines using string
literals and separate them using whitespaces.

Here are some examples of string literal;

EEB 3107_MUST 8
String Literals

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

EEB 3107_MUST 9
Defining Constants

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

Using #define preprocessor.

Using const keyword

EEB 3107_MUST 10
The #define Preprocessor

Following is the form to use #define preprocessor to


define a constant −

#define identifier value

Following example explains it in detail −

EEB 3107_MUST 11
The #define Preprocessor

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

EEB 3107_MUST 12
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 −

EEB 3107_MUST 13
The const Keyword

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

EEB 3107_MUST 14
below. Note that if Data Type is omitted; it is considered as int by default.

Keyword Standard or user define data type: Numeric constant must be of


char, short, int, float, etc. type DataTyape as specified

[DataType]
const = Constant value;
VarialbeName

Figure 1: Constant Quantifiers

EEB 3107_MUST 15
Example
// 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;
}

EEB 3107_MUST 16
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.

EEB 3107_MUST 17
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

EEB 3107_MUST 18
Figure 2: Hierarchy of C++ data types

EEB 3107_MUST 19
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

EEB 3107_MUST 20
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;

EEB 3107_MUST 21
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.
 
 

EEB 3107_MUST 22
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.

EEB 3107_MUST 23
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.

EEB 3107_MUST 24
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 type 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

Typical range and size of these data types are given in table 1.

EEB 3107_MUST 25
Typical range and size of basic data types.

Type 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

EEB 3107_MUST 26
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.

EEB 3107_MUST 27
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;
EEB 3107_MUST 28
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.

EEB 3107_MUST 29
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.

EEB 3107_MUST 30
 
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

EEB 3107_MUST 31
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.

EEB 3107_MUST 32
Table 3: Valid and invalid floating-point
constants
Floating 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

EEB 3107_MUST 33
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.

EEB 3107_MUST 34
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

EEB 3107_MUST 35
Thank you for Listening! Questions

EEB 3107_MUST 36

You might also like