Procedural Programming Using C++ Language: College of Engineering, Kerbala University
Procedural Programming Using C++ Language: College of Engineering, Kerbala University
1 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1 Data Types
1.2 Identifier Names
1.3 Variables
1.4 Operators
1.5 Precedence Summary
2 Console Input/Output . . . . . . . . . . . . . . . . 17
2.1 Reading and Writing Characters
2.2 Reading and Writing Strings
2.3 Formatted Console I/O
3 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.1 True and False in C
3.2 Selection Statements
3.3 Iteration Statements
3.4 Jump Statements
5 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.1 Functions
5.2 Recursion
6 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
6.1 Pointers
7 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
7.1 Structures
8 Files Input/Output . . . . . . . . . . . . . . . . . . . 75
8.1 Streams and Files
8.2 File System Basics
1. Expressions
This chapter examines the most fundamental element of the C++ language: the expression. Expres-
sions in C++ are substantially more flexible and powerful than in many other computer languages.
Expressions are formed from these atomic elements: data and operators, an example of expression
is shown Figure 1.1. Data may be represented by variables, and constants. Also, data has different
types: like intiger and float.
C++ provides a wide variety of operators, like: addition "+", subtraction "-", multiplication "*"
and division "/".
X= 5 + 3.2
operator
(addition)
16
16 bit integer range = 2 = 65636
For unsigned integer then
Range from 0 to 65635
Table ?? shows some valid data type supported by C++, along with their minimal ranges and
1.2 Identifier Names 7
Modifiers
The basic data types, except type void, may have various modifiers preceding them. A type modifier
alters the meaning of the base type to more precisely fit a specific need. The list of modifiers is
shown here:
• signed
• unsigned
• long
• short
Table ?? shows some valid data type combined with modifiers with their minimal ranges and
typical bit widths.
In C++, the names of variables are called identifiers. The length of these identifiers can vary
from one to several characters. The first character must be either a letter or an underscore (_).
The subsequent characters must be either letters, digits, or underscores (special characters are not
allowed). Table 1.3 shows some correct and incorrect identifier names:
8 Chapter 1. Expressions
C++ is a case sensitive language. This means that in an identifier, upper and lowercase are
treated as distinct. Therefore, count , Count, and COUNT are three separate identifiers.
An identifier cannot be the same as a standard C++ keyword and should not have the same
name as the functions that are in the C++ libraries.
1.3 Variables
A variable is a named location in memory that is used to hold a value that can be modified by the
program. All variables must be declared before they can be used. The general form of a declaration
is:
type variable_identifier;
Here, type must be a valid data type plus any modifiers, and variable_identifier may consist of
one or more identifier names separated by commas.
In the following, there are a number of variable declaration examples:
int x ;
int i , j , n ;
short int s i ;
unsigned i n t u i ;
double balance , p r o f i t , l o s s ;
You can give variables a value as you declare them by placing an equal sign and a constant after the
variable name. The general form of initialization is
type variable_name = constant;
in the following code, there are a number of variable declaration examples:
char ch = ’ a ’ ;
int f i r s t = 0;
double b a l a n c e = 1 2 3 . 2 3 ;
1.3 Variables 9
1.3.2 Constants
Constants refer to fixed values that the program may not alter. Constants can be of any of the basic
data type. Examples: integer constant = 1, float constant = 0.4, charater constant = ’a’.
C++ supports another type of constant: the string. A string is a set of characters enclosed in
double quotes ("). For example, ”this is a test" is a string. You must not confuse strings with
characters. A single character constant is enclosed in single quotes, as in ’a’. However, "a" is a
string containing only one letter.