Dr.
Ahmed AlKhazzar
Computer Science
Programming 2: C++ Types &
Variables
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 1
C++ Built-in Types
• A program can use several data
types to solve a given problem,
• for example, characters, integers,
or floating-point numbers.
• Since a computer uses different
methods for processing and
saving data, the data type must
be known. The type defines:
1. the internal representation of
the data, and
2. the amount of memory to
allocate.
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 2
Examples of basic data types
• bool (true or 1,false or 0)
• char (' a' , ' G' , ' 5' ,-128 to 127)
• short (-32768 to +32767)
• int (-32768 to +32767)
• long (-2147483648 to 2147483647)
• float (–3.4E+38) 6 digits accuracy
• double (–1.7E+308) 15 digits accuracy
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 3
void , signed and unsigned
Modifiers
• void: The void type is used for expressions that do
not represent a value. A function call can thus take a
void type.
• char
• int
• long
• unsigned char (0 to 255)
• unsigned int (0 to 65535)
• unsigned long (0 to 4294967295)
• const keyword can be used to create a “read only”
variable.
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 4
char and string constants
Constant Character ASCII Code
'A' Capital A 65
'a' Lowercase a 97
' ' Blank 32
'.' Dot 46
'5' Digit 5 53
'\0' Terminating null character 0
• A character constant is a character enclosed in single quotes.
• A string constant consists of a sequence of characters enclosed in
double quotes. Ex: "Today is a beautiful day!"
String literal: "Hello!"
'H' 'e' 'l' 'l' 'o' '\0’
Stored byte sequence:
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 5
Escape Sequences
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 6
A Sample Program
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 7
Definition and use of variables
#include <iostream>
using namespace std;
int gVar1; // Global variables,
int gVar2 = 2; // explicit initialization
int main() {
char ch ='A'; // Local variable being initialized
// or: char ch('A');
cout << "Value of gVar1: " << gVar1 << endl;
cout << "Value of gVar2: " << gVar2 << endl;
cout << "Character in ch: " << ch << endl;
int sum, number = 3; // Local variables with
// and without initialization
sum = number + 5;
cout << "Value of sum: " << sum << endl;
return 0;
}
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 8
Defining Variables
• A variable must be defined before you can use
it in a program.
• When you define a variable the type is
specified and an appropriate amount of
memory reserved.
SYNTAX: type name1 [,name2 ... ];
EXAMPLES: char c;
int i, counter;
double x, y, size;
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 9
Initializing a Variable
• A variable can be initialized, i.e. a value can be
assigned to the variable, during its definition.
• You can assign a value to a variable after defining
it in one of these two ways:
§ an equals sign ( = ) and an initial value for the variable
§ round brackets containing the value of the variable.
EXAMPLES: char c = 'a';
float x(1.875);
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 10
Global and Local variables
• a variable defined outside of each function is
global, i.e. it can be used by all functions.
• a variable defined within a function is local,
i.e. it can be used only in that function.
• If you do not initialize a global variable it
defaults to 0.
• If you do not initialize a local variable its value
will be random (undefined)
TRY OUT THIS IN THE LAB!
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 11
A Sample Program
// Circumference and area of a circle with radius 2.5
#include <iostream>
using namespace std;
const double pi = 3.141593;
int main()
{
double area, circuit, radius = 1.5;
area = pi * radius * radius;
circuit = 2 * pi * radius;
cout << "\nTo Evaluate a Circle\n" << endl;
cout << "Radius: " << radius
<< "Circumference: " << circuit
<< "Area: " << area;
return 0;
}
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 12
Constant Objects
Screen output
To Evaluate a Circle
Radius: 1.5
Circumference: 9.42478
Area: 7.06858
• The const keyword is used to create a “read only” variable.
• This means that it cannot be modified at a later stage
• A constant variable must be initialized during its definition.
EXAMPLE: const double pi = 3.1415947;
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 13
Exercise
Write a C++ program that two defines variables
for floating-point numbers and initializes them
with the values
123.456 and 76.543
Then display the sum and the difference of
these two numbers on screen.
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 14