Title: CSE 105
1Introduction
- CSE 105
- Structured Programming Language (C)
- Presentation - 1
2Programming Languages
Machine Language
Binary code
Low level
Assembly Language
Pneumonic per binary code
Structured Programming Language (Fortran, Basic)
Program is a set of procedures
Mid level
Modular Programming Language (C, Pascal )
Application is set of Modules consisting of
related procedures
Object Oriented Programming Language (C, Java )
World is composed of entities objects
consisting of data and behavior
high level
Scripting Languages (Python, Awk)
Logic Programming (Prolog )
- English like construction
- Focus on task not on programming
- Application-specific
3Programming Languages
Machine Language
As we move down the hierarchy
- Portability ?
- Low level m/c dependent
- Mid level OS dependent
- High level m/c and OS independent
Low level
Assembly Language
Structured Programming Language (Fortran, Basic)
- Readability ?
- Programs are more English like
- Require less skilled programmer
Mid level
Modular Programming Language (C, Pascal )
Object Oriented Programming Language (C, Java )
- Responsibility on programmer ?
- Memory management (allocate and free)
- Type checking (runtime Vs compile-time
- Array bound checking
high level
Scripting Languages (Python, Awk)
Logic Programming (Prolog )
4An Example
m 4826
n 4828
p 482A
RAM
5C History
- Developed in the 1970s in conjunction with
development of UNIX operating system - When writing an OS kernel, efficiency is crucial
- This requires low-level access to the underlying
hardware - e.g. programmer can leverage knowledge of how
data is laid out in memory, to enable faster data
access - UNIX originally written in low-level assembly
language but there were problems - No structured programming (e.g. encapsulating
routines as functions, methods, etc.) code
hard to maintain - Code worked only for particular hardware not
portable
6Compile time Vs Run time
- Compile time
- Refers to the span of time while the program is
being examined and compiled by the compiler. - The compiler check for syntactical errors in the
program. - High level languages also try to identify
semantic errors. - Run time
- Refers to the span of time while the executable
(.exe) program has been brought to RAM from HDD
and is being executed. - The program has control over CPU, RAM, HDD and
other I/O devices. - If the program contains any semantic error then
it will most likely be revealed during run-time.
7C Characteristics
- C takes a middle path between low-level assembly
language - Direct access to memory layout through pointer
manipulation - Concise syntax, small set of keywords
- and a high-level programming language like
Java - Block structure
- Some encapsulation of code, via functions
- Type checking (pretty weak)
8C Dangers
- C is not object oriented!
- Cant hide data as private or protected
fields - You can follow standards to write C code that
looks object-oriented, but you have to be
disciplined will the other people working on
your code also be disciplined? - C has portability issues
- Low-level tricks may make your C code run well
on one platform but the tricks might not work
elsewhere - The compiler and runtime system will rarely stop
your C program from doing stupid/bad things - Compile-time type checking is weak
- No run-time checks for array bounds errors, etc.
like in Java
9Structure of a Simple C Program
include stdio.h include stdlib.h define
CONSTANT_NAME 4 void main(void) float
y0.1 int x100 char name50 printf(\nPr
ogram start) ...
Header files to be included
Definition of any constants used in program
Main C Program Section
Variable declaration and (optional)
initialisation
C Program Code
10Header File Section
- All C programs will contain this section
- Header files are used by the computer when it is
verifying the syntax of your program - In your C programs, you will use some standard C
commands (functions) that someone else has
written the code to implement - Examples are the functions to display text on the
screen (printf) and to read information typed in
from the keyboard (scanf) - The contents of the header files listed in your
program must contain a description of the valid
syntax for such C functions - At some point, you may write your own C functions
which can be used by any C program in which case
you will also have to create an associated header
file - The syntax for including a header file is
- include header file name
11Definition of Constants
- This is an optional section in your C file
- It is highly recommended that if you have to use
a constant value in your program that you define
it in this section - It could save you a lot of time if you ever need
to change its value (particularly if it occurs in
many places in a long program) - The syntax for the definition is
- define constant name constants value
12Definition of Constants
- It is good programming practice that constants
names should only contain CAPITAL LETTERS and the
_ (underscore) character - You can define integer, real or character
constants - define INTEGER_CONSTANT 101
- define REAL_CONTANT 3.1427
- define CHARACTER_CONSTANT hello
13Main C Program Section
- In simple C programs, this section is always
started with the line - void main(void)
- The beginning and end of the main section are
delimited by chain brackets - and - It is good programming practice to indent your
code in this section - If you have further sub-blocks of code then all
the lines of code in these sub-sections should be
further indented
14Main C Program Section
- It is good practice to include comments in you C
file - One comment at the top of the program should
provide a general overview of what the C file
does - Many comments within the file should explain the
various steps in the program - Comments can be placed either
- Within the bounds of / and /
- On a single line starting with //
15Separate compilation
- A C program consists of source code in one or
more files - Each source file is run through the preprocessor
and compiler, resulting in a file containing
object code - Object files are tied together by the linker to
form a single executable program
Preprocessor/ Compiler
Source code file1.c
Object code file1.obj
Preprocessor/ Compiler
Source code file2.c
Object code file2.oobj
Linker
Libraries
Executable code a.exe
16Separate compilation
- Advantage Quicker compilation
- When modifying a program, a programmer typically
edits only a few source code files at a time. - With separate compilation, only the files that
have been edited since the last compilation need
to be recompiled when re-building the program. - For very large programs, this can save a lot of
time.
17Compilation Process
Source code file1.c
Intermediate C code
Object code file1.obj
Preprocessor
Compiler
Your header file2.h
Header files stdio.h
Source code file2.c
Intermediate C code
Object code file1.obj
Preprocessor
Compiler
Libraries (e.g. stdio.lib)
Linker
Executable code a.exe
18An example
- Suppose you have module mymath.c and you want
to access its functions from mymain.c.
/ mymath.h / int sqr(int x)
Declarations imported in caller program
Declarations place in .h file
/ mymain.c / include mymath.h int main(void)
int a a sqr(10)
/ mymath.c / int sqr(int x) return x x
Linker
Compiler
Compiler
mymath.obj
mymain.exe
mymain.obj
contains binary code for sqr
contains complete code
Doesnt have binary code for sqr
19Execution Environment
CPU
Registers
Input Devices
ALU
CU
Output Devices
RAM
OS memomry
Disk drives
Stack
Heap
Your program
Variables / data
Executable code
Other Programs
20Execution Environment (alternate view)
RAM
File in HDD
OS memomry
fscanf, fread, fgets,
fprintf, fwrite, fputs,
Stack
Monitor
Function call
Your Program
printf, puts,
Heap
malloc, free
int, float,
Keyboard
scanf, gets,
Variables / data
Code is Executed here
Code resides here
Executable code
Other Programs
21References
- The C Programming Language , 2nd Edition,
Brian W. Kernighan Dennis M.Ritchie. - Course Home Page
- www.
-