Intro to C Programming for Students
Intro to C Programming for Students
1
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming
2
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming
Course Assessment:
I CAT: 10%
3
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming
4
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming
5
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming
Understand the factors to consider while selecting a programming languages. First, programming languages are built
programming language upon and relate directly to the underlying computer (hardware). In
Indentify the different parts of a C program fact, they are designed to control the operation of the hardware.
Able to write, debug and execute a simple C program Second, these programming languages can be divided into TWO
major groups; low and high level programming languages
1.2 Introduction to programming
Low-level Languages are machine-dependent i.e. must run
Users communicate with the computer in a language understood by on specific machines. They require extensive knowledge of
it. It needs to be instructed to perform all the tasks. The instructions computer hardware and its configuration. They are further
are provided in the form of computer programs, which are divided in to Machine language and Assembly language
developed by a programmer using a programming language. The High-Level Languages are machine-independent hence can
language, which the user employs to interact with the computer is ran on different types of machines
known as computer programming language. The process of using
such languages to instruct the computer is known
as programming or coding.
6
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
Machine Language
It’s the only language that is directly understood by the computer
therefore it does not need any translation. Its written as strings of 1′s
(one) and 0’s (zero. Program instruction may look like this:
1011000111101. It is considered to be first generation language.
Advantage of Machine Language
The only advantage is that program of machine language run very
fast because no translation program is required for the CPU.
Disadvantages of Machine Language
Figure 1: Levels of Programming Languages It is very difficult to program in machine language. The
programmer has to know details of hardware to write
7
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
Assembly language source must be translated into machine code by It is necessary to remember the registers of CPU and
a separate program known as assembler. The machine code can run mnemonic instructions by the programmer.
on its own at any time therefore the assembler and the source code Several mnemonic instructions are needed to write in
are no longer needed. assembly language than a single line in high-level language.
Thus, assembly language programs are longer than the high
If you make any changes to the source code, the assembler shall be language programs.
needed to recompile the program again and generate a new object 1.3.1.1 Advantages of Low-level Languages
Low-level languages have the advantage that they can be written to
program.
take advantage of any peculiarities in the architecture of the central
Advantages of Assembly Language
CPU. Thus, a program written in a low-level language can be
Mnemonics are easier to understand hence saving the
extremely efficient, making optimum use of both computer memory
programmer lot of time and effort.
and processing time.
It is easier to correct errors and modify program instructions.
1.3.1.2 Disadvantages of Low-level Languages
Assembly Language has the same efficiency of execution as
To write a low-level program takes a substantial amount of time, as
the machine level language. Because this is one-to-one
well as a clear understanding of the inner workings of the processor
translator between assembly language program and its
itself. Therefore, low-level programming is typically used only for
corresponding machine language program.
very small programs, or for segments of code that are highly critical
Disadvantages of Assembly Language:
and must run as efficiently as possible.
Its machine dependent hence a program written for one
1.3.1.1 High-Level Languages
computer might not run in other computers with different High-level programming languages allow the specification of a
hardware configuration i.e. there is no program portability. problem solution in terms closer to those used by human beings.
These languages were designed to make programming far easier,
8
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
less error-prone and to remove the programmer from having to know but the savings in programmer time generally far outweigh the
the details of the internal structure of a particular computer. These inefficiencies of the finished product. This is because the cost of
high-level languages were much closer to human language. writing a program is nearly constant for each line of code, regardless
Examples include Java, Python, Prolog e.t.c. of the language. Thus, a high-level language where each line of code
They are very convenient but very far removed from the computer translates to 10 machine instructions costs only one tenth as much in
they are running on. They are independent hence can run on program development as a low-level language where each line of
different types of machines code represents only a single machine instruction.
Programming paradigms 1.4 Web page program development tools
Procedural programming They are tools used to create web pages. Example of these tools
Programming instructions to tell the computer what to accomplish includes:
and how to accomplish it. Some common procedural programming HTML (Hypertext MarkUp Language) - it’s not actually a
languages include BASIC, COBOL, C among others e.t.c programming language but contains special codes used to
Object Oriented Approach create and format web pages. It usually uses tags that should
Structured programming doesn’t explain how to keep data and be kept as an ASCII file
programs together. However with OOP approach, data and programs Dynamic HTML(DHTML)-allows users to add more
(methods) are packaged into a single unit called an object. OOP are graphics and interactivity on the web page e.g. perform
event driven. Example of OOP languages include C ++, java and animations.
java applets Extensible MarkUp language (XML) allows web page
Advantages of High-level Languages developers to create tags that describe data pass to client so
High-level languages permit faster development of large programs. that the client can process the data rather than the server.
The final program as executed by the computer is not as efficient,
9
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
XHTML( eXtensible HTML) includes features of HTML them. The ease and clarity of a language depends upon its syntax. It
and XML should be capable enough to provide clear, simple and unified set of
WML (Wireless Markup Language) allows web page concepts. The vocabulary of the language should resemble English
developers to design pages specifically for micro browsers. It (or some other natural language). Symbols, abbreviations and
uses the Wireless Application Protocol (WAP) jargons should be avoided unless they are already known to most
Web page authoring which include Dreamweaver, people. Any concept that cannot be easily explained to amateurs
FrontPage, Joomla flash e.t.c. should not be included in the language.
1.5 Factors to consider while selecting a programming language Good Programming Language Features
The features of one programming language may differ from the Its definition should be independent of any particular hardware or
other. One can be easy and simple, while others can be difficult and operating system. It should support software engineering
complex. A program written for a specific task may have few lines technology, discourage or prohibit poor practices, and promote or
in one language, while many lines in the other. We judge the success support maintenance activities. It should support the required level
and strength of a programming language with respect to standard of system reliability and safety. If the language does not support the
features. basic requirements, then it is advisable to learn a new language and
then start the application development.
There a number of factors while selecting a programming just to
mention a few Nature of Application
Each language provides certain distinctive features, which separates
Ease of Use it from other languages. For example, Java should be used to write
It’s the most important factor in choosing a language. The language an application that runs over the Internet, whereas to develop a
should be easy in writing codes for the programs and executing system application, C and C++ are used.
10
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
Language Acquaintance Some applications e.g. railway or air reservation need to be quick as
In cases where a number of languages may suit the application to be
compared to batch processing applications such as payroll and
developed, the most familiar language to the programmers should be
inventory control. In such cases, the execution efficiency is
chosen.
important and the programmer should use assembly language
Ease of Learning
instead of high-level languages. The main reason behind this is that
Sometimes, it may necessity the programmer has to learn a new
assembly language usually has a shorter production time as
language in order develop an application. In such a case, the first
compared to high-level languages.
step is to find out which language, along with its alternatives, would
be suitable for the proposed application. Then, the programmer has 1.6 Translators
It’s a program that converts source statement into machine language.
to decide which language is easier to learn and use.
The source statements are just like English words. The computer
Control over Resources
Apart from the ease of use, the control factor should also be interprets the instructions as 1's and 0's. A program can be written in
considered while selecting a language. Easy to learn and use feature assembly language as well as in high-level language. This written
of the language does not mean that the application created with it program is called the source program. The source program is to be
will be the most powerful. Certain applications require more control converted to the machine language, which is called an object
over the hardware and hence the control factor should not be program
ignored. If the application developed by an easy language is not Translators can be classified as depicted below follows
efficient enough, then it is better to slug it out with a more complex
language. For example, Visual Basic provides an easier way to
develop an application, but it does not have the same control over
the resources as other languages such as Visual C++ or Delphi.
1.5.1 Speed Requirement
11
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
12
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
By the compiler, the machine codes are saved permanently for 3. Execute
future reference. On the other hand, the machine codes produced by The executable is loaded from the disk to memory and CPU
interpreter are not saved. An interpreter is a small program as executes the program one instruction at a time.
compared to compiler. It occupies less memory space, so it can be
1.8 Overview of C language
used in a smaller system which has limited memory space. It was developed in 1972 by Dennis Ritchie at the Bell telephone
laboratories for use with Unix operating system. It was designed for
1.7 The development environment and the development cycle
Programs goes through 3 main phases during development: editing system programming because of its low level memory management
(writing the program), compiling (i.e., translating the program to and its construct are close to the machine instructions.
executable code and detecting syntax errors) and running the
1.9 C program structure and syntax
program and checking for logical errors (called debugging). Every C program consists of a header and a main body and has the
1. Editing following structure.
This involves typing in the program with a text editor and making // Comment statements, which are ignored by computer
corrections if necessary. The program is stored as a text file on the #include <header file name >
disk Void main()
2. Compile {
The translator converts the source into machine language code declaration of variables ;
(object code), which it stores on the disk .A linker then links the statements ;
object code with standard library routines that the program may use return 0;
and creates an executable image, which is also saved on disk, }
usually as a file with the file name without any extension (e.g. We will consider each line of the above program for convenience
hello). the program is reproduced on the next page with line numbers to
13
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
allow us to comment on details of the program. However, you must execution is begun when the program is run. It does not have
not put line numbers in an actual program. to be the first statement in the program but it must exist as the
1. //sample program entry point. The use of the term main is actually a function
2. #include "stdio.h"
3. void main() definition. Functions must return a value and the term void is
4. {
used to denote that the main program does not return a value.
printf ("This is a line of text to output.\n");
5. } Following the ―main‖ program name is a pair of parentheses,
which are an indication to the compiler that this is a function.
The two braces are used to define the limits of the program
Line 1: At the top of the program are comments and instructions,
itself: in this case, the main function. The actual program
which will not be executed. Lines beginning with // indicate
statements go between the two braces.
that the rest of the line is a comment. Comments are inserted
Line 4 The opening (left) brace marks the beginning of the main
by programmers to help people read and understand the
body of the program. The main body consists of instructions,
program. Here the comment states the purpose of the program.
which can be declarations defining the data or statements on
Comments are not executed by the computer. They can in
how the data should be processed. All C declarations and
general be placed anywhere in a program.
statements must end with a semicolon.
Line 2: Lines beginning with # are instructions to the compiler’s
Line 5 printf is a predefined function. To sent output on the monitor,
pre-processor. They include instruction says ―what follows is a
the text is put within the function parentheses and bounded by
file name, find that file and insert its contents right here‖.
quotation marks. The definition of this function is defined in the
It is used to include the contents of a file of definitions that
STDIO library (the Standard Input/output library). To compile and
may be used in the program.
link this program the #include statement is required. The end result
Line 3 The word main is very important, and must appear once (and
is that whatever is included between the quotation marks in the
only once) in every C program. This is the point where
14
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke
Introduction to Programming
15
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke