Chap 1 Basics
Chap 1 Basics
1
programming
Objectives:
Definitions of basic terms used
programming language
Understand components of C.
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
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
Linking
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.
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
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
Flexibility
It has (and still is) been used to solve problems in areas such as
physics and engineering.
Programmer orientation
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
Jane Kuria
02/16/22
Inoorero University
3. Functions
17
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
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
Jane Kuria
02/16/22
Inoorero University
Examples of syntax errors
27
Jane Kuria
02/16/22
Inoorero University
Logic Errors
28
incorrect calculation
omission of a procedure
Jane Kuria
02/16/22
Inoorero University
Examples of logic errors
29
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
Jane Kuria
02/16/22
Inoorero University