0% found this document useful (0 votes)
102 views15 pages

Intro to C Programming for Students

This document outlines a 3-month instructor-led course on programming, focusing on the C programming language to solve scientific problems. It includes learning outcomes, course outline, assessment methods, and teaching methodologies, aiming to provide students with fundamental programming skills. The course is designed for students in the field of software engineering at Machakos University College.

Uploaded by

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

Intro to C Programming for Students

This document outlines a 3-month instructor-led course on programming, focusing on the C programming language to solve scientific problems. It includes learning outcomes, course outline, assessment methods, and teaching methodologies, aiming to provide students with fundamental programming skills. The course is designed for students in the field of software engineering at Machakos University College.

Uploaded by

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

Introduction to Programming

Purpose of the module

In this 3 month instructor-led course, student shall learn the


fundamental skills for programming languages. Learners will use a
high level language (C programming language) to solve scientific
problem. The module provides a basic an entry point for students in
Machakos University College
the field of software engineering.
(A constituent College of Kenyatta University)
Learning Outcomes
P.O Box 136-90100, Machakos Kenya
Telephone: 044-21604  Email: [email protected] Website: By the end of the module, students will be able to:
http://www.machakosuniversity.ac.ke
 Understand the basic principles in programming

SCO102: introduction to programming  Understand problem solving strategies

 Understand the process of creation of a computer


Prepared by:
program and the different approach
Edward M Kioko
 Use high level language to code, write, compile, link
Email:[email protected]
and execute a program with emphasis on scientific
Tel:+254 (0) 725 695 782 applications
August, 2014

1
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming

Course outline programming language


 Translators
 Overview of C language
Programme : BSc Computer science
 Development environment and cycle
Module Name : introduction to programming  Syntax of a C program
Module Code : SCO 102
3 Program design and logic
Credit Hours : 2hours per week; Lab work 3 hours per
week Semester  Algorithm
 Flowcharts & Pseudo code
Semester : August-Dec 2014/2015
Course Leader : Edward Kioko 4 & Basic Elements of programming
5
Meeting hours : Friday 8:00-1:00  Identifiers and keywords
 Commenting in
Venue : Electrical Lab
 data types
E-mail : [email protected]
 Operators
Website : http://www.edwardchris.net
 Constants and variables
Program : Regular
6 & Control structures
Module Schedule (August –December 2014/2015) 7  Decision control structure
Wee Topic/Content Com  loop
k ment
Continuous Assessment Test
1&2 Student registration & introduction to the module
 Introduction to programming methodology
8 & 9 Modular programming in C( functions)
 Programming language levels
 Factors to consider while selecting a

2
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming

10 & Arrays in C Assignments 10%


11 Lab 20%
Exam: 60% .
Lab Practical 100%
Recommended References:
END OF SEMESTER EXAMINATIONS
1. Kerinignan, B. W., & Ritchie, D. M. (2010). The C
Teaching Methodologies:
programming Language (second Edition ed.). New jersey:
Lectures, demonstrations, group/class discussions and practical
Prentice Hall P T R.
exercises
2. King, K. C Programming: A Modern Approach.
Instructional Materials/Equipment:
Computers, projectors, whiteboards and writing materials.

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

2.6 Flow charts ........................................................................ 18


Contents
Chapter One: Introduction to programming ...................................... 6 2.7 Pseudocode........................................................................ 20
1.1 Chapter objectives ............................................................... 6 2.8 Control Structures/Logical Structures ............................... 21
1.2 Introduction to programming .............................................. 6 2.9 Chapter summary .............................................................. 27
1.3 Programming levels............................................................. 6 2.10 Chapter exercise ............................................................ 27
1.4 Factors to consider while selecting a programming 3.1 Chapter objectives ............................................................. 28
language ....................................................................................... 10 3.2 Introduction ....................................................................... 28
1.5 Translators ......................................................................... 11 3.3 Key words ......................................................................... 28
1.6 The development environment and the development cycle 3.4 Comments in C.................................................................. 28
13
Chapter three: Fundamentals of C programming ............................ 28
1.7 Overview of C language .................................................... 13
3.5 Data types in C .................................................................. 29
1.8 C program structure and syntax ........................................ 13
3.6 Variables ........................................................................... 30
1.9 Chapter summary .............................................................. 15
3.7.1 Numeric variables........................................................ 31
1.10 Chapter exercise ............................................................ 15
3.7.2 Character variables ..................................................... 31
1.11 Further reading suggestion ............................................ 15
3.7.3 Assignment ................................................................... 31
Chapter Two: Program Design Process and Logic .......................... 16
3.7 Constants in c programming ............................................. 32
2.1 Chapter objectives ............................................................. 16
3.8 Arithmetic operators ......................................................... 32
2.2 Introduction ....................................................................... 16
3.9 Escape sequence ................................................................ 32
2.3 Program development Lifecycle (PDLC) ......................... 16
3.10 Statements ..................................................................... 32
2.4 Programming design and logic tools ................................. 17
3.11 Casting Data Types ....................................................... 32
2.5 Algorithms ......................................................................... 17
3.12 Input and output............................................................. 33

4
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming

3.13 Formatted input–scanf ................................................... 33 5.4 Defining a function in C.................................................... 48


3.14 Character I/O — getchar& putchar ............................... 35 5.5 Function declaration .......................................................... 49
Chapter Four: Control structures in C .............................................. 36 5.6 Function calls .................................................................... 50
5.1 Introduction ....................................................................... 36 5.7 Parameters and arguments ................................................ 51
5.2 Chapter objectives ............................................................. 36 5.8 Recursive functions in C ................................................... 53
5.3 Boolean expressions and relational operators ................... 36 5.8.1 Number Factorial ........................................................... 54
5.4 Compound Boolean expressions using logical operators .. 36 5.8.2 Fibonacci Series ............................................................ 54
5.5 Control structures in C ...................................................... 37 5.9 Variable scope ................................................................... 55
5.6 Decision control structures ................................................ 37 5.9.1 Local Variables.............................................................. 55
4.5.1 Switch statement in C .................................................... 41 5.9.2 Global Variables ............................................................ 56
5.7 Repetitive control structures in C ...................................... 42 5.9.3 Formal Parameters ......................................................... 57
5.8 Chapter summary .............................................................. 46 5.10 Chapter summary .......................................................... 58
5.9 Chapter exercise ................................................................ 47 5.11 Chapter exercises ........................................................... 58
5.10 Further reading suggestions ........................................... 47 5.12 Further reading suggestions........................................... 58
Chapter Five: Functions in C ........................................................... 48
5.1 Introduction ....................................................................... 48
5.2 Chapter objectives ............................................................. 48
5.3 Benefits associated with Functions ................................... 48

5
The Catholic University of Eastern Africa
P.o Box 62157-00200, Nairobi Kenya
© Edward Kioko 2013
Introduction to Programming

We can therefore define a programming language as computer


Chapter One: Introduction to programming
language used by programmers to develop applications, scripts, or
1.1 Chapter objectives
By the end of this, learners should be able to: other set of instructions for a computer to execute.
 Understand programming levels It is a series of instructions that instruct the computer on how to
 Learn fundamental concepts underlying programming perform tasks
languages 1.3 Programming levels
 Understand the Program Development Life Cycle (PDLC) The figure below is intended to illustrate several key points about

 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

Programming Language program.


 The programmer has to remember a lot of codes to write a
program which might easily result in program errors.
 It is difficult to debug the program.
Low Level High level
Assembly Language
Instead of using a string of binary bits in a machine language,
assembly language use English-like words as commands that can be
Machine Language Assembly Language
easily interpreted by programmers. These commands are referred to
Procedural Object oriented as mnemonics. Examples of mnemonics in assembly language
include ADD, SUB, MUL, DIV.

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

It s relatively slow in speed since it goes through the entire program


and then translates the entire program into machine codes.
If a compiler runs on a computer and produces the machine codes
for the same computer then it is known as a self compiler or resident
compiler. On the other hand, if a compiler runs on a computer and
Translators are as follows:
produces the machine codes for other computer then it is known as a
a) Assembler
cross compiler.
b) Compiler
Compilers will show errors and warnings for the statements
c) Interpreter
violating the syntax rules of the language. They also have the ability
of linking subroutines of the program.
1.6.1 Assembler
1.6.3 Interpreter
They are programs that convert assembly code to equivalent
It translates high level languages into machine code. It translates
machine code. It will translate the symbolic codes of programs of an
only one statement of the program at a time. It reads only one
assembly language into machine language instructions. The
statement of program, translates it and executes it. Then it reads the
symbolic language is translated to the machine code in the ratio of
next statement of the program again translates it and executes it. In
one is to one symbolic instructions to one machine code instructions.
this way it proceeds further till all the statements are translated and
1.6.2 Compiler
executed.
It is a program which translates a high level language program into
On the other hand, a compiler goes through the entire program and
a machine language program. A compiler is more intelligent than an
then translates the entire program into machine codes. A compiler is
assembler. It checks all kinds of limits, ranges, errors etc. But its
5 to 25 times faster than an interpreter.
program run time is more and occupies a larger part of the memory.

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

printf function will be displayed on the monitor when the program is


run. Notice the semi-colon at the end of the line. C uses a semi-
colon as a statement terminator, so the semi-colon is required as a
signal to the compiler that this line is complete.
Line 6 The closing (right) brace marks the end of the main body of
the program.

1.10 Chapter summary


1.11 Chapter exercise
1.12 Further reading suggestion

15
Machakos University College
P.o Box 136-90100 Machakos|Tel:044-21604|Fax:044-20253
Website: www.machakosuniversity.ac.ke

You might also like