Mbeya University of Science and Technology: EEB 3107: Computer Programming Academic Year
Mbeya University of Science and Technology: EEB 3107: Computer Programming Academic Year
Technology
EEB 3107: Computer Programming
Academic year :2018/2019
Constants refer to fixed values that the program may not alter
and they are called literals.
EEB 3107_MUST 2
Integer Literals
EEB 3107_MUST 3
Floating-point Literals
EEB 3107_MUST 4
Boolean Literals
EEB 3107_MUST 5
Character Literals
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
You can break a long line into multiple lines using string
literals and separate them using whitespaces.
EEB 3107_MUST 8
String Literals
"hello, dear"
"hello, \
dear"
EEB 3107_MUST 9
Defining Constants
EEB 3107_MUST 10
The #define Preprocessor
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
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.
[DataType]
const = Constant value;
VarialbeName
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
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.
EEB 3107_MUST 20
Integral Type
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
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
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.
From To
EEB 3107_MUST 26
To declare j as short int and k as long int we write
short int j;
long int k;
EEB 3107_MUST 27
Unsigned integers
unsigned int k;
unsigned short k;
unsigned long n;
EEB 3107_MUST 28
Characters and integers
char = c;
You can make either of the following assignments:
c = ‘A’;
or
c = 65;
EEB 3107_MUST 29
Constants
• 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
3 003 0x3
8 010 0x8
15 017 0xf
16 020 0x10
21 025 0x15
EEB 3107_MUST 31
Floating-point constants
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
EEB 3107_MUST 33
String Constants
EEB 3107_MUST 34
Table 4: Typical range and size of basic data
types
Backslash Character Meaning
EEB 3107_MUST 35
Thank you for Listening! Questions
EEB 3107_MUST 36