theory is when you know something, but it doesn't work. practice is when something works, but...
TRANSCRIPT
Theory is when you know Theory is when you know something, but it doesn't work. something, but it doesn't work. Practice is when something works, Practice is when something works, but you don't know why.but you don't know why.
Programmers combine theory and Programmers combine theory and practice: Nothing works and they practice: Nothing works and they don't know why.don't know why.
--unknown--unknown
Once a new technology starts Once a new technology starts rolling, if you're not part of the rolling, if you're not part of the steamroller, you're part of the steamroller, you're part of the road. road.
--Stewart Brand--Stewart Brand
Nabil Ur RehmanBICSE [email protected]
Zeeshan AhmadBICSE [email protected]
Jahanzeb [email protected]
Bibrak [email protected]
Aqsa [email protected]
Introduction to C Introduction to C ProgrammingProgramming
Lecture 2Lecture 2
Today’s LectureToday’s Lecture Software CategoriesSoftware Categories
System SoftwareSystem Software Application SoftwareApplication Software
Introduction to ‘C’ LanguageIntroduction to ‘C’ Language HistoryHistory EvolutionEvolution JustificationJustification
Development Environment of ‘C’Development Environment of ‘C’
There are two main categories of softwareThere are two main categories of software
System softwareSystem software
Application Software Application Software
Evolution of Evolution of programming languagesprogramming languages
The lack of portability between The lack of portability between different computers led to the different computers led to the development of development of high-level languageshigh-level languages——so called because they permitted a so called because they permitted a programmer to ignore many low-level programmer to ignore many low-level details of the computer's hardware details of the computer's hardware
Details of procedural, non-procedural Details of procedural, non-procedural will follow in the lectureswill follow in the lectures
How people used to How people used to programprogram Machine Language….. Damn! It was Machine Language….. Damn! It was
difficultdifficult Assembly Language…. Remember ADD? Assembly Language…. Remember ADD?
– Required too much user involvementRequired too much user involvement– To much to remember To much to remember – Less semanticLess semantic
C Language C Language – B Language.. Bell LabsB Language.. Bell Labs– Improved to C LanguageImproved to C Language– Is a compiled languageIs a compiled language
ANSI CANSI C
Tools of the Tools of the tradetrade
EditorEditor Interpreter and Compilers Interpreter and Compilers DebuggersDebuggers
Integrated Development EnvironmentIntegrated Development Environment(IDE)(IDE)
It containsIt contains EditorEditor Compilers Compilers DebuggerDebugger Linkers Linkers LoadersLoaders
Preprocessor programprocesses the code.
Loader puts program in memory.
CPU takes eachinstruction and executes it, possibly storing new data values as the program executes.
Compiler creates object code and storesit on disk.Linker links the objectcode with the libraries
Loader
Primary Memory
Compiler
Editor
Preprocessor
Linker
Primary Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
CPU
Disk
DiskProgram is created in the editor and stored on disk.
#include <iostream.h>#include <iostream.h>main ( )main ( ){{
cout << “ Welcome to SEECS “;cout << “ Welcome to SEECS “;}}
VariableVariable
VariableVariable X
VariableVariable Pic of the memoryPic of the memory
2525
1032310323
namename
of the of the variablvariablee
VariableVariableVariable starts withVariable starts with
1.1. CharacterCharacter
2.2. Underscore _ Underscore _ (Not (Not Recommended)Recommended)
VariableVariable Small post boxSmall post box
X
VariableVariable
Variable is the name of a Variable is the name of a location inlocation in
the memorythe memory
e.g. x= 2;e.g. x= 2;
VariableVariableIn a program a variable In a program a variable
has:has:
1.1. NameName
2.2. TypeType
3.3. SizeSize
4.4. ValueValue
Assignment Assignment OperatorOperator
==x = 2x = 2
X 2
Assignment Assignment Operator Operator
L.H.S = R.H.S.L.H.S = R.H.S.
X+ 3 = y + 4 X+ 3 = y + 4 WrongWrongZ = x +4Z = x +4
x +4 = Z x +4 = Z WrongWrong
X = 10 ; X = 10 ;
X = 30 ;X = 30 ;
X 10
X 30
X = X + 1;X = X + 1;
X 10 + 1
= X11
Data typeData type
int i ; int i ; //Declaration line//Declaration line
ii
Writing C programWriting C program
Compiler converts human
readable language to a
language which is
understandable by the operating system/hardware
Examples of C/C++
compilers of today:
Visual C++GCC/G++
DJGPP (open source for
windows like GCC)
Borland CTurbo (obsolete
and not recommended)
3 Stages of 3 Stages of CompilationCompilation
Stage 1: Stage 1: PreprocessingPreprocessing
– Performed by a program called the Performed by a program called the preprocessorpreprocessor
– Modifies the source code (in RAM) according to Modifies the source code (in RAM) according to preprocessor directives (preprocessor preprocessor directives (preprocessor commandscommands) embedded in the source code) embedded in the source code
– Strips comments and white space from the Strips comments and white space from the codecode
– The source code as stored on disk is The source code as stored on disk is notnot modified.modified.
3 Stages of Compilation (con’t)3 Stages of Compilation (con’t)
Stage 2: Stage 2: CompilationCompilation
o Performed by a program called the Performed by a program called the compilercompilero Translates the preprocessor-modified source Translates the preprocessor-modified source
code into code into object code (machine code)object code (machine code)o Checks for Checks for syntax errorssyntax errors and and warningswarningso Saves the Saves the object codeobject code to a disk file, if to a disk file, if
instructed to do so (we will not do this).instructed to do so (we will not do this).o If any compiler errors are received, no object code If any compiler errors are received, no object code
file will be generated.file will be generated.o An object code file An object code file willwill be generated if only be generated if only
warnings, not errors, are received.warnings, not errors, are received.
Object code:Object code:
It is machine language code containing It is machine language code containing various calls specific to operating various calls specific to operating system… e.g the object code written by system… e.g the object code written by compiler is not only hardware compiler is not only hardware dependent but also operating system dependent but also operating system dependent. dependent.
So if you have linux and windows both So if you have linux and windows both operating systems then object file of operating systems then object file of compiled by one Operating System (OS) compiled by one Operating System (OS) will not get executed on the other OSwill not get executed on the other OS
3 Stages of Compilation 3 Stages of Compilation (con’t)(con’t)Stage 3: Stage 3: LinkingLinking
o Combines the program object code with other Combines the program object code with other object code to produce the executable file.object code to produce the executable file.
o The other object code can come from the The other object code can come from the Run-Run-Time LibraryTime Library, other libraries, or object files , other libraries, or object files that you have created.that you have created.
o Saves the executable code to a disk file. On the Saves the executable code to a disk file. On the Linux system, that file is called Linux system, that file is called a.outa.out..
o If any linker errors are received, no executable file If any linker errors are received, no executable file will be generated.will be generated.
Program Development Program Development Using gccUsing gcc
Source File pgm.c
Program Object Code File pgm.o
Executable File a.out
Preprocessor
Modified Source Code in RAM
Compiler
Linker
Other Object Code Files (if any)
Editor
#include <iostream.h> #include <iostream.h> //This is pre-processor directive//This is pre-processor directivevoid main ( ) void main ( ) //this tells the starting point of your program//this tells the starting point of your program{{
int x ;int x ;int y ;int y ;int z ;int z ;x = 10 ;x = 10 ;y = 20 ;y = 20 ;z = x + y ;z = x + y ;
cout << " x = " ; cout << " x = " ; //print the text on monitor//print the text on monitorcout << x ;cout << x ;
cout << " y = " ;cout << " y = " ;cout << y ;cout << y ;
cout << " z =x + y = " ;cout << " z =x + y = " ;cout << z ;cout << z ;
}}
int x, y, z ;int x, y, z ;
int x; int y; int z int x; int y; int z ;;