Lec 2 (How to write a program )
Lec 2 (How to write a program )
Text Editor
Preprocessor
Compiler
Linker
Loader
To convert C++ programs in to machine
language, we need:
Text Editor
The program is written using the text editor is known as source code.
The source code (human readable instructions) is saved on to the secondary
storage section of the computer system (disk) with an extension ‘.cpp’ to let
the compiler know that it is written in C++ language.
The source code needs to be grammatically correct .
What is a preprocessor?
Preprocessor: A program that modifies the source code by adding other
files and performing various text replacements
It executes automatically before the translation period starts.
Examples:
#include <iostream> adds a file called a header file to the source code. It
contains, among other things, the prototypes for cin and cout functions.
#define PI 3.141593 replaces all instances of PI in a program with 3.141593.
Compilation (step 3) follows immediately after the preprocessing, so, none
can access the modifications made by the preprocessor
What is a Complier?
Source Object
Code Compiler Code
.cpp .obj
What is a Linker?
Loader: The loader places the executable file on to the primary storage
location (RAM) of the computer system, from where the CPU executes the
program, instruction by instruction The linker pulls everything together,
makes sure that references to other parts of the program (code) are
resolved.
Example
Text Editor
/* hello.cpp */
#include <iostream>
int main() Preprocessor adds iostream text
{ (prototype for cout)
cout<<"hello world\n";
return 0;
Compiler converts to machine code
}
Linker
-add library
-add cout object code
-add object code for
other functions
hello.exe Executable
image
Compiler creates
Primary
Memory
4. Link Loader
6. Execute Primary
Memory
CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
.. executes.
..
..
11
12 Starting to write a simple C++
program.
Let us look at a simple code that would print the words Hello
World.
1 // Fig. 1.2: fig01_02.cpp
6 {
preprocessor directive
7 std::cout << "Welcome to C++!\n"; Message to the C++ preprocessor
8
Lines beginning with # are preprocessor
directives
9 return 0; // indicate that program #include <iostream> tells the preprocessor
ended successfully
10 }
to include the contents of the file <iostream>,
C++
whichprograms
includescontain one or
input/output more functions,
operations (such as
exactly
printingone of which
to the must be main
screen).
Welcome to C++! Parenthesis used to indicate a function
int means that main "returns" an integer value.
Prints the string ofMore
characters
in Lec contained
functions. between
return is one a way totheexit
quotation
a marks.
function. Left brace { begins the body of every
The
return 0, in this case, entire line, including std::cout, the <<
means function and a right brace } ends it
operator,
that the program terminated the string "Welcome to C++!\n" and
normally. the semicolon (;), is called a statement.
\
escape character
indicates that a “special” character is to be output
C++ Variables
Variables are boxes that can hold things
Each box has a name (“identifier”)
Size of the box depends on the “type” of
things you are planning to put there
You have to tell the compiler in advance
(“declare”),
Names of each of the boxes you want
The type of things that will go in each box
Why Use Types?
Computer sees everything in 1’s and 0’s
“Type” is how we interpret these patterns
What is 1101101?
Integer (int): it is 109
Character (char): it is ‘m’
Floating point (float): it is 1.53x10-43
Identifier sum
Box 75 Built in type
avg = (a + b)/2;
avg, a and b must be same type
Exception
It is OK to mix numerical types
i.e. int, float, double
Be careful not to loose precision
Some C++ Reserved Words
// entry point
int main() main: single C++ statement !
{
float w_lb, w_kg; A declaration statement
// entry point
int main()
{
float w_lb, w_kg;
Using calculator
4-2+5-1 = ?
+, - *, / and = are the operators
3+2*5 vs (3+2)*5
Need to consider operator precedence
* and / have higher precedence than +, -
C++ operators have these and more
C++ Operators
Operators
Binary Unary
Arithmetic Logical Bitwise Comparison Arithmetic
+ add && and & and < - less-than ++ increment
- sub || or | or > - gt.-than --decrement
* mul ^ xor <= - less-or-eq
/ div >= - gt-or-eq
% mod Copy == - equal
= Logical
!= - not-equal ! not
+=, -=, *=, /=, %=
Pointer * , &
Unary Operators
int a=9;
Negative: -a gives -9
Logical-invert: !a gives 0
* and & are pointer operations
Increment: ++
Decrement: --
More Unary Expressions
int a=9, b;
Pre-Increment :
b = ++a; b is 10 and a is 10
Post-Increment:
b = a++; b is 9 and a is 10
Pre-Decrement :
b = --a; b is 8 and a is 8
Post-Decrement:
b = a--; b is 9 and a is 8
Arithmetic Operators: + - * / %
Simple copy: a = b;
Overwrites the left object (l-value) with the result of the expression
on the right (r-value)
l-value must be writable
Copy with add
a += b; is the same as a = a + b;
Other copy-with-subtract, multiplication, division behaves the
same
a -= b; a *= b; a /= b; etc
Comparison Operators
3y
2*x*x-3*y/5*y+6 2x
2
y6
Should really be 5
2*x*x-(3*(y/5)*y)+6
2x 3y
2
(2*x*x-3*y)/(5*y+6)
5y 6
Expressions With Side Effects
r = x + y--; is equivalent to
r = x + y; y = y-1; (two actions!)
r = ++x - y; is equivalent to
x = x+1; r = x - y; (two actions!)
Keep it simple
Not r = --x + y++;
39