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

Final Notes C Programming

This document provides a comprehensive overview of basic programming skills in C, outlining the course objectives and topics to be covered. Students will learn about programming concepts, data types, functions, and control structures, as well as practical applications of C programming. The document serves as a guide for students to develop their programming abilities through structured lessons and assessments.

Uploaded by

nkomofaraijnr
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)
3 views147 pages

Final Notes C Programming

This document provides a comprehensive overview of basic programming skills in C, outlining the course objectives and topics to be covered. Students will learn about programming concepts, data types, functions, and control structures, as well as practical applications of C programming. The document serves as a guide for students to develop their programming abilities through structured lessons and assessments.

Uploaded by

nkomofaraijnr
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

ABSTRACT

This is a document designed to


equip students with a
comprehensive understanding of
basic programming skills.
Primrose Washinyira
C PROGRAMMING

C PROGRAMMING
1201
1 C PROGRAMMING (1201)

AIM:
To provide the student with a comprehensive understanding of basic
programming skills.

MODULE OBJECTIVES
By the end of the course students should be able to:

2.1 explain the systems approach to programming


2.2 analyze data and expressions used in C programming
2.3 use input/output functions to develop programs in C
2.4 use conditional statements and loops in developing programs in C

2.5 qualify and apply functions in developing programs in C

2.6 differentiate arrays and pointers in relation to C programming

2.7 construct subroutines using structures and unions

2.8 employ bitwise operators in C programs

2.9 test and debug program


2 C PROGRAMMING (1201)

TOPICS TO BE COVERED
3 C PROGRAMMING (1201)

Contents
AIM: ............................................................................................................................................................... 1
MODULE OBJECTIVES .................................................................................................................................... 1
TOPICS TO BE COVERED ................................................................................................................................ 2
1. CHAPTER ONE ........................................................................................................................................7
1.1 OBJECTIVES ....................................................................................................................................8
1.2 C PROGRAMMING ......................................................................................................................... 9
1.2.0 C BASICS ........................................................................................................................................9
1.2.1 WHY C? ......................................................................................................................................... 9
1.2.2 Applications of C Programming ....................................................................................................9
1.2.4 Programming languages .............................................................................................................10
1.2.5 LANGUAGES ................................................................................................................................10
1.2.6 COMPILER ...................................................................................................................................11
1.3 WHAT IS C PROGRAMMING? ............................................................................................................ 12
1.3.1 C PROGRAM ................................................................................................................................12
1.3.2 Developing a C program ............................................................................................................. 12
1.3.3 Basics of C programming ............................................................................................................13
1.4.1 C PROGRAMMING BASICS ..........................................................................................................14
1.4.2 C BASICS CODING GROUND ........................................................................................................14
1.4.5 C program ................................................................................................................................... 15
1.5 CONSTANTS ....................................................................................................................................... 16
1.5.1 Syntax to show the use of constants. .........................................................................................17
1.6 Files in C language ............................................................................................................................. 18
1.6.1 Header file .................................................................................................................................. 18
1.7 BASIC FUNCTIONS IN C PROGRAMMING ...................................................................................... 19
1.7.1 DESIGNING YOUR FIRST PROGRAM ........................................................................................... 23
1.8 DATA TYPES AND VARIABLES ........................................................................................................ 25
1.8.1 INPUT AND OUTPUT FUNCTIONS ...............................................................................................28
1.8.2 Keywords .................................................................................................................................... 30
1.8.3 Variable name ............................................................................................................................ 31
1.8.4 Rules for constructing variable names .......................................................................................32
1.9 GETCHAR () FUNCTION ...................................................................................................................... 33
4 C PROGRAMMING (1201)

1.10 PUTCHAR () FUNCTION ....................................................................................................................34


1.11 Comments in C Programming ......................................................................................................... 35
1.11.1 Ways of commenting ................................................................................................................... 35
1.12 FLOWCHARTS, ALGORITHMS AND PSEUDO CODES. .......................................................................36
1.12.1 Flow chart .................................................................................................................................36
1.12.2 Algorithm ..................................................................................................................................37
1.12.3 Pseudo code ............................................................................................................................. 38
1.13 Practical Questions for Chapter 1 ................................................................................................... 40
1.14 End of Chapter 1 Questions ............................................................................................................ 41
2. CHAPTER TWO .................................................................................................................................42
2.1 OBJECTIVES ........................................................................................................................................43
2.1.1 OPERATORS ................................................................................................................................ 44
2.1.2 CLASSES OF OPERATORS ............................................................................................................ 44
2.2 ARITHMETIC OPERATORS .................................................................................................................. 45
2.3 LOGICAL OPERATORS ........................................................................................................................ 47
2.4 ADVANCED LOGICAL OPERATORS ..................................................................................................... 49
2.5 RELATIONAL OPERATORS .................................................................................................................. 50
2.6 ASSIGNMENT OPERATORS ................................................................................................................ 52
2.7 INCREMENTAL/DECREMENTAL ......................................................................................................... 53
2.8 BITWISE OPERATORS .........................................................................................................................55
2.9 TENARY OPERATORS ......................................................................................................................... 57
2.10 End of Chapter Two Assessment Questions ................................................................................... 58
3. CHAPTER THREE .............................................................................................................................. 59
3.1 OBJECTIVES ........................................................................................................................................60
3.1.1 CONTROL STATEMENTS ..............................................................................................................61
3.2 Conditional statements ..................................................................................................................... 61
3.2.1 If statement ................................................................................................................................ 62
3.2.2 IF ELSE .........................................................................................................................................64
3.3.3 Else if .......................................................................................................................................... 66
3.2.4 Switch ......................................................................................................................................... 69
3.3 LOOPING STATEMENTS ..................................................................................................................... 74
3.3.1 Looping statements….while ....................................................................................................... 75
5 C PROGRAMMING (1201)

3.3.3 For loop ...................................................................................................................................... 78


3.3.4 The Infinite Loop .........................................................................................................................85
3.4 JUMP STATEMENTS ........................................................................................................................... 86
3.4.1 Break statement ......................................................................................................................... 86
3.4.2 Continue ..................................................................................................................................... 88
3.5 End of Chapter 3 Assessment Questions .......................................................................................... 91
4. CHAPTER FOUR : FUNCTIONS IN C ................................................................................................. 93
4.1 OBJECTIVES ........................................................................................................................................94
4.2 FUNCTIONS ........................................................................................................................................95
4.2.1 Function types ............................................................................................................................ 97
4.3 LOCAL DECLARATION ........................................................................................................................ 99
4.4 GLOBAL DECLARATION ....................................................................................................................101
4.5 End of Chapter 4 Assessment Questions ........................................................................................ 104
[Link] FIVE : ARRAYS ..................................................................................................................... 105
5.1 OBJECTIVES ......................................................................................................................................106
5.3 Arrays ...............................................................................................................................................107
5.4 TWO DIMENSIONAL ARRAYS ...........................................................................................................111
5.5 End of Chapter 5 Assessment Questions ........................................................................................ 114
CHAPTER 6: POINTERS ...........................................................................................................................115
6.1 OBJECTIVES ......................................................................................................................................116
6.2 Pointers ........................................................................................................................................... 117
6.3 PASSING ARRAY ELEMENTS TO FUNCTIONS ................................................................................... 120
6.4 END OF CHAPTER 6 ASSESSMENT QUESTIONS ............................................................................... 123
7. CHAPTER SEVEN: STRING .............................................................................................................. 124
7.1 OBJECTIVES ......................................................................................................................................125
7.2 STRINGS ........................................................................................................................................... 126
7.3 STANDARD LIBRARY FUNCTIONS .................................................................................................... 129
7.4 END OF CHAPTER SEVEN ASSESSMENT QUESTIONS .......................................................................131
8. CHAPTER EIGHT: STRUCTURES AND UNIONS ................................................................................132
8.1 OBJECTIVES ......................................................................................................................................133
8.2 STRUCTURES ....................................................................................................................................134
8.3 UNIONS ............................................................................................................................................140
6 C PROGRAMMING (1201)

8.4 END OF CHAPTER ASSESSMENT QUESTIONS .................................................................................. 142


9.1 END OF MODULE PRACTICAL ASSESSMENT QUESTIONS ................................................................143
9.2 END OF MODULE PRACTICE QUESTIONS ........................................................................................ 144
7 C PROGRAMMING (1201)

[Link] ONE

INTRODUCTION TO:
8 C PROGRAMMING (1201)

1.1 OBJECTIVES

By the end of the chapter students should be able to:


 Define c programming
 Analyze the basic structure of a c program
 Describe the basic functions used in c programming
 Discuss the basic c programming terms and keywords.
 Outline the uses of c programming
 Design basic c programs
 Design flowcharts, algorithms a pseudocodes for basic c programs.
9 C PROGRAMMING (1201)

1.2 C PROGRAMMING

 C is a programming language.
 Designed and written by a man named Dennis Ritchie.
 It replaced languages like FOTRAN, PASCAL etc.
 Became popular because it is reliable, simple and easy to use.
 Program –any piece of code written to perform a specific task.

1.2.0 C BASICS

• C was invented to write an operating system called UNIX.


• C is a successor of B language which was introduced around the early 1970s.
• The language was formalized in 1988 by the American National Standard Institute (ANSI).
• The UNIX OS was totally written in C.
• Today C is the most widely used and popular System Programming Language.
• Most of the state-of-the-art software have been implemented using C.
• Today's most popular Linux OS and RDBMS MySQL have been written in C.

1.2.1 WHY C?

 Easy to learn
 Structured language
 It produces efficient programs
 Can handle low level activities
 Can be compiled on a variety of computer platforms

1.2.2 APPLICATIONS OF C PROGRAMMING


10 C PROGRAMMING (1201)

• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Databases
• Language Interpreters
• Utilities

1.2.3 PROGRAMMING LANGUAGES

 C
 C++
 Java
 C#
 Python
 Visual basic
 Html
 Pearl etc.

1.2.4 LANGUAGES
11 C PROGRAMMING (1201)

 Programming language -Provides an interface between the user and


the microprocessor (CPU)
-Mode of communication between a computer and user.
 Machine language -it is the language understood by the machine.
 Assembly language -it is the language understood by the user.
 High level language -language used by the programmer.
- C is an example of high level language.
-C files are denoted by .C extension

1.2.6 COMPILER

• It is a software which is used to convert a high level language into a machine level
language.
• Examples of C compilers
-code blocks
-visual studio
-dev cpp
-turbo cpp etc.
12 C PROGRAMMING (1201)

1.3 WHAT IS C PROGRAMMING?

 A process of taking an algorithm and encoding it into a programming language so that it


can be executed by a computer.
 An algorithm is a procedure that describes the exact steps needed for the computer to
solve a problem so as to reach a goal.
 Syntax is a way of writing a program using a programming language.

1.3.1 C PROGRAM
1. add.c –this stage is called pre-processing
2. [Link]-assembly file
3. [Link]-machine language file
4. [Link]-executable file

1.3.2 Developing a C program

 Open your compiler software


 Create a new project and create a new folder for each project.
 You add your .c files (create a new source file).
 Write your code (program).
 Build and run the program. If there are errors you will be notified.
 Observe the output.
 Compiling converts .c to .obj files.
 Running converts .obj to .exe
13 C PROGRAMMING (1201)

1.3.3 Basics of C programming

 It is case sensitive.
 White space not important.
 End statement with a;
 A block of code defined with { }
 A C program basically consists of the following parts −
-Pre-processor Commands
-Functions
-Variables
-Statements & Expressions
-Comments
14 C PROGRAMMING (1201)

1.4 C BASICS

1.4.1 C PROGRAMMING BASICS

• The first line of the program #include <stdio.h> is a pre-processor command, which tells
a C compiler to include stdio.h file before going to actual compilation.
• The next line int main () is the main function where the program execution begins.
• The next line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in the program.
• The next line printf(...) is another function available in C which causes the message
"Hello, World!" to be displayed on the screen.
• The next line return 0; terminates the main () function and returns the value 0.

1.4.2 C BASICS CODING GROUND

[Link]
15 C PROGRAMMING (1201)

1.4.5 C PROGRAM

 Program execution in C will always start from the main.


 Any line of code terminated by a semicolon is called a statement.
 What is written between the opening and closing curly braces is called a block of code.
16 C PROGRAMMING (1201)

1.5 CONSTANTS

 Constants refer to fixed values that the program may not alter during its execution.
These fixed values are also called literals.
 Constants can be of any of the basic data types like an integer constant, a floating
constant, a character constant, or a string literal. There are enumeration constants as
well.
 Constants are treated just like regular variables except that their values cannot be
modified after their definition.

ESCAPE SEQUENCE IN C PROGRAMMING

Escape sequence Description

\n Newline. This will place a cursor on the beginning of the second line.

\t Tab. This will insert horizontal tab.

\\ Backslash. Insert backslash in a string.

\a Alert. This will signify alert sign or sound without changing the position of cursor
17 C PROGRAMMING (1201)

1.5.1 PROGRAM TO SHOW THE USE OF CONSTANTS.

OUTPUT
18 C PROGRAMMING (1201)

1.6 FILES IN C LANGUAGE

 Main.c – c files
 Add.h –header files
 [Link]-assembly files

1.6.1 HEADER FILE

• # include<stdio.h> is a header file


• # is a preprocessor directive
• Include add something/include something
• < > tells the compiler to look for the files stdio.h in the computer.
• stdio.h standard input and output library
19 C PROGRAMMING (1201)

1.7 BASIC FUNCTIONS IN C PROGRAMMING

Main Function
 main () signifies the entry point of our program.
 C is case sensitive i.e. it requires small letters.
 In c programming any word which is followed by ( ) or (…) is a function.
 Main is a keyword.
Output function
• printf is an output function.
Syntax:
printf(“ my first program in C);
Input functions
 scanf is used to get input data from the keyboard.
 It is an input function
scanf (“%d”, &data)
 %c –specifies data type to be captured.
 &-specifies that the captured data is to be stored in memory.
 Data-is the memory location to store captured letter or character.

Steps involved in execution of typical C program

1. Creating a program
2. Preprocessing and Compiling
3. Linking
4. Loading
5. Execution
20 C PROGRAMMING (1201)

1. CREATING A PROGRAM

This is the first step involved in writing a C program which is accomplished with an editor
program such as code blocks. C program file names have .c extension.

2. PREPROCESSING AND COMPILING

When your program is ready it needs to be compiled with the help of a compiler. The compiler
translates the C program into machine language code.

Before compiling a program preprocessor program executes automatically obeying the special
commands from preprocessor directives

3. LINKING.

In this step, linker links the code to missing functions. For example, your program may contain
standard library functions that are defined elsewhere.

4. LOADING

The next step of program development is called loading. In this step, loader loads the compiled
program into computer memory for execution.

5. EXECUTION

Finally, the computer executes the program that is loaded in computer’s memory.
21 C PROGRAMMING (1201)
22 C PROGRAMMING (1201)

Line 1:

The first line of the program begins with /* and ends with */ which represents that any text
written between these two symbols is a comment.

Comments do not affect program while running because C compiler ignores the comments and
programmers use these comments for the documentation of the program.

Line 2:

#include <stdio.h>

This is the directive to the C Preprocessor (You might wonder what these words like
preprocessor and directives are but don’t worry these things will be crystal clear after going
through next chapters).

Lines beginning with # are called preprocessor directives and this specific line tells
preprocessor to include the contents of standard input/output header file (stdio.h) in the
program while the program is being compiled.

Line 3:

int main ( )

This is the main functions of the program which is part of every C program.
23 C PROGRAMMING (1201)

C programs can have multiple functions which are also called the building blocks, however, one
of the function should be main.

The code of the function is written between the curly braces and in this program also the body
of the function is enclosed by the braces in line 4 and 7.

Line 5:

printf ("Hello World!!!");

This is the standard output functions which instruct the computer to print anything written
between the quotes (” ..text..”).

This entire line including the printf, its arguments within the parenthesis and the semicolon is
called a statement.

Every statement must end with a semicolon.

Good Programming Practice


While writing C program, commenting is a good idea which makes clear about the purpose of
the function.

Line 6:

return 0;

This is nothing but an exit sequence for the function.

Moreover, it signifies the termination of the function main returning the integer value 0.
24 C PROGRAMMING (1201)

1.7.1 DESIGNING YOUR FIRST PROGRAM

Let’s do it!
25 C PROGRAMMING (1201)

1.8 DATA TYPES AND VARIABLES

Data type
Reserved words which tells us about:
-the size of data
-type of data
-range of values which can be acceptable to data item
Data types are names or labels which are used to define the kind of data being stored or
manipulated. In any programming language, data type specifies the range of values that
a variable can hold and how this information is stored in the memory heap.

Data types in C programming

integer type

Fundamental data types: floating type

Character type

Function

Derived data types: Arrays

Pointer

Structure

User-defined data types:


Union
26 C PROGRAMMING (1201)

FUNDAMENTAL DATA TYPES

 Char –character (8 bits or less).


-It stores a single character.
 Int-integer(32 bits or less)
-it stores an integer that is a positive or negative whole number.
 Float-float(32 bits or less)
-stores decimal numbers.
 Short-short(16 bits or less)
 Double-double(64 bits or less)
-double float.
27 C PROGRAMMING (1201)

How to capture different data types

• %c-character
• %d-integer(whole number + or -)
• %f-float(decimal)
• %s-string(collection of characters)

EXAMPLES OF SPECIAL CHARACTERS IN C


28 C PROGRAMMING (1201)

1.8.1 INPUT AND OUTPUT FUNCTIONS

Let’s do it!
29 C PROGRAMMING (1201)


30 C PROGRAMMING (1201)

1.8.2 KEYWORDS

The following list shows the reserved words in C. These reserved words may not be used as
constants or variables or any other identifier names.

SimpleTip:

We cannot Use Keywords for:

– For Declaring Variable Name

-For Function Name

-For declaring Constant Variable


31 C PROGRAMMING (1201)

1.8.3 Variable name

 Is a named memory location.


Syntax
int y;
 y is a memory location hence y is a variable name.
Variable declaration
 A variable should be declared before it is used.
 Done at the beginning of the program.
 A data type is declared alongside with the variable name.
int x;
x is the variable name
int is the data type
 Maximum allowable length of a variable is 31 and minimum is allowable length is 1.
 Meaningful variable names should be used.
Use the link below to enhance your understanding on variable declaration.

CODING GOUND FOR VARIABLE DECLARATION


Copy the link to your browser.
[Link]
32 C PROGRAMMING (1201)

1.8.4 RULES FOR CONSTRUCTING VARIABLE NAMES

 Variables can have any combination of 1-31 alphabets ,digits or underscores


e.g. int prim_21_89 ;
 First character of the variable name must be alphabetical or underscore
e.g. int number12; or int _number12;
 No commas or blanks are allowed within a variable name
e.g. int myAge; int myfirstprogram;
 No special symbol other than an underscore can be used on a variable name.

Variable initializing
 Initialization is assigning a value to a variable.
 Since C is case sensitive language variable name written in uppercase will differ from
lowercase.
Syntax
int y=15; value is 15
y is the variable name

Data type
15 is a value to be stored in y.
33 C PROGRAMMING (1201)

1.9 GETCHAR () FUNCTION

 Getchar () is used to capture characters from a keyboard.


 Can also be used to halt the execution of a program.
To get a character from a keyboard :

To halt the execution of a program:


34 C PROGRAMMING (1201)

1.10 PUTCHAR () FUNCTION

Putchar ()
• Displays a character to the output.
35 C PROGRAMMING (1201)

1.11 COMMENTS IN C PROGRAMMING

 A code which is readable


 A guiding factor to a code
 User friendly

1.11.1 WAYS OF COMMENTING

• Multiple line comment/*-----------------------------------------------------------------------------------


-------------------------------------------------------------------------------------*/

• Single line comment //--------------------------------------------------------------


36 C PROGRAMMING (1201)

1.12 FLOWCHARTS, ALGORITHMS AND PSEUDO


CODES.

1.12.1 FLOW CHART

 A flowchart is a diagrammatic representation of an algorithm.


 A flowchart can be helpful for both writing programs and explaining the program to
others.
37 C PROGRAMMING (1201)

1.12.2 ALGORITHM

 An algorithm is a set of well-defined instructions in sequence to solve a problem.

Qualities of a good algorithm


 Input and output should be defined precisely.
 Each step in the algorithm should be clear and unambiguous.
 Algorithms should be most effective among many different ways to solve a problem.
 An algorithm shouldn't have a computer code. Instead, the algorithm should be written
in such a way that it can be used in different programming languages.
38 C PROGRAMMING (1201)

Examples of Algorithms in Programming


 Write an algorithm to add two numbers entered by the user.

1.12.3 PSEUDO CODE

 Pseudo code: It’s simply an implementation of an algorithm in the form of annotations


and informative text written in plain English.
 It has no syntax like any of the programming language and thus can’t be compiled or
interpreted by the computer.
Advantages of Pseudocode
 Improves the readability of any approach. It’s one of the best approaches to start
implementation of an algorithm.
 Acts as a bridge between the program and the algorithm or flowchart. Also works as a
rough documentation, so the program of one developer can be understood easily when
a pseudo code is written out. In industries, the approach of documentation is essential.
And that’s where a pseudo-code proves vital.
 The main goal of a pseudo code is to explain what exactly each line of a program should
do, hence making the code construction phase easier for the programmer.
 This program will check whether the number is even or odd
39 C PROGRAMMING (1201)
40 C PROGRAMMING (1201)

1.13 PRACTICAL QUESTIONS FOR CHAPTER 1

a) Design eco cash page 3 using the output functions and constants you have learnt.[10]
b) Write a program that prompts a user to enter character, integer, float.[10]
c) Write a C program to accept 2 integer values from the keyboard then calculate the
product of these 2 integers and display the output. [10]
Output of the form:
Enter first number y
Enter second number x
Product is z
41 C PROGRAMMING (1201)

1.14 END OF CHAPTER 1 QUESTIONS

i) Define the following terms


a) syntax
b) program
c) high level language
d) keyword [8]
ii) Give three examples of each of the following:
a) data types
b) compilers
c) programming languages
d) Keywords
e) header files [15]
iii) State the rules to follow when declaring variables. [4]
iv) Outline the uses of the following in C programming and hence show the syntax of each.
a) printf
b) scanf
c) getchar()
d) putchar() [8]
v) To design any program in C a flow chart, pseudocode and algorithm must be used to avoid
errors. Show how a flow chart, pseudocode and algorithm can be used if you are designing a
program that captures two integers on a keyboard. [25]
42 C PROGRAMMING (1201)

[Link] TWO: OPERATORS IN C


PROGRAMMING
43 C PROGRAMMING (1201)

2.1 OBJECTIVES

By the end of the chapter students should be able to:


 State the different types of operators used in C programming.
 Discuss the operation of each type of operator
 Design a program for each type of operator.
44 C PROGRAMMING (1201)

2.1.1 OPERATORS

 An operator is a symbol that tells the compiler to perform specific mathematical or


logical functions.
 C language is rich in built-in operators and provides the following types of operators

2.1.2 CLASSES OF OPERATORS

 Arithmetic operators
 Assignment operators
 Relational operators
 Logical operators
 Advanced logical operators
 Bitwise operators
 Increment or decrement operators
 Ternary operators
45 C PROGRAMMING (1201)

2.2 ARITHMETIC OPERATORS

 Used to perform some arithmetic calculations in C Programming.


Below are examples of arithmetic operators

Addition operator +
• Used for the addition of operands to give the sum.
a + b =sum;
If a=3 and b=4 and the sum is 7
Subtraction operator -
• Used to find the difference between operands
a –b=difference;
46 C PROGRAMMING (1201)

If a =3 and b=2 then the difference is 1


Multiplication operator *
Used to find the product of numbers.
a*b=product;
If a=2 and b=3 then the product is 6.
Division operator /
Used to find the quotient of numbers.
a /b=quotient;
If a=6 and b=3 then the quotient is 2.
Modulus operator %
Modulus % extract the remainder of the division operation.
In other ways it just displays the remainder after dividing.
a % b=modulus;
If a=6 and b=4 then the quotient is 1 and the remainder is 2.
So the modulus is 2.
Thus 5 % 2=1 6 % 3 =0
47 C PROGRAMMING (1201)

2.3 LOGICAL OPERATORS

Are used to perform basic logical operations performed by logic gates


& AND
| OR
! NOT
^ EX OR
AND &
Will return a high or a one when both inputs are ones.
a & b =z;
If a =10 b=7 then z= 2
10 = 1 0 1 0
7= 0 1 1 1
0010
z=2

OR |
Will return a high or a one when one of the inputs is a one.
a | b =z;
If a =10 b=7 then z= 15
10 = 1 0 1 0
7= 0 1 1 1
1 11 1
z=15
48 C PROGRAMMING (1201)

NOT!
 Inverts the input
a=1 then the output z is 0
EX OR ^
 Will give a one when both inputs are the same
a ^ b =z;
If a =10 b=3 then z= 5
10 = 1 0 1 0
7= 0 0 1 1
0110
49 C PROGRAMMING (1201)

2.4 ADVANCED LOGICAL OPERATORS

Used to join items or conditions


&& AND
|| OR

Example of advanced AND.


if (mark>80 && mark<=100)
{
printf(“distinction”);
}
50 C PROGRAMMING (1201)

2.5 RELATIONAL OPERATORS

Used to compare two things.


Examples of relational operators
51 C PROGRAMMING (1201)

Example
a=2;
b=3;
if (a>b) // condition
{
printf (“open door”);
}

LIVE DEMO CODING GROUND


• [Link]
52 C PROGRAMMING (1201)

2.6 ASSIGNMENT OPERATORS

Mostly used to assign values to variables


int x;
x=3;
 A value on the right is assigned to a variable on the left and not vice versa.
3=x……….wrong

 x+=y is the same as x=x + y but:


x+=y
 Is a single operation.
 it is only performed in one machine cycle
 it is faster in terms of speed of operation
53 C PROGRAMMING (1201)

x= x+y
 Two operators
 Two machine cycles
 Consumes more time
 Example
 x= x+y
 If x=5 y=3
 Then x on the left side is 8
 x= x-y
 Then x on the left side is 2
SUMMARY

2.7 INCREMENTAL/DECREMENTAL

 Used to increase or decrease value of a variable by 1.


 ++ used to increase value of a variable by 1
 -- used to increase value of a variable by 1
54 C PROGRAMMING (1201)

Example 1
int x;
x=5;
x++;
After operation x=6

Example 2
int x;
x=5;
x--;
After operation x=4
• Pre increment ++x
Will increment after operation
• Post increment x++
Will increment before operation
• Pre decrement --x
Will decrement after operation
• Post decrement x--
Will decrement before operation
55 C PROGRAMMING (1201)

2.8 BITWISE OPERATORS

This involves the shifting of bits


• & AND

• | OR

• ! NOT
• ^ Ex OR
• << logical left shift
• >> logical right shift
• ~ compliment
56 C PROGRAMMING (1201)

 Shifting a number by n bits to the left is identical to multiplying by 2^n


 Shifting a number by n bits to the right is identical to dividing by 2^n and carry discarded.
 ~ =compliment
Conversion of 1s to 0s and 0s to 1s
x=10 1010
~x=5 0101
57 C PROGRAMMING (1201)

2.9 TENARY OPERATORS

Conditional operator is a ternary operator that takes three operands.

Syntax

x = exp1? a: b

Here, if exp1 is non zero i.e. TRUE then the value of a will be assigned to x and if exp1 is zero
i.e. FALSE then the value of b is assigned to x.

Condition? True part: False part;


Example
a=5; b=3;
c= (a>b)? a: b;
If true c=a;
If false c=b;
58 C PROGRAMMING (1201)

2.10 END OF CHAPTER TWO ASSESSMENT


QUESTIONS

1. Define the following terms

a) operator
b) Operand [4]
2. Outline the use of each of the seven operators you have learnt. [8]
3. Design seven programs to show the functionality of the seven operators you have learnt. [40]
59 C PROGRAMMING (1201)

[Link] THREE

CONTROL STATEMENTS
60 C PROGRAMMING (1201)

3.1 OBJECTIVES

By the end of the chapter students should be able to:


 Discuss the three classes of control statements.
 Analyze the differences between the if, if else and else if.
 Formulate flow charts of various conditional statements
 Design programs using the four conditional statements.
 Outline the uses of conditional, loops and jump statements in C programming.
 Discuss the three types of loops.
 Design suitable programs using loops.
 Show where jump statements can be used in a designed C program.
61 C PROGRAMMING (1201)

3.1.1 CONTROL STATEMENTS

• Used to control the flow of a program


• Basically classified into
i) Conditional statements
ii) Looping statements
iii) Jump statements

3.2 CONDITIONAL STATEMENTS

Conditional statements
 If
 If else
 else if
 switch
62 C PROGRAMMING (1201)

3.2.1 IF STATEMENT

Syntax
If (…….)
{
Statement;
}
 If the condition is true it will execute the block of code.
 If the condition is false the program goes to the end of the block.
63 C PROGRAMMING (1201)

OUTPUT
64 C PROGRAMMING (1201)

3.2.2 IF ELSE

Syntax
If (condition)
{
Statement 1….condition is true the if block is executed
}
else
{
Statement 2…. condition is false the else block is executed
}
65 C PROGRAMMING (1201)

IF ELSE

OUTPUT
66 C PROGRAMMING (1201)

3.3.3 ELSE IF

The if (condition) // will be executed if the condition is true it will execute the block of code
and ignore all the else if blocks and the else
{
Statement 1
}
else if (condition) // the else if block is executed if all the above conditions are false
{
Statement 2
}
else if (condition)
{
Statement 3
}
67 C PROGRAMMING (1201)

Let’s do it!

Question
Given a table below design a suitable program

TEMPERATURE CONDITION

<20 degrees Temperature cool

20-30 degrees Temperature warm

>30 degrees Temperature hot


68 C PROGRAMMING (1201)

SOLUTION
69 C PROGRAMMING (1201)

3.2.4 SWITCH

 Used when we have multiple options and we need to perform a different task for each
option.
 It is faster than if else but slower than a loop.
Syntax
switch (n)
{
case constant 1:
//code to be executed if n is equal to constant
break;
case constant 2:
//code to be executed if n is equal to constant
break;
case constant n:
//code to be executed if n is equal to constant
break;
default:
//code to executed if it does not match any constant

• The break statement is used to prevent the code running into the next case.
70 C PROGRAMMING (1201)

SWITCH FLOW CHART

Question
Write a program to display a number entered from keyboard using switch statement.
The number should be in range 1-5.
71 C PROGRAMMING (1201)

SOLUTION
72 C PROGRAMMING (1201)

Let’s do it!
73 C PROGRAMMING (1201)

CONDITIONAL SUMMARY
74 C PROGRAMMING (1201)

3.3 LOOPING STATEMENTS

 Are used to repeat a particular set of statements.


 Consist of :
-initial value
-condition (stop value)
-decrement /increment

Looping statements
 While
 Do while
 for
75 C PROGRAMMING (1201)

3.3.1 LOOPING STATEMENTS….WHILE

 Repeats a statement or group of statements while a given condition is true.


 It test the condition before executing the loop body
Syntax
while (condition)
{
………….
Increment/decrement
}
76 C PROGRAMMING (1201)

3.3.2 Do while

 It is more like a while statement except that it test the condition at the end of the loop
body.
 The first execution of the loop is done without checking the body.

Syntax
do
{
………..
Increment/decrement;
}
while (condition);
77 C PROGRAMMING (1201)

Let’s do it!
78 C PROGRAMMING (1201)

3.3.3 FOR LOOP

 Variable initialization is done once.


 After initialization program checks the condition
 If the condition is true the loop is executed
 If the condition is false the program goes to the end of the loop.
 After executing the program goes to increment/decrement
 Program then checks the condition again.

Syntax
for (initial value; condition; increment/decrement)
{
…….
}
79 C PROGRAMMING (1201)

In the above example, counter is the control variable and counter = 1 is the first statement
which initializes counter to 1.

The second expression, counter <= 5 is the loop continuation condition which repeats the loop
till the value of counter exceeds 5.

Finally, the third expression, counter++ increases the value of counter by 1.

So the above piece of code will print 'hello' five times.


80 C PROGRAMMING (1201)
81 C PROGRAMMING (1201)

LOOP PROGRAMS

While program

OUTPUT
82 C PROGRAMMING (1201)

Do while program
83 C PROGRAMMING (1201)

For loop program


84 C PROGRAMMING (1201)

Lets do it!
85 C PROGRAMMING (1201)

3.3.4 THE INFINITE LOOP

• A loop becomes an infinite loop if a condition never becomes false.


• The for loop is traditionally used for this purpose.
• Since none of the three expressions that form the 'for' loop are required, you can make
an endless loop by leaving the conditional expression empty.
EXAMPLE
86 C PROGRAMMING (1201)

3.4 JUMP STATEMENTS


 Break
 Continue
 Go to

3.4.1 BREAK STATEMENT


 Causes the program to break the currently running loop.
 Used inside a loop or switch

In while loop
87 C PROGRAMMING (1201)

In do while loop

In for loop
88 C PROGRAMMING (1201)

3.4.2 CONTINUE

 Unlike the break statement, with continue the program will not move out of current
running loops.
 Program will skip all statements below continue inside a loop.

In while loop
89 C PROGRAMMING (1201)

In do while loop

In for loop
90 C PROGRAMMING (1201)

JUMP STATEMENTS SUMMARY


91 C PROGRAMMING (1201)

3.5 END OF CHAPTER 3 ASSESSMENT QUESTIONS

i) State the three classes of control statements. [3]


ii) Show the syntax of writing
a) if
b) else if
c) switch
d) for loop [8]
iii) Outline the differences between
a) else if and if else
b) break and continue
c) while and do while [9]
iv) Given the table below design a suitable program using conditional statements

Tomato Mass Grade

15g and below D

16-30 C

31-45 B

46 and above A
92 C PROGRAMMING (1201)

v) Design a program that display the following pattern using loops


22222
2222
222
22
2
22
222
2222
22222 [10]
vi) Design a program that displays “Corona virus will end” infinitely. [10]
93 C PROGRAMMING (1201)

[Link] FOUR
94 C PROGRAMMING (1201)

4.1 OBJECTIVES

By the end of the chapter students should be able to:


 Define a function
 Outline the uses of functions in C.
 State the four types of functions
 Apply the theory of functions to design suitable programs.
95 C PROGRAMMING (1201)

4.2 FUNCTIONS

 A function is an independent block of code which can exist outside the main function.
 Used to manage our code
 This is done by allocating specific tasks to modules outside the main
 Functions enable us to put a specific code inside more than one .c file
 Comprise of three stages
i) Function prototype
ii) Function call
ii) Function definition
 Declare variables if any
 Variables declared inside a function are called local variables.
 Thus local variables are invisible to other functions.
 Variables visible to more than one function are called global variables
96 C PROGRAMMING (1201)

Syntax
return type function name (arguments/parameters)
{
…..
}
Example
void add (int x , int y)
{
…..
}
 Return Type − A function may return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
 Function Name − this is the actual name of the function. The function name and the
parameter list together constitute the function signature.
 Parameters − A parameter is like a placeholder. When a function is invoked, you pass a
value to the parameter. This value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.
 Function Body − the function body contains a collection of statements that define what
the function does.
97 C PROGRAMMING (1201)

4.2.1 Function types


 Function with no return type and no arguments
 Function with return type and no arguments
 Function with no return type and arguments
 Function with return type and arguments
 void add(void)
 First void signifies the return value which is a value returned to the calling function by
the called function.
 In this case nothing is being returned because it is void.
 If an integer is to be returned the void is replaced by int.
 Add is a function name and can be given any value.
98 C PROGRAMMING (1201)

 The second void signifies argument of our function.


 Arguments can only have 1 return value but can have any number of arguments.

CODING GROUND
Use the link below to enhance your understanding on functions.
[Link]

Using add as the function name:


 Function with no return type and no arguments void add(void)
 Function with return type and no arguments int add(void)
 Function with no return type and arguments void add(int x , int y)
 Function with return type and arguments int add(int x, int y)
99 C PROGRAMMING (1201)

FUNCTION CALL

FUNCTION DECLARATION OR PROTOTYPE

Here, int before function name indicates that this function returns integer value to the caller
while int inside parentheses indicates that this function will receive an integer value from caller.
FUNCTION DEFINITION
A function definition provides the actual body of the function.
100 C PROGRAMMING (1201)

RETURN STATEMENT
Return statement returns the value and transfer control to the caller.
101 C PROGRAMMING (1201)

4.3 LOCAL DECLARATION

 Variables that are declared inside a function or block are called local variables.
 They can be used only by statements that are inside that function or block of code.
 Local variables are not known to functions outside their own. The following example
shows how local variables are used. Here all the variables a, b, and c are local to main ()
function.
CODING GROUND
Use the link below.
[Link]

4.4 GLOBAL DECLARATION

 Global variables are defined outside a function, usually on top of the program.
 Global variables hold their values throughout the lifetime of your program and they can
be accessed inside any of the functions defined for the program.
 A global variable can be accessed by any function. That is, a global variable is available
for use throughout your entire program after its declaration. The following program
show how global variables are used in a program.
CODING GROUND

Use the link below.


[Link]
102 C PROGRAMMING (1201)

Function with return with no arguments Function with return with arguments
103 C PROGRAMMING (1201)

FUNCTION WITH RETURN TYPE WITH NO ARGUMENTS

Let’s do it!

• Using the same concept design a program with no return with arguments.
104 C PROGRAMMING (1201)

4.5 END OF CHAPTER 4 ASSESSMENT QUESTIONS

i) Define a function. [2]


ii) Discuss why functions are needed in programming.[3]
iii) State the four types of functions. [4]
iv) Show the syntax of writing the four types of functions.[4]
v) Design a program that calculates the area of a circle and perimeter of a circle using
functions. [10]
vi) Design a program that calculates the area and perimeter of a rectangle/triangle [10]
vii) Design a calculator that adds, multiply, subtract and divide on a single program using
functions.[10]
viii) Using the current bank transfer rate of Zim dollars to USD design a Zim to USD converter
using suitable functions. [10]
105 C PROGRAMMING (1201)

[Link] FIVE
106 C PROGRAMMING (1201)

5.1 OBJECTIVES

By the end of the chapter students should be able to:


 Define an array.
 Discuss why arrays are used in programming.
 Show how one and two dimensional arrays are declared and initialized.
 Apply the knowledge of arrays to design the suitable programs.
107 C PROGRAMMING (1201)

5.3 ARRAYS

 A collection of items of similar type /data type


 A collection of characters, integers, float values etc.
 An array of characters is also referred to as a string.
Why arrays in C

 Helps to eliminate multiple variable declaration


 Helps to reduce code density
 Helps us to manipulate two dimensional data e.g. matrices.
Syntax
Data_type array_name [array size];
Data type
-Int, char, float etc.
Array name
-Name of your array
-Array name you need
Array size
-Maximum number of elements your array can hold
-Number of elements should not exceed array size.
Array declaration and initialization
int animals [5 ]; // declaration
• For array declaration and initialization you can choose to omit the array size
int animals [ ] = {2, 3, 4, 6, 8}; // declaration and initializing
108 C PROGRAMMING (1201)

To print everything in the array


The for loop can be used.
To replace a value in an array
• For example changing the value of position 2 in an array below
int mark [ ] = {5, 7, 8, 9, 4};
Then changing position 2 we say:
mark [2] =10;
109 C PROGRAMMING (1201)

Let’s do it!
A program to display all even numbers
110 C PROGRAMMING (1201)

Practice question
Consider array marks with the following elements 60, 70,76,56,89
Write a program to display the marks.
Solution
111 C PROGRAMMING (1201)

5.4 TWO DIMENSIONAL ARRAYS

 Contains more than one row of elements.


 Declaring a 2D array
int marks [rows] [columns]
 Combining 2 one dimensional array into a 2D we get:
int a [ ]= {3,4,5}; //one dimensional array
int b [ ]= {6,7,8}; //one dimensional array

Reading elements on a 2*2 array:


int x[ ] [ ]={{4,7},{10,5}};
 Two variables are needed for the for loop.
112 C PROGRAMMING (1201)
113 C PROGRAMMING (1201)

Let’s do it!

• Given matrices to add


Matrix 1= 2 3
7 4
Matrix 2= 1 4
3 2
Answer= 3 7
10 6
Design a program that adds the above two matrices and hence find the determinant.
114 C PROGRAMMING (1201)

5.5 END OF CHAPTER 5 ASSESSMENT QUESTIONS

1. Define an array. [2]


2. Discuss why arrays are needed in C programming. [3]
3. Show the syntax of writing arrays. [3]
4. Write a program to display all the even numbers inside the following array
x[ ]={ 7,4,2,13,6,14,23}; [12]
5. For the above array x write a program to find the sum of all the elements of this array.
7+4+2+13+6+14+23 [12]
6. Write a program to display the smallest number in the array x. [12]
7. Display the largest number. [12]
115 C PROGRAMMING (1201)

[Link] SIX
116 C PROGRAMMING (1201)

6.1 OBJECTIVES

By the end of the chapter students should be able to:


 Define a pointer.
 Outline the need of using pointers in C programming.
 Show how pointers can be used to display value at address operator
 Show how pointers can be used to display address of operator.
 Demonstrate the use of pointers by calling array elements by value and by reference.
117 C PROGRAMMING (1201)

6.2 POINTERS

 Pointers in C are easy and fun to learn.


 Some C programming tasks are performed more easily with pointers, and other tasks,
such as dynamic memory allocation, cannot be performed without using pointers.
 So it becomes necessary to learn pointers to become a perfect C programmer.
 A pointer is a variable whose value is the address of another variable, i.e., direct address
of the memory location.
 Like any variable or constant, you must declare a pointer before using it to store any
variable address.
 The general form of a pointer variable declaration is −
type *var-name;
• Are used in C to:
i) Point to a memory location
ii) Store the value of a memory location
iii) Two memory address operators
* - value at address operator
& - address of operator
Example
int i=10;
i) printf(“ address of i is %u ”,& i);
ii) printf(“ address of i is %d ”, i);
iii) printf(“ address of i is %d”,*(&i));
 For the first command output will be a memory location
(0x0000008)
 For the second location output will be 10.
 For the third location output will be 10.
118 C PROGRAMMING (1201)

Let’s do it!


119 C PROGRAMMING (1201)

 &i gives the address of the variable I


 This variable can be stored in a variable
E.g. j j=& i
 j is not an ordinary variable since it contains an address of another variable.
 J is referred to a pointer.
 Declaring j
int *j;
 This tells the compiler that j will be used to store the address of the integer value.
 Address locations are always positive whole numbers with zero included.
 All memory locations are unsigned thus %u.
 Declaring a float *t does not mean that is going to contain a float value but it is going to
contain the address of the float value.
120 C PROGRAMMING (1201)

6.3 PASSING ARRAY ELEMENTS TO FUNCTIONS

Can be passed using two methods:


i) Call by value-actual value of element is passed.
ii) Call by reference –address of the element is passed.
• Addresses on individual array elements are passed to the function hence the variable in
which this address is stored.
121 C PROGRAMMING (1201)
122 C PROGRAMMING (1201)
123 C PROGRAMMING (1201)

6.4 END OF CHAPTER 6 ASSESSMENT QUESTIONS

1. Define a pointer. [2]


2. Outline the uses of pointers in C programming. [2]
3. Show the syntax of displaying a memory location. [3]
4. State the two symbols that are mainly used when writing a program with pointers.[2]
5. Discuss the meaning of passing array elements by value and passing by reference and hence
design a program to show passing by value and by reference. [15]
6. Design a program to show your understanding of pointers.[10]
124 C PROGRAMMING (1201)

[Link] SEVEN
125 C PROGRAMMING (1201)

7.1 OBJECTIVES
By the end of the chapter students should be able to:
 Define, declare and initialize a string.
 Print strings to output.
 Capture a string from keyboard.
 Discuss standard library functions.
 Design programs using all listed standard library functions.
126 C PROGRAMMING (1201)

7.2 STRINGS

 It is a collection of characters
 A string is an array but with characters
 String declaration:
char string name [string size];
char name [15];
 The string size should be at least one greater than the maximum string size so as to
accommodate a null character.
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
 The null character signifies the end of the string.

Declaration and initialization


 char name [ ] ={‘P’,’R’ ,’I’,’M’);
 char name [ ]=“PRIM’;
127 C PROGRAMMING (1201)
128 C PROGRAMMING (1201)

 Scanf will not accept two worded strings e.g. Luke Mahomed
 Thus can be eliminated by using gets and puts
 Gets() reads characters from the standard input and stores them as a string
 Puts () prints characters from the standard output just like printf statement.
129 C PROGRAMMING (1201)

7.3 STANDARD LIBRARY FUNCTIONS


130 C PROGRAMMING (1201)

Let’s do it!

CODING GROUND
Standard Library String Functions live demo

[Link]
131 C PROGRAMMING (1201)

7.4 END OF CHAPTER SEVEN ASSESSMENT


QUESTIONS

1. Define a string. [2]


2. Show the syntax of declaring and initializing a string. [3]
3. List seven standard library functions you have learnt and outline the function of each.
[14]
4. Choose any three standard library functions and design a program to illustrate the
functionality of each.[9]
132 C PROGRAMMING (1201)

8. CHAPTER EIGHT
133 C PROGRAMMING (1201)

8.1 OBJECTIVES

By the end of the chapter students should be able to:


 Define structures and unions
 Show the syntax of writing a structure/union.
 Design programs using a structure.
 Design programs using unions.
134 C PROGRAMMING (1201)

8.2 STRUCTURES

 A structure or union is a collection of elements of different data types.


• E.g. consider a book
book name – string
book price – float
book pages- int
 Structures are similar to unions
 Keyword struct is used when declaring a structure.
struct structure_name
{
/*different data types items are declared here*/
};

Here the keyword struct declares a structure that will hold the data fields named members. ‘tag
name’ is a name of the structure that we have declared. Inside the structure, we can use any
kind of datatypes like int, float, char and others.
135 C PROGRAMMING (1201)

Example
struct books
{
char name[20];
float price;
int pages;
};
 Once a structure has been defined one or more variables can be declared.
 A structure is usually defined outside the main function.
Declaration and initialization of a structure
 Struct books B1={”cprog ”,300.00,67};
 Struct books B2={”c fundamental ”,250.00,67};

Accessing elements of a structure


• Dot operators are used for example to access number of pages: [Link]
printf(“number of pages is %d”,[Link]);

To add a name to a book B1.


gets([Link]);
To add number of pages to B2
scanf(“%d”,&[Link]);
To add price of B1
scanf(“%.2f”,&[Link]);
136 C PROGRAMMING (1201)

Here product1 and product2 are variable which will hold members product name and product
number.
NOTE: Remember that members inside the structure are not variables and they do not occupy
memory until they are associated with structure variables such as product1 and product2.
137 C PROGRAMMING (1201)

Let’s do it!

Structures live demo

[Link]
138 C PROGRAMMING (1201)

Let’s do it!
139 C PROGRAMMING (1201)

Program to take employee, his/her name, age, and salary.


140 C PROGRAMMING (1201)

8.3 UNIONS

Defining a Union
 To define a union, you must use the union statement in the same way as you did while
defining a structure.
 The union statement defines a new data type with more than one member for your
program.
141 C PROGRAMMING (1201)

Let’s do it!
142 C PROGRAMMING (1201)

8.4 END OF CHAPTER ASSESSMENT QUESTIONS

1. Design a canteen system using structures or unions. [10]


2. Mrs. Pota is planning to sell online books. [10]
3. Design a program using structures that you think would be best for
her business. [25]
4. Define a structure. [2]
3. Outline the differences between a structure and an array. [3]
4. Show the similarities of structures and unions. [4]
143 C PROGRAMMING (1201)

9.1 END OF MODULE PRACTICAL ASSESSMENT


QUESTIONS

1. As a C programmer design a working program that you think would be useful to TCFL.
Your program must include almost everything you have learnt in C programming
including comments.[100]
2. A group of innovators are working towards the designing of a gate monitoring system.
As a C programmer design a program that you think these innovators would use on the
gate monitoring system.[100]
3. The TCFL librarian has launched a library system programming competition. Assuming
that you are one of the competitors design a program that you think would benefit the
library.[100]
4. Design a covid-19 body temperature detector. [ 15]
144 C PROGRAMMING (1201)

9.2 END OF MODULE PRACTICE QUESTIONS


Question 1

Define the following terms with relationship to C programming

a) Program
b) Get(char)
c) Printf
d) Compiler
e) Array [10]

Question 2

a) Give two examples on each of the following


i) Data type
ii) Bitwise operators
iii) Jump statements
[6]
b) Outline the four rules to follow when declaring variables [4]

Question 4

Design a program that displays at least 6 assignment operators you have learnt one at time
using switch statements. [10]

Question 5

Outline the differences between each of the following using suitable programs

a) While and do while [7]


b) Continue and break [7]
145 C PROGRAMMING (1201)

Question 6

a) As a c programmer who has been tasked to design an online library system at TCFL.
Design a suitable program that performs the following to access the library:
i) Enter password student6229 to be granted access.
ii) If the password is wrong it must deny access. [9]
b) Show the syntax of writing the following
i) The for loop statement
ii) Pointers
iii) Structures
iv) while
v) do while [10]

Question 7

Mrs. Rose owns a bookshop. Design a program that you think she must use in order for her to
sell her books. The program must include the following:

a) Book price
b) Book title
c) Number of pages in the book
d) Book Author [10]

Question 8

a) Write a program to display all the even numbers inside the following array
x = {7, 4, 2, 13, 6, 14, 23};
b) For the above array x, write a program to find the sum of all the elements of this array
7+4+2+13+………23.
c) Write a program to display the smallest number in the array x. [10]
146 C PROGRAMMING (1201)

Question 9

A function is an independent block of code which can exist outside the main function. They are
used to manage our code. Show how functions can be used to calculate the area and perimeter
of a rectangle on a single program. Use a function type of your choice.
[10]

*************************************************************************************

You might also like