Week1 2
Week1 2
• 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
‘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 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++
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:
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
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++
variable-name = expression;
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. 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.
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);
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;
int x; scanf("%d",&x);
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
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);
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
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
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