[go: up one dir, main page]

0% found this document useful (0 votes)
17 views21 pages

L1 Intro

Uploaded by

Reaz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views21 pages

L1 Intro

Uploaded by

Reaz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction

CSE 105
Structured Programming Language
(C)
Presentation - 1

CSE, BUET CSE-105 – Structured Programming


Programming Languages
Machine Language Binary code
Low level
Assembly Language Pneumonic per binary code
Structured
Programming
Language (Fortran, Program is a set of procedures
Mid level Basic)
Modular Programming Application is set of Modules –
Language (C, Pascal ) consisting of related procedures
Object Oriented
World is composed of entities
Programming
objects – consisting of data and
Language (C++, Java )
behavior
high level Scripting Logic
Languages Programm • English like construction
(Python, ing • Focus on task not on
Awk) (Prolog ) programming
• Application-specific

2
CSE, BUET CSE-105 – Structured Programming
Programming Languages
Machine Language • Portability 
Low level • Low level : m/c dependent

As we move down the hierarchy


Assembly Language • Mid level : OS dependent
Structured • High level: m/c and OS independe
Programming
Language (Fortran, • Readability 
Mid level Basic) • Programs are more English like
Modular Programming
• Require less skilled programmer
Language (C, Pascal )
Object Oriented • Responsibility on
Programming
programmer 
Language (C++, Java ) • Memory management
high level Scripting Logic (allocate and free)
Languages Programm • Type checking (runtime Vs
(Python, ing compile-time
Awk) (Prolog ) • Array bound checking

3
CSE, BUET CSE-105 – Structured Programming
An Example
C code Intel 80x80 Intel 80x80
Assembly Machine Code
language
int m, n, p; MOV AX, n A1 4828
m = n + p; ADD AX, p 01 06 482A
MOV m, AX A3 4826

m: 4826
n: 4828
p: 482A

RAM
4
CSE, BUET CSE-105 – Structured Programming
C: 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
5
CSE, BUET CSE-105 – Structured Programming
Compile 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.
6
CSE, BUET CSE-105 – Structured Programming
C: 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)
7
CSE, BUET CSE-105 – Structured Programming
C: Dangers
C is not object oriented!
Can’t “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
8
CSE, BUET CSE-105 – Structured Programming
Structure of a Simple C Program
#include “stdio.h” Header files to be included
#include “stdlib.h”

Definition of any constants


#define CONSTANT_NAME 4 used in program

void main(void) Main C Program Section


{
float y=0.1; Variable declaration and
int x[100]; (optional) initialisation
char name[50];

printf(“\nProgram start”);
C Program Code
………..…….
}
9
CSE, BUET CSE-105 – Structured Programming
Header 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”
10
CSE, BUET CSE-105 – Structured Programming
Definition 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 constant’s value

11
CSE, BUET CSE-105 – Structured Programming
Definition of Constants
It is good programming practice that constant’s
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”

12
CSE, BUET CSE-105 – Structured Programming
Main 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

13
CSE, BUET CSE-105 – Structured Programming
Main 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 //

14
CSE, BUET CSE-105 – Structured Programming
Separate 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

Preprocesso
Source code Object code
r/
file1.c Compiler file1.obj

Preprocesso
Source code Object code
r/
file2.c Compiler file2.oobj

Libraries
Linker

Executable
code
a.exe
15
CSE, BUET CSE-105 – Structured Programming
Separate 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.

16
CSE, BUET CSE-105 – Structured Programming
Compilation Process
Source code Preprocess Intermediate Compile Object code
file1.c or C code r file1.obj

Your header
Header files file2.h
stdio.h

Source code Preprocess Intermediate Compile Object code


file2.c or C code r file1.obj

Libraries
Linker
(e.g.
stdio.lib)

Executable
code
a.exe
17
CSE, BUET CSE-105 – Structured Programming
An example
Suppose you have module “mymath.c” and you
want to access its functions from “mymain.c”.

Declarations place in /* mymath.h */ Declarations imported in


.h file int sqr(int x); caller program

/* mymain.c */
/* mymath.c */
#include “mymath.h”
int sqr(int x) {
int main(void) {
return x * x;
int a;
}
a = sqr(10);
}
Compile Linker
r
Compile
r
mymath.obj
mymain.exe
contains binary code mymain.obj
for sqr contains complete code
Doesn’t have binary
18
CSE, BUET CSE-105 – Structured Programming
code for sqr
Execution Environment
CPU

Input Devices Registers

ALU CU
Output Devices
RAM

OS memomry
Disk drives
Stack Heap
Your
Variables / data
program
Executable code

Other Programs

19
CSE, BUET CSE-105 – Structured Programming
Execution Environment (alternate view)
RAM
File in
OS memomry HDD
fprintf, fwrite, fscanf, fread,
Stack fputs, … fgets, …
Fun
c tion Monitor
call
printf, puts,
malloc, free …
Heap Your Program
oa t, …
f l
int, scanf, gets, Keyboard
s Code is …
Variables / data de Executed here
esi
r e
e
d her
o CPU
C
Executable code
Registers
Other Programs
ALU CU
20
CSE, BUET CSE-105 – Structured Programming
References
“The C Programming Language” , 2nd
Edition,
Brian W. Kernighan & Dennis M.Ritchie.
Course Home Page
www.

21
CSE, BUET CSE-105 – Structured Programming

You might also like