0% found this document useful (0 votes)
63 views9 pages

Procedural Programming Using C++ Language: College of Engineering, Kerbala University

This document contains notes about procedural programming using the C++ language from the College of Engineering at Kerbala University. It was compiled by Assistant Professor Dr. Ali Fawzi Al-Shammari from various references on C++. The notes cover fundamental C++ concepts like data types, variables, expressions, input/output, statements, arrays, functions, pointers, structures, and file input/output. It provides examples and explanations of each concept.
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)
63 views9 pages

Procedural Programming Using C++ Language: College of Engineering, Kerbala University

This document contains notes about procedural programming using the C++ language from the College of Engineering at Kerbala University. It was compiled by Assistant Professor Dr. Ali Fawzi Al-Shammari from various references on C++. The notes cover fundamental C++ concepts like data types, variables, expressions, input/output, statements, arrays, functions, pointers, structures, and file input/output. It provides examples and explanations of each concept.
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/ 9

Procedural Programming using C++ Language

College of Engineering, Kerbala University

Assist. Prof. Dr. Ali Fawzi Al-Shammari


C++ L ANGUAGES N OTES .
C OLLEGE OF E NGINEERING , K ERBALA U NIVERSITY, 2019-2020.

Assist. Prof. Dr. Ali Fawzi Najm Al-Shammari

These notes are collected from the following references:

• C++: The Complete Reference, 4th Edition.


I
Part One

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

4 Arrays And Strings . . . . . . . . . . . . . . . . . . . 41


4.1 Arrays
4.2 Strings

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 "/".

variable constant data constant data


data (type: integer) (type: float)

X= 5 + 3.2

operator
(addition)

Figure 1.1: An example of expression: addition.


6 Chapter 1. Expressions

1.1 Data Types

C++ has five fundamental data types:


• character.
• integer.
• floating-point.
• double floating-point
• valueless.
These are declared using char, int, float, double, and void, respectively.
These types form the basis for several other types. The size and range of these data types
may vary among processor types and compilers. However, in all cases an object of type char is
1 byte. The size of an int is usually the same as the word length of the execution environment of
the program. For most 16-bit environments, such as DOS, an integer is 16 bits. For most 32-bit
environments, such as Windows 95/98/NT/2000, an integer is 32 bits. However, you cannot make
assumptions about the size of an integer if you want your programs to be portable to the widest
range of environments.

16
16 bit integer range = 2 = 65636
For unsigned integer then
Range from 0 to 65635

If it is signed integer, then


Range -32768 to 32767
Figure 1.2: 16 bit integer range.

int x; define a variable of name x and type integer

x=110; set value to the variable x


x=70000; if the integer is 16 bit, then this is wrong (out of range)
Figure 1.3: An example of define a variable of type integer.

Table ?? shows some valid data type supported by C++, along with their minimal ranges and
1.2 Identifier Names 7

typical bit widths.

Type Size in bit Minimal Range


char 8 -127 to 127
int 16 bit -32,768 to 32,767
int 32 bit -2147483648 to 2147483647
float 32 bit
double 64 bit

Table 1.1: Ranges of identifiers

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.

Type Size in bit Minimal Range


unsigned char 8 0 to 255
unsigned int 16 bit 0 to 65,535
unsigned int 32 bit 0 to 4294967295
short int 16 bit -32,767 to 32,767
unsigned short int 16 bit 0 to 65,535
long int 32 bit -2,147,483,647 to 2,147,483,647
unsigned long int 32 bit 0 to 4,294,967,295
unsigned long long int 64 bit 264 -1

Table 1.2: Modifiers ranges in C++

1.2 Identifier Names

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

Table 1.3: Some correct and incorrect names

Name Correct or not Reason


count Correct
1count Not Correct name start with number
test23 Correct
hi!there Not Correct a special character "!" used in name
high_balance Correct
_degree Correct
high.....balance Not Correct a special character "." used in name

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 ;

1.3.1 Variable Initializations

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.

You might also like