0% found this document useful (0 votes)
3 views34 pages

Chap 1 Basics

Uploaded by

muasyafrancis566
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)
3 views34 pages

Chap 1 Basics

Uploaded by

muasyafrancis566
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/ 34

Overview of structured

1
programming
Objectives:
 Definitions of basic terms used

 Understand structured programming

 Identify steps of creating a program

 Explain the characteristics of C

programming language
 Understand components of C.

 Create and run a simple C program

Jane Kuria 02/16/22


Inoorero University
Definition of basic terms
2

 Programming: Programming is the


translation of user ideas into a
representation or form that can be
understood by the computer. The tools
of writing programs are called
programming languages.
 Programming language:A programming
language is a set of rules used to write
computer programs. Like human
languages, computer languages have a
syntax
Jane Kuria
which defines
Inoorero the language
University
02/16/22
Structured Programming
3

 Structured programming is an approach to


writing programs that are easier to read, test,
debug and modify.
 The approach assists in the development of
large programs through stepwise refinement
and modularity.
 Programs designed this way can be developed
faster. When modules are used to develop
large programs, several programmers can work
on different modules, thereby reducing
program development time.
 structured programming serves to increase
programmer
Jane Kuria
productivity, program reliability
(readability and execution time),02/16/22
Inoorero University program
Steps in program
development
4

 Design program objectives


 It involves forming a clear idea in terms of the
information you want to include in the program,
computations needed and the output. At this stage,
the programmer should think in general terms, not in
terms of some specific computer language.

 Design program
 The programmer decides how the program will go
about its implementation, what should the user
interface be like, how should the program be
organized, how to represent the data and what
methods to use during processing. At this point, you
should also be thinking generally although some of
your decisions may be based on some general
characteristics
Jane Kuria of the C language.
02/16/22
Inoorero University
Steps in program
5
development…
 Develop the program

 Developing a program in a compiled language such as C requires at least


four steps:
 Editing (or writing) the program
 Compiling it
 Linking it
 Executing it

 Editing

 You write a computer program with words and symbols that are
understandable to human beings. This is the editing part of the
development cycle. You type the program directly into a window on the
screen and save the resulting text as a separate file. This is often referred
to as the source file The custom is that the text of a C program is stored in
a file with the extension .c for C programming language

Jane Kuria
02/16/22
Inoorero University
Steps in program
6
development…
 Compiling

 You cannot directly execute the source file. To run on any


computer system, the source file must be translated into binary
numbers understandable to the computer's Central Processing
Unit. This process produces an intermediate object file - with the
extension .obj.

 Linking

 The main reason for linking is that many compiled languages


come with library routines which can be added to your program.
Theses routines are written by the manufacturer of the compiler
to perform a variety of tasks, from input/output to complicated
mathematical functions. In the case of C the standard input and
output functions are contained in a library (stdio.h) so even the
most basic program will require a library function. After linking,
the file extension is .exe which is an executable file.
 Jane Kuria
02/16/22
Inoorero University
Steps in program
7
development…
 Executable files

 The text editor produces .c source files, which go to the compiler, which
produces .obj object files, which go to the linker, which produces .exe
executable file. You can then run .exe files as you run applications, simply
by typing their names at the DOS prompt or run using Windows menu.

 Test the program


 This involves checking whether the system does what it is supposed to do.
Programs may have bugs (errors). Debugging involves the finding and
fixing of program errors.

 Maintain the program


 Occasionally, changes become necessary to make to a given program. You
may think of a better way to do something in a program, a clever feature
or you may want to adapt the program to run in a different machine.
These tasks are simplified greatly if you document the program clearly
and follow good program design practices.

Jane Kuria
02/16/22
Inoorero University
C as a programming
8
language
 C has been standardized(ANSI C) and
spawned new language C++, Stroustrap,
that improve C
 Its basic characteristics include
 Small in size
 Loose typing(lots of freedom, error
prone)
 Structured(extensive use of functions)
 designed for systems programming (i.e.,
low level programming of the type
needed
Jane Kuria to implement an operating 02/16/22
system) Inoorero University
C as a programming
9
language
 C is higher level than assembler but still
close to the hardware and allows direct
manipulation of many system aspects:
pointers, memory allocation, bitwise
manipulation ...
 C is not very far from the assembler
language but it provides higher level
language constructs (functions, data
structures)that facilitate programming
without loosing too much performance
Jane Kuria
02/16/22
Inoorero University
Advantages of C Language
10

 C Supports structured programming design features


 It allows programmers to break down their programs
into functions. It also supports the use of comments,
making programs readable and easily maintainable.

 Efficiency
 C is a concise language that allows you to say
what you mean in a few words. The final code
tends to be more compact and runs quickly.

 Portability
 C programs written for one system can be run with
little or no modification on other systems.

Jane Kuria
02/16/22
Inoorero University
Advantages of C language
11

 Power and flexibility


 Power
 C has been used to write operating systems (such as Unix and
Windows), Language Compilers, Assemblers ,Text Editors, Print
Spoolers, Network Drivers, Application packages (such as
WordPerfect and Dbase), Language Interpreters, Utilities, et al.

 Flexibility

 It has (and still is) been used to solve problems in areas such as
physics and engineering.

 Programmer orientation

 C is oriented towards the programmer’s needs. It gives access to


the hardware. It lets you manipulate individual bits of memory. It
also has a rich selection of operators that allow you to expand
programming capability.
Jane Kuria
02/16/22
Inoorero University
C Program components
12

 Keywords
 Preprocessor directives
 Functions
 Statements
 Declaration statements
 Comments
 Assignment and Expression statements
 Input and output statements

Jane Kuria
02/16/22
Inoorero University
Sample Program
13

1. #include<stdio.h>
2. Int main()
3. {
4. int num; / define a variable called num */
5. num = 1; / assignment /
6. printf(“This is a simple program ”);
7. printf(“to display a message. \n”);
8. printf (“My favorite number is %d because ”,
num);
9. printf(“ it is first.\n ”);
10. return 0;
11. }

Jane Kuria
02/16/22
Inoorero University
1.Keywords
14

 These are reserved words that have


special meaning in a language. E.g.
num, return, main, int…from the above
program
 The compiler recognizes a keyword as
part of the language’s built – in syntax
and therefore it cannot be used for any
other purpose such as a variable or a
function name.
 C keywords must be used in
lowercase otherwise they will not be
recognized.
Jane Kuria
Inoorero University
02/16/22
2. Pre-processor directives and
15
header files
 <include stdio.h>A preprocessor directive
performs various manipulations on your source
file before it is actually compiled.
 Preprocessor directives are not actually part of
the C language, but rather instructions from
you to the compiler
 The preprocessor directive #include is an
instruction to read in the contents of another
file and include it within your program.
 This is generally used to read in header files for
library functions ( See line 1 in sample
program above).
Jane Kuria
02/16/22
Inoorero University
Header files
16

 Header files contain details of functions


and types used within the library. They
must be included before the program
can make use of the library functions.
 Library header file names are enclosed
in angle brackets, < >. These tell the
preprocessor to look for the header file
in the standard location for library
definitions.

Jane Kuria
02/16/22
Inoorero University
3. Functions
17

 In C, a function is a named subroutine


that can be called by other parts of the
program. Functions are the building
blocks of C.
 All C programs consist of one or more
functions, each of which contains one or
more statements.
 Although a C program may contain
several functions, the only function that
it must have is main( ).
Jane Kuria
02/16/22
Inoorero University
3. Functions…main()
18

 The main( ) function is the point at


which execution of your program begins.
That is, when your program begins
running, it starts executing the
statements inside the main( ) function,
beginning with the first statement after
the opening curly brace. Execution of
your program terminates when the
closing brace is reached.

Jane Kuria
02/16/22
Inoorero University
3.Functions…library
19
functions
 The ANSI C standard specifies a minimal set
of library functions to be supplied by all C
compilers, which your program may use.
E.g. printf()
 This collection of functions is called the C
standard library. The standard library
contains functions to perform disk I/O (input
/ output), string manipulations,
mathematics, and much more.
 When your program is compiled, the code
for library functions is automatically added
to your program.

Jane Kuria
02/16/22
Inoorero University
4. Statements
20

 Statements are usually contained within a


function.
 A statement specifies an action to be
performed by the program i.e. statements
are parts of your program that actually
perform operations.
 All C statements must end with a
semicolon. C does not recognize the end of
a line as a terminator. This means that
there are no constraints on the position of
statements within a line.
Jane Kuria
02/16/22
Inoorero University
5. Comments
21

 Comments are non – executable program


statements meant to enhance program
readability and allow easier program
maintenance, i.e. they document the program.
They can be used in the same line as the
material they explain (see lines 4 and 5 in
sample program).

 A long comment can be put on its own line or


even spread on more than one line. Comments
are however optional in a program. Everything
between the opening /* and closing */ is
ignored by the compiler
Jane Kuria
02/16/22
Inoorero University
6. Declaration Statements
22

 In C, all variables must be declared


before they are used.
 Variable declarations ensure that
appropriate memory space is reserved
for the variables, depending on the data
types of the variables.
 Line 4 is a declaration for an integer
variable called num.

Jane Kuria
02/16/22
Inoorero University
7. Assignments and
23
Expressions statements
 An assignment statement uses the
assignment operator “=” to give a
variable on the operator’s left side the
value to the operator’s right or the result
of the expression on the right.
 The statement num =1; (Line 5) is an
assignment statement.

Jane Kuria
02/16/22
Inoorero University
Input and output
24
statements
 C language uses printf to give output in
a given program as shown in line 6.
 It uses scanf to get input from the
keyboard that is to be used in the
program.
 The input and output will be covered
later in the course unit.

Jane Kuria
02/16/22
Inoorero University
Program errors and
25
debugging
There are three types of errors:
 Syntax errors

 Semantic errors

 Logic errors.

Jane Kuria
02/16/22
Inoorero University
Syntax errors
26

 They result from the incorrect use of the


rules of programming.
 The compiler detects such errors as soon
as you start compiling.
 A program that has syntax errors can
produce no results.
 You should look for the error in the line
suggested by the compiler.

Jane Kuria
02/16/22
Inoorero University
Examples of syntax errors
27

 Missing semi colon at the end of a


statement e.g. Area = Base * Length
 Use of an undeclared variable in an
expression
 Illegal declaration e.g. int x, int y, int z;
 Use of a keyword in uppercase e.g.
FLOAT, WHILE
 Misspelling keywords e.g. init instead of
int.

Jane Kuria
02/16/22
Inoorero University
Logic Errors
28

These occur from the


 incorrect use of control structures

 incorrect calculation

 omission of a procedure

Jane Kuria
02/16/22
Inoorero University
Examples of logic errors
29

 An indefinite loop in a program


 Generation of negative values instead of
positive values.
 The compiler will not detect such errors
since it has no way of knowing your
intentions.
 The programmer must dry run the
program so that he/she can compare the
program’s results with already known
results.
Jane Kuria
02/16/22
Inoorero University
Semantic errors
30

 They are caused by illegal expressions


that the computer cannot make meaning
of.
 Usually no results will come out of them
and the programmer will find it difficult
to debug such errors. Examples include
a data overflow caused by an attempt to
assign a value to a field or memory
space smaller than the value requires,
division by zero, etc.

Jane Kuria
02/16/22
Inoorero University
Examples of semantic
31
errors
 a data overflow caused by an attempt to
assign a value to a field or memory
space smaller than the value requires
 division by zero, etc.

Jane Kuria
02/16/22
Inoorero University
Guidelines to good C
32
programming
 Ensure that your program logic design is
clear and correct. Avoid starting to code
before the logic is clearly set out .
Declare all variables before using them.
 Use sensible names for variables. Use of
general names such as n instead of
net_sal for net salary makes variables
vague and may make debugging
difficult. Use a variable name in the case
that it was declared in.

Jane Kuria
02/16/22
Inoorero University
Guidelines to good C
33
programming….
 Use a variable name in the case that it was
declared in.
 Never use keywords in uppercase.
 Terminate C declarations, expressions and
input/output statements with a semi colon.
 Always save your program every time you
make changes.
 Proof read your program to check for any
errors or omissions
 Dry run you design with some simple test
data before running the code, then
compare the two.

Jane Kuria
02/16/22
Inoorero University
Exercise
34

 Write a program to output your name,


admission number, stage and semester.
 Write a program to output the sum of
two numbers.

Jane Kuria
02/16/22
Inoorero University

You might also like