0% found this document useful (0 votes)
58 views

Week1 2

The document discusses the translation process of high-level programming languages to machine code. It explains that source code is first compiled then linked together with libraries before being loaded into memory for execution. Key components like editors, preprocessors, compilers, linkers and loaders in the development environment are described.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Week1 2

The document discusses the translation process of high-level programming languages to machine code. It explains that source code is first compiled then linked together with libraries before being loaded into memory for execution. Key components like editors, preprocessors, compilers, linkers and loaders in the development environment are described.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 93

Introduction

Translation of high level language to machine code


Introduction
• There are several high level programming languages, include C, C++, Java,
Visual basic, FORTRAN, COBOL, etc.

• In this course, we discuss computer programming using ‘ C++’. C


programming language is designed by Dennis Ritche in 1973 and C++ is
designed by Bjarne Stroustrup in 1980 as an extension of C.

• Both C and C++ are designed in AT&T Bell Laboratories. C++ is an extension
of the ‘C’ language, which means that all the features supported by ‘C’ are
also supported by C++. Furthermore, C++ adds many useful features such
as object orientation and template management, which are not supported
by ‘C’.
Layered architecture of application program, system programs, and
computer hardware

Application Programs written


using high level languages to System Programs These are utility
deliver a specific business programs which are necessary to develop
requirement. High level and execute application programs. For
programming languages like C and example, operating system, compiler,
C++ are extensively used to design assembler, loader, linker, etc
application programs.
Computer System Architecture

‘Data’ and
‘instructions’ are
given as input to
the system so as to
perform a
particular operation
Input to the system
‘Data’ and ‘instructions’ are given as input to the system so as to
perform a particular operation.
Translation
The set of programs that perform a translation of high-level language
into a low-level language are called as system programs.
Processing unit
After the high-level language is translated into a language of 0’s and 1’s,
the machine starts running each instruction one by one, so as to generate
required output. The processing stage is called as ‘execution stage’ of the
program

The processing unit consists of two major blocks in it:


1. Arithmetic and logical unit (ALU)
2. Control unit

The ALU performs all the arithmetic and logical operations on the input data
whereas the control unit is a circuitry that manages and controls the overall
flow of data and instructions within the computer system.
Storage unit
The storage unit comprises of the memory devices where
instructions and data are stored. The area of the memory
where instructions of the program are stored is called as a ‘code
segment’ whereas the area of the memory where the data
required for the program is stored is called as a ‘data segment’
1. Primary memory (e.g. RAM)
2. Secondary memory (e.g. hard disk)
Hard disk or a secondary memory is generally a magnetic memory
that stores the information persistently. The primary memory or
RAM is a semiconductor memory that can store the information
only for the time when the computer is powered ON
C++ Development Environment

To translate the high-level language into the machine language every C++
program has to go through the set of system programs before the machine
can execute the code to generate the required output. The list of system
programs involved in the process of translating the high-level language into a
low-level language is as given below:

1. Editor
2. Pre-processor
3. Compiler
4. Linker
5. Loader
Editor
Editor provides an integrated development environment
(IDE) to create and develop a C++ program.
Some of the key facilities provided by editor include:
1. Creation of a new program
2. Editing an existing program
3. Debugging of a program
4. Initiate compilation and execution of the program
Pre-processor
• We can give an abbreviation for group of instructions to be
executed in a C++ program. Such an abbreviation is called as a
macro
• A macro pre-processor is a system program that resolves the
macro references by substituting a ‘macro name’ with its
‘definition’.
• The instructions within the program that are understood by the pre-
processor are called as pre-processor directives. The pre-processor
directives are generally prefixed by a symbol # in C++. Some of the pre-
processor directives are:
1. #include – The directive is used to include a header file in the C/C++
program.
2. #define – The pre-processor directive is used to define macros in the
C/C++ program
Compiler
The compiler is a system program that converts the source code into the
machine language. The machine language is also called as an object code,
which is generated as an output of the compilation process. The compiler
generates an ‘.obj’ file on the disk after the successful compilation of the source
code.

Linker
The ‘linker’ is a system program, which links the object code produced by the
compiler with the object code for the external libraries by resolving all the external
symbol references thereby filling the gaps present in the object file. Once all the
external references are resolved, the linker generates an executable code (which is
named as an ‘.exe’ file) on the disk
Loader
The linker generates the executable code (.exe file) on the disk. A loader is the
system program that loads the executable code in the main memory of the
computer system (in RAM) so that the code can be executed by the CPU.
Characteristics of C++

C++ is not a purely object-oriented language but a hybrid that contains


the functionality of the C programming language.
This means that you have all the features that are available in C:

■ universally usable modular programs


■ efficient, close to the machine programming
■ portable programs for various platforms.
Characteristics of C++

The large quantities of existing C source code can also be used in C++
programs. C++ supports the concepts of object-oriented programming
(or OOP for short), which are:

■ data abstraction, that is, the creation of classes to describe objects


■ data encapsulation for controlled access to object data
■ inheritance by creating derived classes (including multiple derived
classes)
■ polymorphism (Greek for multiform), that is, the implementation of
instructions that can have varying effects during program execution.
Traditional Procedural Programming
Data and functions (subroutines, procedures) are kept separate from the data
they process.
■ the programmer must ensure that data are initialized with suitable values
before use and that suitable data are passed to a function when it is called
■ if the data representation is changed, e.g. if a record is extended, the
corresponding functions must also be modified. Both of these points can lead to
errors and neither support low program maintenance requirements.
Object-oriented programming

Attention to the objects, OOP objects combine data (properties) and


functions (capacities).
A class defines a certain object type by defining both the properties and the
capacities of the objects of that type. Objects communicate by sending each
other “messages,” which in turn activate another object’s capacities.
Object-oriented programming

As an example, if we consider the development of a website for a particular


educational institute, we will be required to maintain data about the following
real world objects:

1. Students studying in the institute


2. Professors in the institute
3. Courses offered by the institute
4. Departments of the institute, etc.

we can create classes named as Student, Professor, Course and Department so as to


store representing each of the respective real world entities
Object-oriented programming

Example of classes in C++


Advantages of OOP Object-oriented programming

Offers several major advantages to software development:

■ reduced susceptibility to errors: an object controls access to its


own data. More specifically, an object can reject erroneous access
attempts

■ easy re-use: objects maintain themselves and can therefore be


used as building blocks for other programs

■ low maintenance requirement: an object type can modify its own


internal data representation without requiring changes to the
application.
The First C++ Program

Template for creating the main() function


let us create a first program that prints a text message on the computer screen

Source code to print a message on the computer screen


Following is applicable to C++ and not to C

The operator << is called as an insertion operator


Writing Comments
1. Single line comments (only supported by C++ and not by C
2. Multi-line comments (supported by both C and C++) A single line comment
starts with two slash symbols written back to back without any space in
between two slash symbols as shown below:
// This is a single line comment

• Single line commenting style is only supported by C++ but not by C


//This commenting style is only supported in C++

• Multi-line commenting style is supported by both C as well as C++ /*This


commenting style is supported by C as well as C++*/
Constants or Literals

A constant or a literal is a part of the program that cannot change its value.
The constants in C/C++ are classified as:

1. Integer constants
2. Real number constants
3. Character constants
4. String constants
Integer constants
These are numbers without a decimal point which can be written directly in
the program

10 /*This is a VALID integer constant*/


-95 /*This is a VALID integer constant which represents a negative value*/
45.9 /*This is an INVALID integer constant as there is a decimal point in the
number*/
25,000 /*This is an INVALID integer constant as there is a comma used in
between the digits of the number*/

Note that the integer constants must be written as a combination of digits 0


to 9 in the code without any special characters like comma, space in between
the digits.
Real constants

These are numbers with decimal point which can be written directly in the program
Real constants are also called as float constants or double constants in C/C++

10.5 /*This is a VALID floating point constant*/


-90.832 /*This is a VALID floating point constant*/
25,000.976 /*This is an INVALID floating point constant as there is a comma
used in between the digits of the number*/

Real constants must be written as a combination of digits 0 to 9 in the code


without any special characters like comma, space in between the digits.
Character constants

A character constant is a single symbol enclosed in the single quotes. Given


below are some valid and invalid examples of character constants in C/C++.

‘e’ /*This is a VALID character constant which represents a letter e*/


‘1’ /*This is a VALID character constant which represents a symbol 1*/
‘$’ /*This is a VALID character constant which represents a symbol dollar*/
‘ ’ /*This is a VALID character constant which represents a blank space*/
‘10’ /*This is an INVALID character constant as it encloses two different symbols (1
and 0) within the single quotes*/
‘ad’ /*This is an INVALID character constant as it encloses two different
symbols(letters ‘a’ and ‘d’) within the single quotes*/
Storing characters in memory
String constants
Collection of characters is called as a string. A string constant is formed
by enclosing multiple characters in double quotes.
"Computer" /*This is a VALID string constant which is a collection of eight
characters*/
"C" /*This is a VALID string constant which contains just a single character*/
'com' /*This is an INVALID string constant. This is because a string cannot be
enclosed in single quotes*/
"45+6" /*This is a VALID string constant which is a collection of 4 characters*/
Every string in C/C++ terminates with a special character called as a NULL
character, which is represented by ‘\0’
Variables and Data Types
A variable is a place holder for a particular value to be stored in the memory.
The process of creating a variable is called a ‘declaration’ of a variable whereas the
process of storing a particular value inside a variable is called as ‘initialization’ of the
variable.
Variables and Data Types
Declaration of variables

Given below is the syntax to declare a variable in C++:


<DataType> variableName;
For example, the statement
int marks;
Variable name is also called as an identifier.
For example, the below statement creates three different variables named as
marks, salary, and score of type float:
Initialization of variables

variable-name = expression;

For example, let us consider the two statements given below:

int age; /*Declaration of the variable named as ‘age’*/


age=10; /*Initialization of the variable ‘age’ with a value 10*/
Rules for naming variables

A variable name is a sequence of letters and digits. Given below are a set
of rules that the programmer must follow while naming variables:

1. Variable name cannot contain special characters such as comma,


semicolon, and space, however, we can have a underscore (symbol _)
as a part of the variable name.

1. The variable name cannot begin with a number. This means that the
first symbol of the variable name can be a letter or an underscore.

2. A variable name cannot be same as that of a built-in keyword of


C/C++.
Rules for naming variables

roll_no This is a VALID variable name.

Sum This is a VALID variable name.

Data12 This is a VALID variable name.

student’s This is an INVALID variable name, because the variable name cannot
contain any special characters like ‘.

8bat This is an INVALID variable name, because the variable name cannot start
with a number.

Roll no This is an INVALID variable name, because the variable name cannot
contain any special characters like SPACE.
List of escape sequences supported by C/C++
Format specifiers supported by C/C++

Format specifiers are used when we want to print a value of a particular variable
within the message to be printed on the screen

int x=3;
int y=5;
printf("There are %d students",x);

printf("There are %d students and %d professors",x,y);


Format specifiers supported by C/C++

int x=100;
printf("%6d",x);

float y = 100.56453;
printf("%6.2f",y);

printf("%-8.2f",y);
Examples of format specifiers
Examples of format specifiers
C++ Style of Printing the Value on Computer Screen

cout<<variable1<<variable2<<variable3………….<<variableN;
int a=10;
cout<<"The value of variable is "<<a;
will print the following message on the computer screen.
The value of variable is 10

int x=10,y=20;
cout<<"Value of x is:"<<x<<" Value of y is:"<<y;

will print the following message on the computer screen.


Value of x is:10 Value of y is:20
endl Modifier (Supported by C++ not by C)
endl is a keyword in C++ which is used to transfer the cursor on the new line
of the computer screen.
cout<<"I like C++"; cout<<"I like Computer programming";

cout<<"I like C++"<<endI; cout<<"I like Computer programming";


Accepting User Input Using scanf() Function (Supported by both C and C++)

scanf("control string", &var1, &var2,……….&varn);


The presence of & symbol before each variable indicates that the function
requires the exact memory location of each of the variables to store the input
data. & is called as an address-of-operator in C/C++, which gives the address of
the location in memory where the variable is stored.

int x; scanf("%d",&x);

int a,b; float c; scanf("%d %d %f",&a,&b,&c);

int e; char f; scanf("%d%c",&e,&f);


cin Object in C++ (Only in C++ not in C)

cin>>variable1>>variable2>>variable3 ... >>variableN;


Manipulator setw
The manipulator setw is used to set the width of the output which is generated
by the cout statement
Defining Constants using #define—A Pre-processor Directive
#define is a pre-processor directive which is used to define the constant values which
are not going to change throughout the execution of the program. The constants
defined by #define directive are also called as macros in the C/C++ program
NULL—a built-in macro

NULL is constant defined in C/C++ with a value set as zero. NULL is a name given
to a built-in macro which is defined inside C/C++ libraries using #define statement
shown below:

#define NULL 0

This means that we can use a symbol NULL or an integer constant 0


interchangeably in the C/C++ program.

The statement, int a = NULL; is same as , int a=0;


Character Specific Input/Output
C/C++ supports getchar() and putchar(), which are functions specifically used
for character input and output operations
Using the operators +, –, *, /and % in binary form
Precedence
Relational Operators

int a=10,b=20; int k=(a>=b);


REMEMBER Relational operators return Boolean values. Return value 1
represents true and return value 0 represents false.
Shorthand Operators
a=a+100; can be written in a shorthand notation as a+=100;

a=a*(b+c+d); will be equivalent to a*=(b+c+d);


Bitwise Operators in C/C++
The bitwise operators are used to support bit level operations with integer values.

int x = 0; x = ~x;
x = 0000 0000 0000 0000
x = 1111 1111 1111 1111
Increment/Decrement Operators
++Variable; /*usage of increment operator in a prefix form*/
Variable++; /*usage of increment operator in a postfix form*/
--Variable; /*usage of decrement operator in a prefix form*/
Variable--; /*usage of decrement operator in a postfix form*/

20
Order of Operations Evaluated by the printf()
Statement The printf() statement in evaluates all the operations specified in a
direction from right to left (RtoL).
#include void main()
{ int a=10;
printf("%d %d %d\n",a++,++a,--a);
printf("The last value of a is %d",a); }

The operations in cout are also executed from right to left as with that of
printf(). Hence the output of both the statements will be exactly identical.

printf("%d %d %d\n",a++,++a,--a);

cout<<a++<<" "<<++a<<" "<<--a<<endl;


Explicit Type Casting
A program to find the average of 5 numbers: An example
Scope Resolution Operator(::) Only in C++ not in C
• The scope resolution operator (::) is used to access the global value of
the variable when the variable with the same name is defined globally as
well as locally in the same C++ program.

• A global variable is a variable which can be accessed by all the functions


in a program whereas a local variable is a variable which can only be
accessed by the function in which the variable is defined. Given below is
the syntax of using the scope resolution operator

• in the unary form:


::variableName;
Creating blocks to define scope of variables
Decision Making Control Statements
if else Statement
Program to check if the number is even or odd: An example
Switch Statement
goto Statement
goto statement is used to implement an unconditional jump in a program.
Iterative Control Statements: Loops
Calculating factorial using while loop: An example
for Loop in C/C++

Program to compute the series 12+22+32+...x2 using for loop: An example


Creating Variables Local to Loops (Possible in C++ but not in C)
When a variable is declared within the loop, it is called local variable of the loop. Such
variables cannot be accessed outside the loop in any manner,
If you want to access the value of variable i even outside the loop, you should not
create it as a local variable of the loop.
Creating an Array
Given below is the syntax to create an array:
Initializing an array at the time of creation
Direct initialization
Specifying size of an array

NOTES Memory is allocated for a variable at the time of compilation of the


program, but its initialization happens at run time.
To find sum and average on n numbers: An example
To find sum and average on n numbers: An example
Program to arrange n numbers in ascending order: An example
Let us assume that the five values (user inputs) to be sorted are 15, 12,
19, 17 and 6 which will be stored at a[0], a[1], a[2], a[3] and a[4],
respectively

Comparison chart
Program to arrange n numbers in ascending order: An example
Let the variable i always represent the index of number above in the array and
let variable j represent the index of number below in the array
Program to arrange n numbers in ascending order: An example

Sorting process
Array of Characters

Similar to any other type of array, we can create an array of characters


as shown below:

char array_name[size];
Strings
An alternative approach to initialize array of characters as a string constant. A
string constant is always surrounded in double quotes. For example,
"computer"
Strings

C Notation C++ Notation


Calculating length of a string: An example
2D Arrays
The following statement creates an integer type matrix a of 3 rows and 4 columns a

In general, if the matrix has r rows and c columns the rows will be numbered as 0
to r-1 and columns will be numbered as 0 to c-1
Initialization of 2D Arrays
Initialization of 2D Arrays

printing all the elements of array a


Program to perform multiplication of matrix: An example
Two matrices a and b as input from the user and perform the multiplication of a
and b, storing the result in another matrix Res. Let the matrix a have r1 rows and
c1 columns and matrix b have r2 rows and c2 columns

You might also like