C Programming Notes
C Programming Notes
CONTENTS
INTRODUCTION ................................................................. Error! Bookmark not defined.
1 BASIC CONCEPTS .................................................................................................... 1
Objectives ....................................................................................................... 1
Introduction ................................................................................................... 1
Programming defined..................................................................................... 1
Programming language defined..................................................................... 2
Structured Programming defined .................................................................. 2
Steps in program development ...................................................................... 2
C language basic features .............................................................................. 3
Advantages of C Language ............................................................................. 4
C Program components .................................................................................. 5
Program errors and debugging .................................................................... 11
Guidelines to good C programming ............................................................. 12
Revision Exercise ................................................... Error! Bookmark not defined.
2 DATA HANDLING ..................................................................................................... 14
Objectives ..................................................................................................... 14
Introduction ................................................................................................. 14
Variables ....................................................................................................... 14
Basic data types ........................................................................................... 15
Using printf( ) to output values ................................................................... 16
Inputting values from the keyboard using scanf( ) ..................................... 18
Types of variables ........................................................................................ 20
Storage classes............................................................................................. 20
Constants...................................................................................................... 23
Revision Exercise ................................................... Error! Bookmark not defined.
3 OPERATORS ............................................................................................................ 27
Objectives ..................................................................................................... 27
Introduction ................................................................................................. 27
Operators versus operands .......................................................................... 27
Arithmetic operators .................................................................................... 28
The assignment operator ............................................................................. 31
Relational operators ..................................................................................... 32
Logical operators .......................................................................................... 33
The Conditional operator ............................................................................. 33
Unary operators ........................................................................................... 34
The sizeof operator ..................................................................................... 34
Revision Exercise ................................................... Error! Bookmark not defined.
ii
iii
iv
1
BASIC CONCEPTS
Objectives
At the end of this chapter, the reader should be able to:
Introduction
Literally speaking, a language is a vehicle of communication amongst human beings.
To communicate, a given procedure is followed which may normally be dictated by
grammar.
A computer language is not very different. Computers do not understand any of the
natural languages for transfer of data and instructions. So there are languages specially
developed so that you could pass your data and instructions to the computer to do a
specific job.
This chapter introduces structured approach to programming which the rest of this
guide is all about. The chapter also presents a quick overview of C language. The goal
is to give you sufficient working knowledge of C language so that you can understand
more concrete concepts in later chapters.
Programming defined
Programming is the translation of user ideas into a representation or form that can be
understood by the computer. The tools of writing programs are called programming
languages.
It involves forming a clear idea in terms of the information you want to include in the
program, computations needed and the output. At this stage, the programmer should
think in general terms, not in terms of some specific computer language.
Design program
The programmer decides how the program will go about its implementation, what
should the user interface be like, how should the program be organized, how to
represent the data and what methods to use during processing. At this point, you
should also be thinking generally although some of your decisions may be based on
some general characteristics of the C language.
You write a computer program with words and symbols that are understandable to
human beings. This is the editing part of the development cycle. You type the program
directly into a window on the screen and save the resulting text as a separate file. This
is often referred to as the source file The custom is that the text of a C program is
stored in a file with the extension .c for C programming language
Compiling
You cannot directly execute the source file. To run on any computer system, the source
file must be translated into binary numbers understandable to the computer's Central
Processing Unit. This process produces an intermediate object file - with the extension
.obj.
Linking
The main reason for linking is that many compiled languages come with library routines
which can be added to your program. Theses routines are written by the manufacturer
of the compiler to perform a variety of tasks, from input/output to complicated
mathematical functions. In the case of C the standard input and output functions are
contained in a library (stdio.h) so even the most basic program will require a library
function. After linking, the file extension is .exe which is an executable file.
Executable files
The text editor produces .c source files, which go to the compiler, which produces .obj
object files, which go to the linker, which produces .exe executable file. You can then
run .exe files as you run applications, simply by typing their names at the DOS prompt
or run using Windows menu.
This involves checking whether the system does what it is supposed to do. Programs
may have bugs (errors). Debugging involves the finding and fixing of program errors.
Occasionally, changes become necessary to make to a given program. You may think
of a better way to do something in a program, a clever feature or you may want to adapt
the program to run in a different machine. These tasks are simplified greatly if you
document the program clearly and follow good program design practices.
C language basic features
C is a general-purpose language which has been closely associated with the Unix
operating system for which it was developed - since the system and most of the
programs that run it are written in C.
It allows programmers to break down their programs into functions. It also supports the
use of comments, making programs readable and easily maintainable.
Efficiency
C is a concise language that allows you to say what you mean in a few words. The final
code tends to be more compact and runs quickly.
Portability
C programs written for one system can be run with little or no modification on other
systems.
Flexibility
It has (and still is) been used to solve problems in areas such as physics and
engineering.
Programmer orientation
C is oriented towards the programmers needs. It gives access to the hardware.
It lets you manipulate individual bits of memory. It also has a rich selection of
operators that allow you to expand programming capability.
C Program components
Keywords
These are reserved words that have special meaning in a language. The compiler
recognizes a keyword as part of the languages built in syntax and therefore it cannot
be used for any other purpose such as a variable or a function name. C keywords must
be used in lowercase otherwise they will not be recognized.
Examples of keywords
auto
default
float
register
struct
volatile
break
do
for
return
switch
while
case
double
goto
short
typedef
char
else
if
signed
union
const
enum
int
sizeof
unsigned
continue
extern
static
Preprocessor directives
Functions
Declaration statements
Comments
Expressions
Input and output statements
void
long
Example
This program will print out the message: This is a C program.
#include<stdio.h>
main()
{
printf("This is a C program \n");
return 0;
}
Though the program is very simple, a few points are worthy of note.
Every C program contains a function called main. This is the start point of the program.
#include<stdio.h> allows the program to interact with the screen, keyboard and file
system of your computer. You will find it at the beginning of almost every C program.
main() declares the start of the function, while the two curly brackets show the start
and finish of the function. Curly brackets in C are used to group statements together as
in a function, or in the body of a loop. Such a grouping is known as a compound
statement or a block.
printf("This is a C program \n"); prints the words on the screen. The text
to be printed is enclosed in double quotes. The \n at the end of the text tells the
program to print a new line as part of the output.
Most C programs are written in lower case letters. You will usually find upper case
letters used in preprocessor definitions (which will be discussed later) or inside quotes
as parts of character strings.
C is case sensitive, that is, it recognises a lower case letter and it's upper case
equivalent as being different.
The following program demonstrates the C program components.
#include<stdio.h>
main()
{
int num;
/* define a variable called num */
num = 1;
/* assignment */
printf(This is a simple program );
printf(to display a message. \n);
printf (My favorite number is %d because , num);
printf( it is first.\n );
return 0;
Functions
All C programs consist of one or more functions, each of which contains one or more
statements. In C, a function is a named subroutine that can be called by other parts of
the program. Functions are the building blocks of C.
A statement specifies an action to be performed by the program. In other words,
statements are parts of your program that actually perform operations.
All C statements must end with a semicolon. C does not recognize the end of a line as a
terminator. This means that there are no constraints on the position of statements within
a line. Also you may place two or more statements on one line.
Although a C program may contain several functions, the only function that it must have
is main( ).
The main( ) function is the point at which execution of your program begins. That is,
when your program begins running, it starts executing the statements inside the main( )
function, beginning with the first statement after the opening curly brace. Execution of
your program terminates when the closing brace is reached.
Another important component of all C programs is library functions. The ANSI C
standard specifies a minimal set of library functions to be supplied by all C compilers,
which your program may use. This collection of functions is called the C standard
library. The standard library contains functions to perform disk I/O (input / output), string
manipulations, mathematics, and much more. When your program is compiled, the
code for library functions is automatically added to your program.
One of the most common library functions is called printf( ). This is Cs general purpose
output function. Its simplest form is
printf(string to output);
The printf( ) outputs the characters that are contained between the beginning and
ending double quotes.
For example, printf( This is a C program );
The double quotes are not displayed on the screen. In C, one or more characters
enclosed between double quotes is called a string. The quoted string between printf( )s
parenthesis is called an argument to printf( ). In general, information passed to a
function is called an argument. In C, calling a library function such as printf( ) is a
statement; therefore it must end with a semicolon.
In the above program, line 6 causes the message enclosed in speech marks to be
printed on the screen. Line 7 does the same thing.
The \n in line 7 tells the computer to insert a new line after printing the message. \n is
an example of an escape sequence.
Line 8 prints the value of the variable num (1) embedded in the phrase. The %d
instructs the computer where and in what form to print the value. %d is a type
specifier used to specify the output format for integer numbers.
Line 9 has the same effect as line 7.
Line11 indicates the value to be returned by the function main( ) when it is executed.
By default any function used in a C program returns an integer value (when it is called
to execute). Therefore, line 2 could also be written int main( ). If the int keyword is
omitted, still an integer is returned.
Then, why return (0); ? Since all functions are subordinate to main( ), the function
does not return any value.
Note
(i)
(ii)
Since the main function does not return any value, line 3 can alternatively be
written as : void main( ) void means valueless. In this case, the statement
return 0; is not necessary.
While omitting the keyword int to imply the return type of the main( ) function
does not disqualify the fact that an integer is returned (since int is default),
you should explicitly write it in other functions, especially if another value
other than zero is to be returned by the function.
Declaration statements
In C, all variables must be declared before they are used. Variable declarations ensure
that appropriate memory space is reserved for the variables, depending on the data
types of the variables. Line 4 is a declaration for an integer variable called num.
Assignment and expression statements
An assignment statement uses the assignment operator = to give a variable on the
operators left side the value to the operators right or the result of the expression on the
right. The statement num =1; (Line 5) is an assignment statement.
Escape sequences
Escape sequences (also called back slash codes) are character combinations that
begin with a backslash symbol (\) used to format output and represent difficult-to-type
characters.
One of the most important escape sequences is \n, which is often referred to as the new
line character. When the C compiler encounters \n, it translates it into a carriage return.
Example
The program below displays the following output on the screen.
#include<stdio.h>
main()
{
printf(This is line one \n);
printf(This is line two \n);
printf(This is line three);
return 0;
}
Example
The program below sounds the bell.
#include<stdio.h>
main()
{
printf(\a);
return 0;
}
Remember that the escape sequences are character constants. Therefore to assign
one to a character variable, you must enclose the escape sequence within single
quotes, as shown in this fragment.
char ch;
ch = \t
Meaning
\a
alert/bell
\b
backspace
\n
new line
10
*/
\v
vertical tab
\t
horizontal tab
\\
back slash
Single quote ()
Double quote ()
\0
null
Missing semi colon at the end of a statement e.g. Area = Base * Length
Use of an undeclared variable in an expression
Illegal declaration e.g. int x, int y, int z;
Use of a keyword in uppercase e.g. FLOAT, WHILE
Misspelling keywords e.g. init instead of int
Note
The compiler may suggest that other program line statements have errors when they
may not. This will be the case when a syntax error in one line affects directly the
execution of other statements, for example multiple use of an undeclared variable.
Declaring the variable will remove all the errors in the other statements.
Logic Errors
These occur from the incorrect use of control structures, incorrect calculation, or
omission of a procedure. Examples include: An indefinite loop in a program, generation
of negative values instead of positive values. The compiler will not detect such errors
since it has no way of knowing your intentions. The programmer must dry run the
program so that he/she can compare the programs results with already known results.
11
Semantic errors
They are caused by illegal expressions that the computer cannot make meaning of.
Usually no results will come out of them and the programmer will find it difficult to debug
such errors. Examples include a data overflow caused by an attempt to assign a value
to a field or memory space smaller than the value requires, division by zero, etc.
Guidelines to good C programming
Ensure that your program logic design is clear and correct. Avoid starting to code
before the logic is clearly set out .Good logic will reduce coding time and result in
programs that are easy to understand, error free and easily maintainable.
Use sensible names for variables. Use of general names such as n instead of
net_sal for net salary makes variables vague and may make debugging difficult.
This however depends on the programmers ingenuity.
Dry run you design with some simple test data before running the code, then
compare the two.
Revision Exercise
1. Outline the logical stages of C programs development.
2. From the following program, suggest the syntax and logical errors that may have
been made.
The program is supposed to find the square and cube of 5, then output 5, its square
and cube.
#include<stdio.h>
main()
{
12
13
2
DATA HANDLING
Objectives
At the end of this chapter, the reader should be able to:
Introduction
Almost every program you write performs some sort of data manipulation, however
basic. These data are stored and understood differently in your program depending on
how you have defined them.
The aim of this chapter is to help you understand what is required to use data items in a
C program.
Variables
Variable defined
A variable is a memory location whose value can change during program execution. In
C, a variable must be declared before it can be used. Variables can be declared at the
start of any block of code.
A declaration begins with the data type, followed by the name of one or more variables.
That is:
datatype variable; or
datatype variable1,variable2,.variablen; (where there are n variables)
For example,
int high, low, results[20];
14
Declarations can be spread out, allowing space for an explanatory comment. That is:
Variables can also be initialised when they are declared. This is done by adding an
equals sign and the required value after the declaration.
int high = 250;
int low = -40;
int results[20];
/* Maximum Temperature */
/* Minimum Temperature */
/* Series of temperature readings */
Variable names
Every variable has a name and a value. The name identifies the variable and the value
stores data. There is a limitation on what these names can be. Every variable name in
C must start with a letter; the rest of the name can consist of letters, numbers and
underscore characters.
C recognizes upper and lower case characters as being different (C is case- sensitive).
Finally, you cannot use any of C's keywords like main, while, switch etc as variable
names.
Examples of legal variable names
x
x1
power
result
x2
impetus
outfile
out_file
gamma
bestyet
best_yet
hi_score
It is conventional to avoid the use of capital letters in variablenames. These are used for
names of constants. Some old implementations of C only use the first 8 characters of a
variable name. Most modern ones don't apply this limit though. The rules governing
variable names also apply to the names of functions, to be covered in chapter 5.
Basic data types
C supports five basic data types. The table below shows the five types, along with the C
keywords that represent them. Dont be confused by void. This is a special purpose
data type used to explicitly declare functions that return no value.
Type
Character
Integer
Float
Double
Void
Meaning
Character data
Signed whole number
floating-point numbers
double precision floatingpoint numbers
Valueless
15
Keyword
Char
Int
Float
Double
Void
16
displays This prints the number 99 on the screen. As you can see, this call to the
printf( ) function contains two arguments. The first one is the quoted string and the other
is the constant 99. Notice that the arguments are separated from each other by a
comma.
In general, when there is more than one argument to a function, the arguments are
separated from each other by commas. The first argument is a quoted string that may
contain either normal characters or format specifiers that begin with a percent (%) sign.
Normal characters are simply displayed as is on the screen in the order in which they
are encountered in the string (reading left to right). A format specifier, on the other hand
informs printf( ) that a different type item is being displayed. In this case, the %d,
means that an integer is to be output in decimal format. The value to be displayed is to
be found in the second argument. This value is then output at the position at which the
format specifier is found on the string.
If you want to specify a character value, the format specifier is %c. To specify a floatingpoint value, use %f. The %f works for both float and double. Keep in mind that the
values matched with the format specifier need not be constants (such as 99 in the
printf statement above). They may be variables too.
Code
Format
%c
Character
%d
%i
%e
%E
%f
%s
String of characters
%u
%x
%X
Examples
1. The program shown below illustrates the above concepts. First, it declares a variable
called num. Second, it assigns this variable the value 100. Finally, it uses printf( ) to
display the value is 100 on the screen. Examine it closely.
17
#include<stdio.h>
main()
{
int num;
num = 100;
printf( The value is %d , num);
return 0;
}
2. This program creates variables of types char, float, and double assigns each a
value and outputs these values to the screen.
#include<stdio.h>
main()
{
char ch;
float f;
double d;
ch = X;
f = 100.123;
d = 123.009;
printf( ch is %c , ch);
printf( f is %f , f);
printf( d is %f , d);
return 0;
}
Exercise
Enter, compile, and run the two programs above.
Inputting values from the keyboard using scanf( )
There are several ways to input values through the keyboard. One of the easiest is to
use another of Cs standard library functions called scanf( ).
To use scanf( ) to read an integer value from the keyboard, call it using the general
form:
scanf(%d, &int_var-name);
Where int-var-name is the name of the integer variable you wish to receive the value.
The first argument to scanf( ) is a string that determines how the second argument will
be treated. In this case the %d specifies that the second argument will be receiving an
integer value entered in decimal format. The fragment below, for example, reads an
integer entered from the keyboard.
18
int num;
scanf(%d, &num);
The & preceding the variable name means address of. The values you enter are put
into variables using the variables location in memory.
When you enter a number at the keyboard, you are simply typing a string of digits. The
scanf( ) function waits until you have pressed <ENTER> before it converts the string
into the internal format used by the computer.
The table below shows format specifiers or codes used in the scanf() function and their
meaning.
Code
Meaning
%c
%d
%i
%e
%f
%lf
Read a double
%s
Read a string
%u
Examples
1. This program asks you to input an integer and a floating-point number and displays
the value.
#include<stdio.h>
main()
{
int num;
float f;
printf( \nEnter an integer: );
scanf( %d , &num);
printf(\n Enter a floating point number: );
scanf( %f , &f);
printf( %d , num);
printf( \n %f , f);
return 0;
}
19
2. This program computes the area of a rectangle, given its dimensions. It first prompts
the user for the length and width of the rectangle and then displays the area.
#include<stdio.h>
main()
{
int len, width;
printf(\n Enter length: );
scanf (%d , &len);
printf(\n Enter width : );
scanf( %d , &width);
printf(\n The area is %d , len
return 0;
}
* width);
Exercise
Enter, compile and run the example programs.
Types of variables
There are two places where variables are declared: inside a function or outside all
functions.
Global variables
Variables declared outside all functions are called global variables and they may be
accessed by any function in your program. Global variables exist the entire time your
program is executing.
Local variables
Variables declared inside a function are called local variables. A local variable is
known to and may be accessed by only the function in which it is declared. You need to
be aware of two important points about local variables.
(i) The local variables in one function have no relationship to the local variables in
another function. That is, if a variable called count is declared in one function,
another variable called count may also be declared in a second function the two
variables are completely separate from and unrelated to one another.
(ii) Local variables are created when a function is called, and they are destroyed when
the function is exited. Therefore local variables do not maintain their values between
function calls.
Storage classes
C storage classes determine how a variable is stored. The storage class specifiers are
20
auto
extern
register
static
21
File 2
#include<stdlib.h>
void f1(void)
{
count = rand ( );
}
If you try to compile File 2, an error will be reported because count is not defined.
However, you cannot change File 2 as follows:
#include<stdlib.h>
int count;
void f1(void)
{
count = rand ();
}
But there is still a problem, If you declare count a second time, the linker will report a
duplicate-symbol error, which means that count is defined twice, and the linker doesnt
know which to use.
The solution to this problem is Cs extern specifier. By placing extern in front of
counts declaration in File 2, you are telling the compiler that count is an integer
declared elsewhere. In other words, using extern informs the compiler about the
existence and type of the variable it precedes but does not cause storage for that
variable to be allocated. The correct version for File 2 is:
#include<stdlib.h>
extern int count;
void f1(void)
{
count = rand ( );
}
Note: stdlib.h is a header file that contains certain standard library functions. rand()
function is one of them and is used to generate a random number between . Others are
abort() to abort a program, abs() to get the absolute value ,malloc() for dynamic
memory allocation ,free() to free memory allocated with malloc(), qsort()to sort an array,
realloc() to reallocate memory, et al.
register
register is used to define local variables that should be stored in a register instead of
Random Access Memory (RAM). For example:
register int
Miles;
22
register should only be used for variables that require quick access - such as counters.
It should also be noted that defining 'register' goes not mean that the variable will be
stored in a register. It means that it might be stored in a register - depending on
hardware and implementation restrictions.
static
The static modifier causes the contents of a local variable to be preserved between
function calls. Also, unlike normal local variables, which are initialized each time a
function is entered a static variable is initialized only once. For example, take a look at
the following program:
#include<stdio.h>
void f(void);
main()
{
int i;
for (i =0; i < 10; i ++)
f( );
return 0;
}
void f (void)
{
static int count = 0;
count ++;
printf(Count is %d \n, count);
}
which displays the following output.
Visibly from above, count retains its value between function calls.
Constants
A constant is a value that does not change during program execution. In other words,
constants are fixed values that may not be altered by the program.
23
Integer constants are specified as numbers without fractional components. For example
10, 1000 are integer constants.
Floating - point constants require the use of the decimal point followed by the numbers
fractional component. For example, 11.123 is a floating point constant. C allows you to
use scientific notation for floating point numbers. Constants using scientific notation
must follow this general form:
number E sign exponent
The number is optional. Although the general form is shown with spaces between the
component parts for clarity, there may be no spaces between parts in an actual number.
For example, the following defines the value 1234.56 using scientific notation.
123.456E1
Character constants are usually just the character enclosed in single quotes; 'a', 'b', 'c'.
For example:
ch = z;
There is nothing in C that prevents you from assigning a character variable a value
using a numeric constant. For example the ASCII Code for A is 65. Therefore, give the
declaration:
char ch;
then these two assignments are equivalent.
ch = A;
ch = 65;
Types of constants
Constants can be used in C expressions as:
Direct constants
Symbolic constants
Direct constants
Here the constant value is inserted in the expression, as it should typically be.
For example:
Area = 3.14 * Radius * Radius;
24
The value 3.14 is used directly to represent the value of PI which never requires
changes in the computation of the area of a circle
Symbolic constant
This involves the use of another C preprocessor, #define.
For example, #define SIZE 10
A symbolic constant is an identifier that is replaced with replacement text by the C
preprocessor before the program is compiled. For example, all occurrences of the
symbolic constant SIZE are replaced with the replacement text 10.
This process is generally referred to as macro substitution. The general form of the
#define statement is;
#define macro-name string
Notice that this line does not end in a semi colon. Each time the macro - name is
encountered in the program, the associated string is substituted for it. For example,
consider the following program.
Example: Area of a circle
#include<stdio.h>
#define PI 3.14
main()
{
float radius, area;
printf(Enter the radius of the circle \n);
scanf(%f, &radius);
area = PI * radius * radius; /* PI is a symbolic constant */
printf(Area is %.2f cm squared ,area);
return 0;
}
At the time of the substitution, the text such as 3.14 is simply a string of characters
composed of 3, ., 1 and 4. The preprocessor does not convert a number into any sort of
internal format. This is left to the compiler.
The macro name can be any valid C identifier. Although macro names can appear in
either uppercase or lowercase letters, most programmers have adopted the convention
of using uppercase for macro names to distinguish them from variable names. This
makes it easy for anyone reading your program to know when a macro name is being
used.
25
Macro substitutions are useful in that they make it easier to maintain programs. For
example, if you know that a value, such as array size, is going to be used in several
places in your program, it is better to create a macro for this value. Then, if you ever
need to change this value, you simply change the macro definition. All references will
be automatically changed when the program is recompiled.
Revision Exercise
1. Discuss four fundamental data types supported by C, stating how each type is stored
in memory.
2. Distinguish between a variable and a constant.
3. Suggest, with examples two ways in which constant values can be used in C
expression statements.
4. Give the meaning of the following declarations;
(i) char name[20];
(ii) int num_emp;
(iii) double tax, basicpay;
(iv) char response;
5. What is the difference between a local and a global variable?
6. Write a program that computes the number of seconds in a year.
The mass of a single molecule of water is about 3.0 x 10-23 grams. A quart of
water is about 950 grams. Write a program that requests an amount of water in
quarts and displays the number of water molecules in that amount.
7. Write a program that declares one integer variable called num. Give this variable the
1000 and then, using one printf ( ) statement, display the value on the screen like
this:
1000 is the value of num
8. Write a program that inputs two floating-point numbers (use type float) and then
displays their sum.
9. Write a program that computes the volume of a cube. Have the program prompt the
user for each dimension.
10. Write a program that inputs an integer from the keyboard and displays its square
11. Write a program that reads your first name and surname when you enter them.
Each part of your name should not be more than 12 characters. Finally, have the
program redisplay your full name.
26
3
OPERATORS
Objectives
At the end of this chapter, the reader should be able to:
Introduction
How does a C programmer tell a program to perform a calculation, compare values and
so on. This requires one to know the symbols associated with these tasks, for example
+ for addition, > for checking whether one value is greater than another value, = for
assignment a value to a variable and so on.
This chapter takes you through the basic symbols so that you may be able to construct
various expressions, for example writing a Mathematical formula as a C statement, in
the programs you will be developing.
Operators versus operands
An operator is a component or symbol of any expression that joins individual constants,
variables, array elements and function references.
An operand is a data item that is acted upon by an operator. Some operators act upon
two operands (binary operators) while others act upon only one operand (unary
operators).
An operand can be a constant value, a variable name or a symbolic constant.
An expression is a combination of operators and operands.
Examples
(i) x + y ; x, y are operands, + is an addition operator.
(ii) 3 * 5; 3, 5 are constant operands, * is a multiplication operator.
(iii) x % 2.5; x, 5 are operands, % is a modulus (remainder) operator.
27
Purpose
+
*
/
%
Addition
Subtraction
Multiplication
Division
Remainder after integer division
Note:
(i)
(ii)
(iii)
(iv)
(v)
If one or both operands represent negative values, then the addition, subtraction,
multiplication, and division operators will result in values whose signs are determined by
their usual rules of algebra. Thus if a b, and c are 11, -3 and 11 respectively, then
a+b =8
a b = 14
a * b = -33
a / b = -3
a % b = -2
c % b = -2
c/b=3
Examples of floating point arithmetic operators
28
double) the lower precision operand will be converted to the precision of the other
operand, and the result will be expressed in this higher precision. (Thus if an
expression has a float and a double operand, the result will be a double).
(ii) If one operand is a floating-point type (e.g. float, double or long double) and the
other is a character or integer (including short or long integer), the character or
integer will be converted to the floating point type and the result will be expressed as
such.
(iii) If neither operand is a floating-point type but one is long integer, the other will be
converted to long integer and the result is expressed as such. (Thus between an int
and a long int, the long int will be taken).
(iv) If neither operand is a floating type or long int, then both operands will be converted
to int (if necessary) and the result will be int (compare short int and long int)
From the above, evaluate the following expressions given:
i =7, f = 5.5, c = w. State the type of the result.
(i)
(ii)
(iii)
(iv)
i+f
i+c
i + c-w
( i + c) - ( 2 * f / 5)
The variable is too small to hold the value. In this case it will be corrupted (this is
bad).
The variable is an integer type and is being assigned a real value. The value is
rounded down. This is often done deliberately by the programmer.
29
Values passed as function arguments must be of the correct type. The function has no
way of determining the type passed to it, so automatic conversion cannot take place.
This can lead to corrupt results. The solution is to use a method called casting which
temporarily disguises a value as a different type.
For example, the function sqrt finds the square root of a double.
int i = 256;
int root;
root = sqrt( (double) i);
The cast is made by putting the bracketed name of the required type just before the
value, (double) in this example. The result of sqrt( (double) i) is also a double, but this is
automatically converted to an int on assignment to root.
Operator precedence
The order of executing the various operations makes a significant difference in the
result. C assigns each operator a precedence level. The rules are;
(i) Multiplication and division have a higher precedence than addition and subtraction,
so they are performed first.
(ii) If operators of equal precedence; (*, /), (+, -) share an operand, they are executed in
the order in which they occur in the statement. For most operators, the order
(associativity) is from left to right with the exception of the assignment ( = ) operator.
Consider the statement:
butter = 25.0 + 60.0 * n / SCALE;
Where n = 6.0 and SCALE = 2.0.
The order of operations is as follows;
First:
60.0 * n = 360.0
(Since * and / are first before + but * and / share the operand n with * first)
Second:
30
(25+
60.0 * n) / SCALE?
Try changing the order of evaluation by shifting the parenthesis and note the change in
the top score.
Analyse the sample output below. Run the program with different values.
31
Note
(i)
(ii)
(iii)
(iv)
Relational operators
There are four relational operators in C.
<
<=
>
>=
Less than
Less than or equal to
Greater than
Greater than or equal to
Example
Consider a, b, c to be integers with values 1, 2,3 respectively. Note their results with
relational operators below.
32
Expression
Result
a<b
(a+ b) > = c
(b + c) > (a+5)
c:=3
b==2
1 (true)
1 (true)
0 (false)
0 (false)
1 (true)
Logical operators
&&
||
!
Logical AND
Logical OR
NOT
The two operators act upon operands that are themselves logical expressions to
produce more complex conditions that are either true or false.
Example
Suppose i is an integer whose value is 7, f is a floating point variable whose value is 5.5
and C is a character that represents the character w, then;
(i > = = 6 ) && ( C = = w ) is 1 (true)
( C > = 6 ) || (C = 119 ) is 1 (true)
(f < 11 ) && (i > 100) is 0 (false)
(C! = p) || ((i + f) < = 10 ) is 1 (true)
33
Unary operators
These are operators that act on a singe operand to produce a value. The operators may
precede the operand or are after an operand.
Examples
(i)
(ii)
(iii)
(iv)
Run the program and analyse the results. You can later modify program to make the
variable n a different data type and other appropriate changes, then run it.
Revision Exercise
1. Describe with examples, four relational operators.
2. What is operator precedence? Give the relative precedence of arithmetic operators.
3. Suppose a, b, c are integer variables that have been assigned the values a =8, b = 3
and c = - 5, x, y, z are floating point variables with values x =8.8, y = 3.5, z = -5.2.
Further suppose that c1, c2, c3 are character-type variables assigned the values E, 5
and ? respectively.
34
a/b
2 * b + 3 * (a c)
(a * c) % b
(x / y) + z
x%y
2 * x / (3 * y)
c1 / c3
(c1 / c2) * c3
6. Identify errors in the following programs and write error free programs.
(i)
35
#include <stdio.h>
#include <math.h>
#define PI = 3.142
VOID main[ ]
{
/* Radius, area and perimeter are declared to be floats /*
Float r, area, perimeter,
printf(\n Enter the radius of the circle - > \n);
scanf(%f,&r);
area = PI * pow(r, 2)
perimeter = 2 * PI * r;
/* Result is printed after the computation */
printf( Area = %f Perimeter = %d /n, area, perimeter);
}
(ii)
#include <stdio.h>
Main( )
{
cows, legs, integer;
printf(How many cow legs did you count ? \n;
Scanf(%c, legs);
cows = legs/4;
printf(That implies that there are %f cows. \n, cows)
}
36
4
CONTROL STRUCTURES
Objectives
At the end of this chapter, the reader should be able to:
Introduction
Control structures represent the forms by which statements in a program are executed.
Three structures control program execution:
Sequence
Selection or decision structure
Iteration or looping structure
Basically, program statements are executed in the sequence in which they appear in
the program.
In reality, a logical test using logical and relational operators may require to be used in
order to determine which actions to take (subsequent statements to be executed)
depending on the outcome of the test. This is selection. For example:
if (score >= 50)
printf(Pass);
else
printf(Fail);
In addition, a group of statements in a program may have to be executed repeatedly
until some condition is satisfied. This is known as looping. Suppose you were asked to
display "Hello World!" five times on the screen. You would probably write the following
program.
37
#include <stdio.h>
main()
{
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
}
Indeed, this does exactly what was asked. But suppose you were asked to do the same
job for 100 Hello Worlds or, if you're still willing to type that much code, maybe 1,000
Hello Worlds. You will realise that that this is not a sensible method!
What you really need is some way of repeating the printf statements without having to
write it out each time. This is the purpose of a loop.
The if statement
The if statement provides a junction at which the program has to select which path to
follow. The general form is :
if(expression)
statement;
If expression is true (i.e. non zero) , the statement is executed, otherwise it is skipped.
Normally the expression is a relational expression that compares the magnitude of two
quantities ( For example x > y or c = = 6)
Examples
(i) if (x<y)
printf(x is less that y);
(ii) if (salary >500)
tax-amount = salary
1.5;
38
{
block of statements;
}
Example
if(salary>5000)
{
tax_amt = salary *1.5;
printf(Tax charged is %f, tax_amt);
}
The if - else statement
The if else statement lets the programmer choose between two statements as opposed
to the simple if statement which gives you the choice of executing a statement
(possibly compound) or skipping it.
The general form is:
if (expression)
statement1;
else
statement2;
If expression is true, statement1 is executed. If expression is false, the single
statement following the else (statement2) is executed. The statements can be
simple or compound.
Note: Indentation is not required but it is a standard style of programming.
Example
if(x >=0)
{
printf(let us increment x:\n);
x++;
}
else
printf(x
< 0 \n);
39
/*ten percent/
else if (sal_amt>=5000&&sal_amt<1000 )
printf(The discount is %f,sal_amt*0.07 ); /*seven percent */
else if(sal_amt=3000&&sal_amt<5000)
{
Disc = sal_amt * 0.05;
/* five percent
40
\n);
*/
41
}
else
break;
case 3:
/* label 3 */
printf(\n You typed 3);
break;
case 4:
/* label 4 */
printf( \n You typed 4);
break;
default:
printf(There is no match in your choice);
/* End of switch */
printf(Your choice is out of range);
return (0);
/* End of main */
Explanation
The expression in the parenthesis following the switch is evaluated. In the example
above, it has whatever value we entered as our choice.
Then the program scans a list of labels (case 1, case 2,. case 4) until it finds one that
matches the one that is in parenthesis following the switch statement.
If there is no match, the program moves to the line labeled default, otherwise the
program proceeds to the statements following the switch.
The break statement causes the program to break out of the switch and skip to the next
statement after the switch. Without the break statement, every statement from the
matched label to the end of the switch will be processed.
For example if we remove all the break statements from the program and then run the
program using the number 3 we will have the following exchange.
42
case constant 2:
statement; optional
default: (optional)
statement; (optional)
}
Note
(i) The switch labels (case labels) must be type int (including char) constants or
constant expression.
(ii) You cannot use a variable for an expression for a label expression.
(iii) The expressions in the parenthesis should be one with an integer value. (again
including type char)
Example: Demonstrating the switch structure
#include<stdio.h>
main()
{
char ch;
printf(Give me a letter of the alphabet \n);
printf(An animal beginning with letter);
printf (is displayed \n );
scanf(%c, &ch);
if (ch>=a && ch<=z)
/*lowercase letters only */
switch (ch)
{
/*begin of switch*/
case `a`:
printf(Alligator , Australian aquatic animal \n):
break;
case b:
printf(Barbirusa, a wild pig of Malaysia \n);
break;
case c:
printf(Coati, baboon like animal \n);
break;
case d:
printf(Desman, aquatic mole-like creature \n);
break;
default:
printf( That is a stumper! \n)
}
else
printf(I only recognize lowercase letters.\n);
43
return 0;
/* End of main
*/
Looping
C supports three loop versions:
while loop
do while loop
for loop.
(iii)
(iv)
Carry out the following repeatedly (as long as the count is less or equal to n).
(a) Read in a number, say x.
44
(vi)
Program code:
/* To add numbers and compute the average */
#include<stdio.h>
main()
{
int n, count = 1;
float x, average, sum=0.0;
/* initialise and read in a value of n */
printf(How many numbers? );
scanf(%d, &n);
/*Read in the number */
while (count<=n)
{
printf(x = );
scanf(%f, &x);
sum+=x;
count++;
}
/* Calculate the average and display the answer */
average = sum/n;
printf(\n The average is %f \n, average);
return 0;
}
Note that using the while loop, the loop test is carried out at the beginning of each loop
pass.
The do .. while loop
It is used when the loop condition is executed at the end of each loop pass.
It takes the form:
do
statement;
while(expression);
The statement (simple or compound) will be executed repeatedly as long as the value
of the expression is true. (i.e. non zero).
45
Notice that since the test comes at the end, the loop body (statement) must be
executed at least once.
Rewriting the program that counts from 0 to 9, using the do while loop:
/* counter1.c */
/* Displays the digits 1 through 9 */
main()
{
int digit=0; /* Initialisation */
do
{
printf(%d \n, digit);
digit++;
} while (digit<=9);
return 0;
}
Exercise
Rewrite the program that computes the average of n numbers using the do while loop.
The for loop
This is the most commonly used looping statement in C.
It takes the form:
for (expression1;expression2;expression3)
statement;
where:
expression1 is used to initialize some parameter (called an index). The index
controls the loop action. It is usually an assignment operator.
expression2 is a test expression, usually comparing the initialised index in
expression1 to some maximum or minimum value.
expression3 is used to alter the value of the parameter index initially assigned by
expression and is usually a unary expression or assignment operator;
Example
for(int k=0;k<=5; k++)
printf(k = %d \n, k);
46
47
You can count by any number you wish; twos threes, etc.
You can test some condition other than the number of operators.
Here's an example of use for the break statement. The program for loop is infinite. The
loop is terminated when the break statement is executed, which happens upon the user
inputting the value 10 to the variable t.
48
#include <stdio.h>
main()
{
int t ;
for ( ; ; )
{
printf(Value of t: );
scanf("%d" , &t)
;
if ( t==10 )
break ;
}
printf("End of an infinite loop...\n");
}
Alternative 2
if (a>14)
goto a;
sheds=2;
sheds=3;
else
goto b;
a: sheds=3;
sheds=2;
k=2*sheds;
b: k=2 * sheds;
Nesting statements
49
It is possible to embed (place one inside another) control structures, particularly the if
and for statements.
Nested if statement
It is used whenever choosing a particular selection leads to an additional choice.
Example
if (number>6)
if (number<12)
printf(You are very close to the target!);
else
printf(Sorry, you lose!);
50
printf(x = );
scanf(%f, &x);
sum+=x;
}
/* End of inner loop */
/* Calculate the average and display the answer */
average = sum/n;
printf(\n The average is %f \n, average);
/*End of outer loop */
return 0;
Revision Exercise
1. A retail shop offers discounts to its customers according to the following rules:
Purchase Amount >= Ksh. 10,000 - Give 10% discount on the amount.
Ksh. 5, 000 <= Purchase Amount < Ksh. 10,000 - Give 5% discount on the amount.
Ksh. 3, 000 <= Purchase Amount < Ksh. 5,000 - Give 3% discount on the amount.
0 > Purchase Amount < Ksh. 3,000 - Pay full amount.
Write a program that asks for the customers purchase amount, then uses if
statements to recommend the appropriate payable amount. The program should
cater for negative purchase amounts and display the payable amount in each case.
2. In what circumstance is the continue statement used in a C program?
3. Using a nested if statement, write a program that prompts the user for a number and
then reports if the number is positive, zero or negative.
4. Write a while loop that will calculate the sum of every fourth integer, beginning with
the integer 3 (that is calculate the sum 3 + 7 +11 + 15 + ...) for all integers that are
less than 30.
5. Write a program that prints only the odd numbers between 1 and 100. Use a for loop
that looks this:
for(i=1; i<101; i++)....
Use a continue statement to avoid printing even numbers.
6. Write a program that computes the area of a circle, rectangle or triangle using an
if..else if ladder.
7. Write a program that requests two numbers and then displays their sum or product,
depending upon what the user selects.
8. Write a program that asks the user for an integer and then tells the user if that
number is even or odd. (Hint: Use Cs modulus operator %)
51
52
5
FUNCTIONS
Objectives
At the end of this chapter, the reader should be able to:
Introduction
A function is a self-contained program segment that carries out some specific well defined task. Every C program consists of one or more functions. One of these
functions must be called main. Execution of the program will always begin by carrying
out the instructions in main. Additional functions will be subordinate to main, and
perhaps to one another.
If a program contains multiple functions, their definitions may appear in any order,
though they must be independent of one another. That is, one function definition cannot
be embedded within another.
For example many programs require that a particular group of instructions be accessed
repeatedly from several different places in the program. The repeated instructions can
be placed within a single function which can then be accessed whenever it is needed.
Moreover a different set of data can be transferred to the function each time it is
53
accessed . Thus the use of a function eliminates the need for redundant programming
of the same instructions.
Equally important is the logical clarity resulting from the decomposition of a program
into several concise functions where each function represents some well-defined part of
the overall problem. Such programs are easier to write and debug and their logical
structure is more clear than programs which lack this type of structure. This is especially
true of lengthy, complicated programs. Most C programs are therefore modularised in
this manner, even though they may not involve repeated execution of some task. In fact
the decomposition of a program into individual program modules is generally
considered to be good programming practice.
Program libraries
54
Defining a function
The general form of a C function is:
type function_name(parameter- list)
{
statements
}
type specifies the return type of a function. A function can return any type of data. If no
data type specifier is present, the C compiler assumes that the function is returning an
integer. In other words int is the default type when no type specifier is present.
However, when a function returns a type other than int, it must be explicitly declared.
For example, this function returns a double.
/* Compute the area of a rectangle */
double rect_area(double side1, double side 2)
{
return(side1*side2);
}
function_name represents any meaningful descriptive tag you will assign to the
function for example rect_area above.
parameter-list represents the variables used in the function body (if any). If more
than one, they are listed inside the parenthesis with each parameter (also known as an
argument) preceded by its data type as it is done in a declaration statement. Hence in
the previous example, side1 and side2 are the parameters of the rect_area function.
statements is the sequence of statements that make up the function body. If the
function is expected to return some value, the sequence will have a return statement
(discussed shortly), otherwise (void type functions), there wont be any need for a return
statement.
55
The arguments are called formal arguments because they represent the names of
data items that are transferred into the function from the calling portion of the program.
They are also known as parameters or formal parameters.
The corresponding arguments in the function reference are called actual arguments
since they define the data items that are actually transferred). Each formal argument
must be of the same data type as the data item it receives from the calling portion of the
program.
Information is returned from the function to the calling portion of the program via a
return statement. The return statement also causes the program logic to return to the
point from which the function was accessed.
In general terms the return statement is written as:
return (expression);
Only one expression can be included in the return statement. Thus, a function can
return only one value to the calling portion via return.
56
assigned an initial value of 1 though its value is recalculated within a for loop. The final
value of prod which is returned by the function represents the desired value of n
factorial (n!).
If the data type specified in the first line is inconsistent with the expression appearing in
the return statement, the compiler will attempt to convert the quantity represented by
the expression to the data type specified in the first line. This could result in a
compilation error or it may involve a partial loss in data (due to truncation).
Inconsistency of this type should be avoided at all costs.
The keyword void can be used as a type specifier when defining a function that does
not return anything or when the function definition does not include any arguments. The
presence of this keyword is not mandatory but it is good programming practice to make
use of this feature.
Consider a function that accepts two integer quantities, determines the larger of the two
and displays it (the larger one). This function does not return anything to the calling
portion. Therefore the function can be written as;
void maximum (int x, int y)
{
int z;
z = (x >= y)? x : y;
printf(\n \n maximum value
= %d , z),
}
Accessing a function
A function can be accessed by specifying its name followed by a list of arguments
enclosed in parenthesis and separated by commas. If the function call does not require
any arguments an empty pair of parenthesis must follow the name of the function. The
function call may be part of a simple expression (such as an assignment statement), or
it may be one of the operands within an expression.
The arguments appearing in the function call are referred to as actual arguments in
contrast to the formal arguments that appear in the first line of the function definition.
(They are also known as actual parameters or arguments). In a normal function call,
there will be one actual argument for each formal argument. Each actual argument must
be of the same data type as its corresponding formal argument. Remember that it is the
value of each actual argument that is transferred into the function and assigned into the
corresponding formal argument.
57
There may be several different calls to the same function from various places within a
program. The actual arguments may differ from one function call to another. Within
each function call however the actual arguments must correspond to the formal
arguments in the functions definition; i.e. the number of actual arguments must be the
same as the number of formal arguments and each actual argument must be of the
same data type as its corresponding formal argument.
Example: Determining the maximum of two integers (Complete program)
The following program determines the largest of three integers quantities. The program
makes use of a function that determines the larger of two integer quantities. The overall
strategy is to determine the larger of the first two quantities and then compare the value
with the third quantity. The largest quantity is then displayed by the main part of the
program.
/*Determine the largest of the three integer quantities*/
#include<stdio.h>
int maximum (int x, int y) /*Determine the larger of two quantities*/
{
int z;
z = (x > = y )? x : y;
return(z);
}
main()
{
int a , b , c ,d;
/*read the integer quantities*/
printf(\n a = );
scanf(%d, &a);
printf(\n b = );
scanf(%d, &b);
printf(\n c = );
scanf(%d, &c);
/* Calculate and display the maximum value */
d = maximum (a, b);
printf (\n \n maximum = % d , maximum (c ,d));
return 0;
}
The function maximum is accessed from two different places in main. In the first call to
maximum, the actual arguments are the variables a and b whereas in the second call,
the arguments are c and d. (d is a temporary variable representing the maximum value
of a and b).
Note the two statements in main that access maximum, i.e.
d = maximum (a, b);
printf( \n \n maximum = %d , maximum (c, d));
58
function-name (parameter-list);
Where type represents the type of the item that is returned by the function,
function-name represents the name of the function, parameter-list represents
the types and names of the arguments used in executing the function.
For example, the statement void square(int number); is a prototype for the
function square
Note that a function prototype resembles the first line of a function definition (although a
definition prototype ends with a semicolon).
The names of the argument(s) can be omitted (though it is not a good idea to do so).
However the arguments data types are essential. Hence, the prototype for the function
square may also be written as void square(int);
59
Although function prototypes are not mandatory in C, they are however desirable
because they facilitate error checking between the calls to a function and the
corresponding function definition. Since a prototype is a model of a function, the
compiler uses it to check each of your calls to the function and determine if you have
used the correct number of arguments in the function call and if they are of the correct
type.
60
Recursion
Recursion is the process by which a function calls itself repeatedly until a special
condition is satisfied.
For a problem to be solved using recursion, two conditions must be satisfied. These
are:
(i) The solution must be written in a recursive form i.e. it should be possible to express
the solution in form of itself.
(ii) There must be a stopping case or a simple solution. This is the terminating condition.
We reuse the factorial function to illustrate this.
The factorial of any possible integer can be expressed as;
n ! =n * (n-1) * (n-2) ** 1.
e.g. 5 ! = 5 * 4 * 3 * 2 * 1.
However we can rewrite the expression as; 5! = 5 * 4!
Or generally,
n! = n * (n 1)!
(Recursive statement)
This is to say that in the factorial example, the calculation of n is expressed in form of a
previous result (condition (i) is satisfied).
Secondly 1! = 1 by definition, therefore condition (ii) is satisfied i.e. a stopping case.
Example: Using the factorial function in recursive form.
#include<stdio.h>
long int factorial (int n);
/*factorial function prototype*/
main()
{
int n;
/*Read in the integer quantity*/
printf (n = );
scanf (%d , &n);
/*Calculate and display the factorial*/
printf (n! =%d \n, factorial (n));
return 0;
}
/* Function definition */
long int factorial (int n)
{
if (n <=1)
/*terminating condition*/
61
return (1);
else
return (n * factorial (n-1));
}
The functional factorial calls itself recursively with an actual argument that decreases in
magnitude for each successive call. The recursive call terminates when the value of the
actual argument becomes equal to 1.
Consider the following two program examples:
#include<stdio.h>
/* define the prototype for the function used in this program. */
void count_dn(int count);
void main()
{
int index;
index = 8;
count_dn(index);
}
/* -- Function definition -------------------- */
void count_dn(int count)
{
count--;
printf("The value of the count is %d\n",count);
if(count > 0)
count_dn(count);
printf("Now the count is %d\n",count);
}
62
In this program the variable index is set to 8, and is used as the argument to the
function count_dn. The function simply decrements the variable, prints it out in a
message, and if the variable is not zero, it calls itself, where it decrements it again,
prints it, etc. etc. etc. Finally, the variable will reach zero, and the function will not call
itself again. Instead, it will return to the prior time it called itself, and return again, until
finally it will return to the main program.
For purposes of understanding you can think of it as having 8 copies of the function
count_dn available and it simply called all of them one at a time, keeping track of
which copy it was in at any given time.
#include <stdio.h>
void recurse(int i);
void main()
{
recurse(0);
}
/* -- Function definition -------------------- */
void recurse(int i)
{
if(i<10)
{
printf(%d , i);
recurse(i+1);
}
}
The recurse( ) function is first called with 0. This is recurses( ) first activation.
Since 0 is less than 10, it is printed. After printing it, the function is called again with
(i+1), i.e. 1. The process repeats until recurse( ) is called with the value 10.
Therefore the numbers are printed in ascending order from 0 to 9.
Revision Exercise
1. Explain the meaning of each of the following function prototypes
(i)
int f(int a);
(ii)
void f(long a, short b, unsigned c);
63
(iii)
2.
char f(void);
Each of the following is the first line of a function definition. Explain the meaning of
each.
(i)
float f(float a, float b)
(ii)
long f(long a)
3. Write appropriate function prototypes for each of the following skeletal outlines
shown below.
(a)
main()
{
int a, b, c;
c =function1(a,b);
}
int fucntion1(int x, int y)
{
int z;
z =
return(z);
}
(b)
main()
{
int a;
float b;
long int c;
c = funct1(a,b);
}
int func1(int x, float y)
{
long int z;
z =
return (z);
}
64
4.
1
(1 + r)n
Required:
65
Write a program that reads the investment amount, rate and number of years,
calculates and prints the future value.
Hint: Implement this program using a function getfv( ), which accepts three parameters
(p, r and n), calculates the fv and returns the value of fv.
66
6
ARRAYS
Objectives
At the end of this chapter, the reader should be able to:
Define an array
Declare one-dimensional arrays
Initialise arrays
Write one-dimensional array programs
Write two-dimensional arrays
Use basic string functions.
Introduction
It is often necessary to store data items that have common characteristics in a form that
supports convenient access and processing e.g. a set of marks for a known number of
students, a list of prices, stock quantities, etc. Arrays provide this facility.
Array defined
An array is a homogeneous ordered set of elements or a series of data objects of the
same type stored sequentially. That is to say that an array has the following
characteristics;
22
19
19
20
67
Because the array is declared as type float, each element can be assigned a float value
such as debts[5] = 32.54;
Other examples;
int emp_no[15]; /*An array to hold 15 integer employee numbers*/
char alpha [26];
/*an array to hold 26 characters */
Declaring arrays
An array definition comprises;
(i)
Data type
(ii)
Array name
(iii)
Arraysize expression (usually a positive integer or symbolic constant). This is
enclosed in square brackets.
For a one-dimensional array (discussed shortly), we use the general form:
type array_name[size];
Where type is a valid C data type, array_name is the name of the array, and size
specifies the number of elements in the array. For example, to declare an integer array
with 10 elements called mylist, use the statement:
int mylist[10];
An array element is accessed by indexing the array using the number of the element. In
C all arrays begin with 0. This means that if you want to access the first element in the
array, use 0 for the index. To index an array, specify the index of the element you want
inside square brackets. For example, the following accesses the third element in the
array mylist.
mylist[2]
The last is element in array is array[size-1].The last element in mylist array is
therefore mylist[9].
To assign an array element a value, put the array reference on the left side of an
assignment statement. For example, this gives the first element in mylist the value
200.
mylist[0] = 200;
Array dimensions
An arrays dimension is the number of indices required to manipulate the elements of
the array.
68
One-dimensional arrays
This is a single list row (or column) of values. For this reason, only a single index is
required to access its values.
C stores one-dimensional arrays on one contiguous memory location with the first
element at the lowest address. For example, after this fragment executes,
int i[5];
int j;
for(j=0;j<5;j++)
i[j]= j+1;
array i will look like this:
i
i
0
1
1
2
2
3
3
4
4
5
You may use the value of an array element anywhere you would use a simple variable
or constant. For example, the following program loads the sqrs array with the squares
of the numbers 1 through 10 and then displays them.
#include<stdio.h>
void main()
{
int sqrs[10];
int i;
for(i=1;i<11;i++)
sqrs[i-1]= i*i;
for(i=0;i<10;i++)
printf(%d , sqrs[i]);
}
69
When you want to use scanf( ) to input a numeric value into an array element, simply
put the & in front of the array name. For example, this scanf( ) reads an integer into
mylist[9].
scanf(%d, &mylist[9]);
When using arrays, you should note the following:
1. Single operations which involve entire arrays are not permitted in C. Operations
such as assignment, comparison operators, sorting etc must be carried out on an
element-by-element basis. This is usually accomplished within a loop where each
pass through the loop is used to process one array element. The number of passes
through the loops will therefore be equal to the number of array elements to be
processed. The above program example demonstrates this.
2. C does not perform any bounds checking on array indexes. This means that it is
possible to overrun the end of an array. For example, if an array called a is declared
as having 5 elements, the compiler will still let you access the (nonexistent) tenth
element with a statement like a[9]. Of course attempting to access nonexistent
elements will generally have disastrous results, often causing the program-even the
computer to crash. It is up to you, the programmer to ensure that that ends of arrays
are never overrun.
3. In C, you must not assign one entire array to another. For example, this fragment is
incorrect.
char a1[10],a2[10];
.
.
.
a2 = a1; /* this is wrong */
If you wish to copy the values of all the elements of one array to another,
you must do so by copying each element separately. That is:
a2[0]=a1[0];
a2[0]=a1[0];
a2[0]=a1[0];
.
.
.
a2[9]=a1[9];
Arrays are very useful in programming when lists of information need to be managed.
For example, the following program reads the noon day temperature for each day of a
month and then reports the months average temperature, as well as its hottest and
coolest days.
70
#include<stdio.h>
void main()
{
int temp[31],i,min,max,avg;
int days;
printf(How many days in the month
? );
scanf(%d, &days);
for(i=0;i<days;i++)
{
printf(Enter noonday temperature for day %d: ; i +1);
scanf(%d, &temp[i]);
}
/* find average */
avg=0;
for(i=0;i<days;i++) avg=avg+temp[i];
printf(Average temperature: %d, avg/days);
/* find min and max */
min = 200; /* Initialise min and max */
max = 0;
for(i=0;i<days;i++)
{
if (min>temp[i])
min = temp[i];
if (max<temp[i])
max= temp[i];
}
printf(Minimimum temperature: %d\n, min);
printf(Maximum temperature: %d\n, max);
}
71
0
1
2
3
Examples of two-dimensional array declarations
float table[50][50];
char page[24][80];
Static double records[100][60][255];
Two-dimensional arrays are used like one-dimensional ones. For example, the following
program loads a 4 x 5 array with the products of the indices, then displays the array in
row, column format.
#include<stdio.h>
void main()
{
int twod[4][5];
int i,j;
for(i=0;i<4;i++)
for(j=0;j<5;j++)
twod[i][j]=i*j;
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
printf(%d ,twod[i][j]);
printf(\n);
}
}
72
A good use of a two-dimensional array is to manage lists of numbers. For example, you
could use this array to hold the noontime temperature for each day of the year, grouped
by month.
float yeartemp[12][31];
Similarly, the following program can be used to keep track of the number of points
scored per season by each member of a basketball team. There are four seasons and 5
members.
#include<stdio.h>
void main()
{
int bball[4][5];
int i,j;
for(i=0;i<4;i++)
for(j=0;j<5;j++)
{
printf(Season %d, Player %d, ,i+1,j+1);
printf(Enter number of points: );
scanf(%d,&bball[i][j]);
}
/* display results */
for(i=0;i<4;i++)
for(j=0;j<5;j++)
{
printf(Season %d, Player %d, ,i+1,j+1);
printf(%d\n,bball[i][j]);
}
}
Initialising arrays
Like other types of variables, you can give the elements of arrays initial values. This is
accomplished by specifying a list of values the array elements will have. The general
form of array initialisation for a one-dimensional array is shown below.
type array_name[size] = { value list };
73
The value list is a comma separated list of constants that are type compatible with the
base type of the array. The first constant will be placed in the first position of the array,
the second constant in the second position and so on. Note that a semi colon follows
the }.
In the following example, a five element integer array is initialised with the squares of
the number 1 though 5.
int i[5] = {1, 4, 9, 16, 25};
This means that i[0] will have the value 1 and i[4] will have the value 25.
You can initialise character arrays in two ways. First, if the array is not holding a
null -terminated string, you simply specify each character using a comma separated
list. For example, this initialises a with the letters A, B, and C.
char a[3] = { A,
B, C};
If the character array is going to hold a string, you can initialise the array using a
quoted string, as shown here.
char name[6] =
Peter;
Notice that no curly braces surround the string. They are not used in this form of
initialisation. Because strings in C must end with a null, you must make sure that the
array you declare is long enough to include the null. This is why name is 6 characters
long, even though Peter is only 5 characters. When a string constant is used, the
compiler automatically supplies the null terminator.
Multidimensional arrays are initialised the same way as one-dimensional ones.
For example, here the array sqr is initialised with the values 1 though 9, using row
order.
int sqr[3][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
This initialisation causes sqr[0][0] to have the value 1, sqr[0][1] to contain 2,
sqr[0][2] to contain 3, and so forth.
If you are initialising a one-dimensional array, you need not specify the size of the array,
simply put nothing inside the square brackets. If you dont specify the size, the compiler
simply counts the number of initialisation constants and uses that that value as the size
of the array.
74
75
Explanation
By defining days [ ] outside the function, we make it external. We initialise it with
a list enclosed in braces, commas are used to separate the members of the list.
Inside the function the optional declaration extern int days [ ]; uses the keyword
extern to remind us that days array is defined elsewhere in the program as an external
array. Because it is defined elsewhere we need not give its size here. (ommitting it has
no effect on how the program works)
Example: Calculating the average of n numbers
#include<stdio.h>
main()
{
int n, count;
float avg, d, sum =0.0;
float list[100];
/* Read in a value of n */
printf( \n How many numbers will be averaged ? );
scanf(%d, &n);
printf(\n);
/* Read in the numbers */
for (count = 0; count < n; count++)
{
printf( i = %d x = , count+1);
scanf(%f, &list[count]);
sum+=list[count];
}
/* Calculate the average */
avg = sum/n;
76
printf(\n The
average is %5.2f
\n\n, avg);
77
int item[100];
int a,b,t;
int count;
/* Read in the numbers */
printf("How many numbers? ");
scanf("%d",&count);
printf("\n");
for(a=0;a<count;a++)
scanf(" %d", &item[a]);
/* Now sort them using a bubble sort */
for(a=1;a<count;++a)
for(b=count-1; b>=a;--b)
{
/* Compare adjacent items */
if (item[b -1] > item[b])
/* exchange the elements */
{
t = item[b - 1];
item[b -1] = item[b];
item[b] = t;
}
}
/* Display sorted list */
printf("\n\n");
for(t=0;t<count;t++)
printf("%d", item[t]);
return 0;
/* End of program */
Sample output:
Exercise
Run the program with a different set of random integers to sort.
78
79
Sample output
Exercise
Change the number of students i.e rows to 5 and and Cats i.e columns to 3 then run
program with cat scores of your choice. Confirm that the output is correct.
Strings
80
In C, one or more characters enclosed between double quotes is called a string. C has
no built-in string data type. Instead, C supports strings using one dimensional character
arrays.
String defined
A string is defined as a null terminated character array. In C, a null is 0. This fact means
that you must define the array is going to hold a string to be one byte larger then the
largest string it is going to hold, to make room for the null.
Reading a string
To read a string from the keyboard you must use another of Cs standard library
functions, gets( ) which requires the STDIO.H header file. To use gets( ), call it using
the name of a character array without any index. The gets( ) function reads characters
until you press <ENTER>. The carriage return is not stored, but it is replaced by a null,
which terminates the string. For example, this program reads and writes a string
entered at the keyboard.
#include<stdio.h>
main()
{
char str[80];
int i;
printf(Enter a string (less than 80 characters): \n);
gets(str); /* Read the string */
printf(\n\n); /* print 2 blank lines /
/* print string */
printf("Here is the string entered\n");
for(i = 0;str[i]; i++)
printf(%c, str[i]);
return 0;
}
81
The gets( ) function performs no bounds checking, so it is possible for the user to
enter more characters that gets( ) is called with can hold. Therefore be sure to call it
with an array large enough to hold the expected input.
Outputting a string
In the previous program, the string that was entered by the user was output to the
screen a character at a time. There is however a much easier way to display a string,
using printf( ). Here is the previous program rewritten..
#include<stdio.h>
main()
{
char str[80];
printf( Enter a string (less than 80 characters): \n);
gets(str);
printf(str);
return 0;
}
If you wanted to output a new line, you could output str like this:
printf(%s \n, str);
This method uses the %s format specifier followed by the new line character and uses
the array as a second argument to be matched by the %s specifier.
String related functions
The C standard library supplies many string-related functions. The four most important
are strcpy( ), strcat( ), strcmp( ) and strlen( ). These functions
require the header file STRING.H.
strcpy function
The strcpy( ) function has this general form.
strcpy(to,from);
It copies the contents of from to to. The contents of from are unchanged. For
example, this fragment copies the string hello into str and displays it on the screen.
char str[80];
strcpy(str, hello);
printf(%s, str);
82
The strcpy( ) function performs no bounds checking, so you just make sure that the
array on the receiving end is large enough to hold what is being copied, including the
null terminator.
strcat() function
The strcat ( ) function adds the contents of one string to another. This is called
concatenation. Its general form is
strcat (to, from);
It adds the contents of from to to. It performs no bounds checking, so you must make
sure to is large enough to hold its current contents plus what it will be receiving. This
fragment displays hello there.
char str[80];
strcpy (str, hello);
strcat (str, there);
printf(str);
strcmp() function
The strcmp ( ) function compares two strings. It takes this general form.
strcmp (s1,s2);
It returns 0 if the strings are the same. It returns less than 0 if s1 is less than s2 and
greater then 0 if s1 is greater than s2. The strings are compared lexicographically; that
is in dictionary order. Therefore, a string is less than another when it would appear
before the other in a dictionary. A string is greater than another when it would appear
after the other. The comparison is not based upon the length of the string. Also, the
comparison is case-sensitive, lowercase characters being greater than uppercase. This
fragment prints 0, because the strings are the same.
printf( %d , strcmp ( one, one));
strlen() function
The strlen() function returns the length , in characters, of a string. Its general form is
strlen(str);
The strlen() function does not count the null terminator.
83
See below how this program would use the string functions as it runs.
84
Note
You can use scanf( ) to read a string using the %s specifier, but you probably wont
need to. Why? This is because when scanf( ) inputs a string, it stops reading that
string when the first white space character is encountered. A white space character is a
space, a tab, or a new line. This means that you cannot use scanf() to read input like
the following string.
This is one string
Because there is a space after the word This, scanf() will stop inputting the string at
that point. That is why gets()is generally used to input strings.
Revision Exercise
1.
2.
3.
What is an array index and how are indexes assigned to array elements?
4.
5.
6.
7.
CAT 2
CAT 3
CAT 4
NANCY
90
34
76
45
JAMES
55
56
70
67
MARY
45
78
70
89
ALEX
89
65
56
90
MOSES
67
56
72
76
CAROL
70
90
68
56
85
(ii) Write a statement that would create the above table and initialize it with
the given scores.
(b) Suppose the name of the above table was SCORES.
(i) What is the value of SCORES[2][3]?
(ii) What is the result of: (SCORES[3][3] % 11) *3?
(c)
Write a complete program that initializes the above values in the table,
computes and displays the total mark and average scored by each
student.
8. Show how to initialise an integer array called items with the values 1 through 10.
9. (i) Write a program that defines a 3 by 3 by 3 three dimensional array, and load it
with the numbers 1 to 27.
(ii) Have the program in (i) display the sum of the elements.
10. Consider the following algorithm:
(i) Declare an array mark for storing the marks for three students in two subjects.
(ii) Repeat the following for all students:
Input the 6 marks into the array.
Compute the total and average mark for each student .
If average is at least 50 and not more than 100, then output Student passed;
otherwise output Wrong mark.
Write a program to implement the above.
11. Write a program that uses a one dimensional array to:
(i)
Enter scores of aptitude tests administered to 10 job applicants.
(ii)
Calculate the average of the ten scores
(iii)
Output the average and the ten scores on different lines.
12. Using an array, write a program that stores a 10 by 10 multiplication table displaying
them on the screen. No input from the keyboard is required.
86
7
POINTERS
Objectives
At the end of this chapter, the reader should be able to:
Define a pointer
Declare pointer variables
Use basic pointer operators * and &
Understand operations permitted with pointers
Use pointers with arrays
Use pointers with string variables
Introduction
This chapter covers one of Cs most important and sometimes most troublesome
features: the pointer. A pointer is basically an address of an object. One reason that
pointers are so important is that much of the power of the C language is derived from
the unique way in which they are implemented. You will learn abut special pointer
operators, pointer arithmetic and how arrays and pointers are related.
Pointer defined
A pointer is a variable that holds the memory address of another variable. For example,
if a variable called p contains the address of another variable called q, then p is said to
point to q.
Therefore if q were at location 100 in memory, then p would have the value 100.
Pointer declaration
To declare a pointer variable, use this general form:
type *var_name;
Here, type is the base type of the pointer. The base type specifies the type of the object
that the pointer can point to. Notice that an asterisk precedes the variable name. This
tells the computer that a pointer variable is being created. For example, the following
statement creates a pointer to an integer.
int *p;
87
Pointer Operators
C contains two special pointer operators: * and &. The & operator returns the address of
the variable it precedes. The * operator returns the value stored at the address that it
precedes. The * pointer operator has no relationship to the multiplication operator,
which uses the same symbol). For example, examine this short program.
#include<stdio.h>
main()
{
int *p, q;
q = 100;
/* assign q 100 */
p = &q;
/* assign p the address of q*/
printf(%d, p);/* display qs value using pointer*/
return 0;
}
Note: The type of the variable and type of pointer must match.
Take a look at two other examples below.
This program uses pointers to display the values of a loop counter.
88
#include<stdio.h>
main()
{
int i,*p;
p = &i;
for (i =0; i <10; i++)
printf (%d\n, *p);
return 0;
}
u2
= %d ,
u1, u2);
Explain why.
Pointers operations
It is possible to perform several operations with pointer variables as explained below.
Assignment
89
Pointer variables have an address and a value. The & operator tells us where the
pointer itself is stored.
From the previous example, p1 is stored in address 3606 whose value is 234.
Pointer arithmetic
In general, pointers may be used like other variables. However, you need to understand
a few rules and restrictions.
In addition to the & and * operators, there are only four other operators that may be
applied to pointer variables: the arithmetic operators +, ++, - and --. Further, you may
add or subtract only integer quantities. You cannot, for example, add a floating point
number to a pointer.
Pointer incrementation arthmetic differs from normal because it is performed relative to
the base type of the pointer. Each time a pointer is incremented, it will point to the next
item, as defined by the base type, beyond the one being currently pointed to
For example, assume an integer pointer called p contains the address 200. After the
statement p++ exeutes, p will have the value 202, assuming integers are 2 bytes long. If
p had been a floating point value (4 bytes long), then the resultant value contained in p
would be 204.
Pointer arithmetic with character appears normal when character pointers are used.
Because characters are 1 byte long, an increment increases the pointer value by one,
decrement decreases it by one.
Addition and subtraction of pointers
You may add or subtract any integer quantity you want, to or from a pointer. For
example, the following is a valid fragment.
int *p;
.
.
p = p + 200;
90
causes p to point to the 200th integer past the one to which p was currently pointing to.
Note
You may not perform any other type of arithmetic operations. You may not divide,
multiply or take modulus of a pointer. However, you may subtract a pointer from another
to find the number of elements separating them.
Incrementation and decrementation of pointers
You can apply the increment and decrement operations to either the pointer itself or the
object to which it points. However you must be careful when attempting to increment
the object pointed to by a pointer.
For example, assume p points to an integer that contains the value 1.Now consider the
statements:
*p++; and
*(p)++;
*p++ first increments p and then obtains the value at the new location. To increment
what is pointed to by a pointer, you must use the second statement.
Pointer precautions
*fp;
fp = &q;
fp = 100.23;
Do not use a pointer before it has been assigned the address of a variable. May
cause program to crash.
For example:
main()
{
int
*p;
91
}
The above is meaningless and dangerous.
Pointers and arrays
Pointers can be extended for use in arrays. Array elements can be accesses using
pointers. It is possible to create an array of pointers.
Accessing arrays using pointers
We can declare an array of characters and a pointer to a character
For example
char line[100],
*p;
We may refer to the first two elements of the array line using
line[0] = a;
line[1] = b; and for each assignment, the compliler calculates the
address.
Another way to perform the assignments is to use a pointer. First, we must initialize the
pointer p to point to the beginning of the array.
i.e. p = &line [0];
Since an arrays name is a synonynm to the arrays starting addres, we can use:
p = line;
We can now perform the assignments:
*p = a; and *(p+1) = b;
92
&p1);
How close is your output to the one below which was produced when the program was
run? Can you explain any differences.
93
You will notice that first we assign a string constant to the string variable named strg
so we will have some data to work with. Next, we assign the value of the first element to
the variable one, a simple char variable.
Next, since the string name is a pointer by definition, we can assign the same value to
two by using the asterisk and the string name. The result of the two assignments are
such that one now has the same value as two, and both contain the character T, the
first character in the string. Note that it would be incorrect to write the ninth line as two
= *strg[0]; because the asterisk takes the place of the square brackets.
For all practical purposes, strg is a pointer. It does, however, have one restriction that a
true pointer does not have. It cannot be changed like a variable, but must always
contain the initial value and therefore always points to its string. It could be thought of
as a pointer constant, and in some applications you may desire a pointer that cannot be
corrupted in any way. Even though it cannot be changed, it can be used to refer to other
values than the one it is defined to point to, as we see in the next section of the
program.
Moving ahead to line 9, the variable one is assigned the value of the ninth variable
(since the indexing starts at zero) and two is assigned the same value because we are
allowed to index a pointer to get to values farther ahead in the string. Both variables
now contain the character a.
Hence the following output.
Revision Exercise
1. What is a pointer?
2. How do arrays and pointers relate to each other?
3. How does pointer arithmetic differ from ordinary arithmetic?
4. State two rules one should observe when using pointers.
5. Using examples, give the meaning of the pointer operators & and *.
6. Write a program that uses a for loop to print digits from 1 to 5 using pointers
94
9. Write a program that assigns a value to a variable indirectly by using a pointer to that
variable.
10. Assume p is declared as a pointer to a double and contains the address 100.
Further, assume that doubles are 8 bytes long. After p is incremented, what will its
value be?
95
8
STRUCTURES
Objectives
At the end of this chapter, the reader should be able to:
Introduction
Suppose you want to write a program that keeps tracks of students [Name, Marks] i.e. a
variety of information to be stored about each student. This requires;
An array of strings (for the Names).
Marks in an array of integers.
Keeping track of many related arrays can be problematic, for example in operations
such as sorting all of the arrays using a certain format.
A data form or type containing both the strings and integers and somehow keep the
information separate is required. Such a data type is called a structure.
Structure defined
A structure is an aggregate data type that is composed of two or more related elements.
Unlike arrays, each element of a structure can have its own type, which may differ from
the types of any other elements.
Declaring a structure
Declaring a structure is a two-stage process. The first stage defines a new data type
that has the required structure which can then be used to declare as many variables
with the same structure as required.
96
97
and you can have as long a list of component variables as you need. Once defined you
can declare as many examples of the new type as you like using:
struct name list of variables;
For example:
struct emprec employee, oldemploy, newemploy;
If you want to you can also declare a structure variable within the type definition by
writing its name before the final semi-colon. For example:
struct emprec
{
char name[25];
int age;
int pay;
} employee;
98
Initializing a structure
A structure can be initialized like any other variable - external, static or automatic. This
will depend on where the structure is defined. For example mystudent variable created
above can be initialized as follows:
struct student mystudent =
{
Fred Otieno,25;
};
Each member is given its own line of initialization and a comma separator, one member
initialization from the next.
Accessing structure members
When you first start working with arrays it seems obvious that you access the individual
elements of the array using an index as in a[i] for the ith element of the array, but how
to get at the individual components of a structure?
The answer is that you have to use qualified names. You first give the name of the
structure variable and then the name of the component separated by a dot. For
example, given:
struct emprec employee;
then:
employee.age
is an int and:
employee.name
is a char array. Once you have used a qualified name to get down to the level of a
component then it behaves like a normal variable of the type. For example:
employee.age=32;
is a valid assignment to an int and:
99
employee.name[2] = 'X';
is a valid assignment to an element of the char array.
Notice that the qualified name uses the structure variable name and not the structure
type name.
You can use employee.age or employee.name exactly the way you use other
variables.
For example, you can use scanf(%d,&employee.age);
Or
gets(employee.name);
Note
Although employee is a structure, employee.age is an int type and is like any other
integer variable. Therefore;
The use of scanf(%d.........) requires the address of an integer and that
is what &employee.age does.
You can use this method even to read values into another variable.
If employee1 is another amployee structure declared as follows;
struct employee employee1;, then it is possible to read the age and name
into the variable using the statements;
gets(employee1.name);
scanf(%d,&employee1.age);
You can also define a structure that includes another structure as a component and of
course that structure can contain another structure and so on. In this case you simply
use the name of each structure in turn, separated by dots, until you reach a final
component that isn't a structure. For example, if you declare a struct firm which
includes a component employee which is an emprec then:
firm.employee.age
is an int.
100
Run the program and test it with your choice studentr name and marks as has been
done below.
Arrays of structures
Let us extend our student program to handle a greater number of students.
One student can be described by one structure variable of type student, 2 students by
two variables, 3 students by three variables, n students by n such structure variables,
etc.
To have n students (n being any number), we use an array structure of n elements.
Example: Entering a students details using an array structure
#include<stdio.h>
#define SIZE 40
#define MAXSTU 4
struct student{
char name[SIZE];
int marks;
};
101
main()
{
struct student mystudent[MAXSTU];
int count;
for(count=0; count<MAXSTU; count++){
printf(Enter the name and marks of student %d\n, count+1);
scanf(%s, &mystudent[count].name);
scanf(%d, &mystudent[count].marks);
}
printf(\n\n ===Student details as entered===\n\n;
for(count=0;count<MAXSTU; count++){
printf(%s got %d\n,mystudent[count].name,mystudent[count].marks);
}
return 0;
}
Run the program with a different array size and sets of your choice names and marks
(watch out that you do not use a very large size unless you have a lot of patience to run
the program for a long time!). Compare your results with the output of the above
program shown below.
Uses of structures
Where are structures useful?
The immediate application that comes to mind is database management. For example,
to maintain data about employees in an organization, books in a library, items in a store,
financial transactions in a company, etc.
102
Their use however, stretches beyond database management. They can be used for a
variety of applications like:
To program the above applications, you need thorough knowledge of internal details of
the operating system.
User defined types (typedef)
The typedef feature allows users to define new data types that are equivalent to existing
data types. Once a user defined type has been established, then new variables, arrays,
structures, etc. can be declared in terms of this new data type.
For example,
typedef int age;
In this declaration, age is a user defined data type which is equivalent to type int.
Hence the declaration:
age male, female; - is equivalent to writing int male,male;
In other words, male and female are regarded as variables of type age, though they are
actualy integer type variables.
Similarly, the declarations;
typedef float height[100];
height men , women;
define height as a 100 - element floating type array, hence men and women are 100
element floating point arrays.
typedef is particularly convenient when defining structures, since it eliminates the need
to write the struct tag whenever a structure is referenced.
Example: Demonstrating typedef
103
typedef struct
{
char name[SIZE];
int marks;
}student;
student student1,student2;
Example: typedef further demonstrated
typedef struct
{
int month;
int day;
int year;
}date;
typedef struct
{
int acc_number;
char acc_type;
char name[30];
float balance;
date lastpayment;
}BankAcct; /*BankAcct is a user defined type */
BankAcct customer[100];
Unions
A union is a single memory location that stores two or more variables. Members within a
union all share the same storage area, whereas each member within a structure is
assigned its own unique storage area.
104
Thus, unions are used to conserve memory. They are useful for applications involving
multiple members, where values need not be assigned to all members at a time.
The similarity between structures and unions is that both contain members whose
individual data types may differ from one another.
A union takes the following form
union tag
{
member 1;
member 2;
..
..
member n;
};
Or
union tag
{
member 1;
member 2;
..
..
member n;
}variable list;
Consider that a C program contains the following union declaration:
union id{
char color[12];
int size;
}shirt, blouse;
Explanation
(i) There are two union variables shirt and blouse. Each variable can represent
either a 2 character string (colour) or a integer quantity (size) at any one time. The
12-character string will require more storage area within the computers memory
than the integer quantity.
Therefore a block of memory large enough for the 12-character string will be
allocated to each union variable.
105
Explanation
shirt and blouse are structure variables of type clothes.
Each variable will contain the following members;
A string manufact
A floating point quantity cost
A union descr. The union may represent either a string (color) or an integer quantity
(size).
An alternative declaration of the variables shirt and blouse is:
struct clothes
{
char manufact[20];
float cost;
union
{
char color[12];
int size;
}descr;
106
};shirt, blouse;
Revision Exercise
1. How is a structure different from:
(a) a union?
(b) an array?
2. Show how to create a structure called stype that contains these five elements:
ch - character
d
- floating point;
- integer;
s_type
{
int a;
char b;
float bal;
}myvar,*p;
p = &myvar;
p.a =
10;
4. Set up a suitable structure for an invoice that should hold the following details:
Element
Type
Invoice number
integer
107
Customer number
Invoice date
Customer address
Item
Invoice Total
integer
structure
(with three integer elements; day, month, year)
string (20 characters)
structure
[with product code (integer), unit price (float)
quantity (float) , amount (double)]
double
5. (a) Write a program that sets up a structure of a student record comprising the
students name, age and fee balance, then reads in the three items into a structure
variable and outputs to the screen.
(b) Rewrite the program in part (a) above such that you enter the student data into an
array of 5 structure variables and then print it.
6 (a) What is a user defined data type?
(b) Using a user defined type named invoices set up the structure template in part (a)
above, show how you can set two simple structure variables invoice1, invoice2 using
the new type.
108
9
FILE HANDLING
Objectives
At the end of this chapter, the reader should be able to:
Understand streams
Master file system basics
Understand random access
Apply various file-system functions
Develop a simple file-based system
Introduction
Disk input/output (I/O) operations are performed on entities called files.
Although C does not have any built in method of performing file I/O, the standard C
library contains a very rich set of I/O functions.
High-level Disk I/O functions are more commonly used in C programs, since they are
easier to use than low-level disk I/O functions.
The low-level disk I/O functions are more closely related to the computers operating
system than the high-level disk I/O functions.
Low-level disk I/O is harder to program than high-level disk I/O. However, low-level disk
I/O is more efficient both in terms of operation and the amount of memory used by the
program.
This chapter is concerned with the high level functions.
109
For example, when the new line character is output, it is converted into a carriage
return/line feed sequence. For this reason, there may not be a one-to-one
correspondence between what is sent to the stream and what is written to the file.
A binary stream is used with any type of data. No character translations will occur, and
there is a one-to-one correspondence between what is sent to the stream and what is
actually contained in the file.
One final concept you need to understand is that of the current location. The current
location also referred to as the current position is the location is a file where the next file
access will begin. For example, if a file is 100 bytes long and half the file has been read,
the next read operation will occur at location 50, which is the current location.
Opening a file
Before we can write information to a file or disk or read it we must open the file.
Opening a file establishes a link between the program and the operating system. We
provide the operating system with the name of the file and whether we plan to read or
write to it.
To open a file and associate it with a stream, use fopen() function.
The link between our program and the operating system is a structure called FILE
(which has been defined in the header file stdio.h.
If the open operation is successful, what we get back is a pointer to the structure FILE.
That is why we make the following declaration before opening the file,
FILE *fp;
Each file we open will have its own file structure. The file structure contains information
about the file being used, such as its current size, memory location, access modes, etc.
Consider the following statements,
FILE *fp;
Fp = fopen (myfile.txt, r);
Meaning? fp is a pointer variable, which contains address of the structure FILE which
has been defined in the stdio.h header file.
fopen ( ) will open a file myfile.txt in read mode, which tells the C compiler that we
would be reading the contents of the file.
Legal values for modes that can be used with fopen () are summarised below.
110
Searches file. If the file exists, loads it into memory and sets up a pointer which
points to the first character in it. If the file doesnt exist it returns NULL.
Operation possible - reading from the file.
w
Searches file. If the file exists, its contents are overwritten. If the file doesnt exist, a new file is
created; Returns NULL if unable to open file.
Searches file. If the file exists, loads it into memory and sets up a pointer which
points to the first character in it. If the file doesnt exist, a new file is created.
Returns NULL if unable to open file.
Operations possible Appending new contents at end of file.
r+
Searches file. If the file exists, loads it into memory and sets up a pointer which
points to the first character in it. Returns NULL if unable to open file.
Operations possible Reading existing contents, writing new contents, modifying
contents of the file.
w+
Searches file. If the file exists, its contents are destroyed. If the file doesnt exist,
a new file is created. Returns NULL if unable to open file.
Operations possible Writing new contents, reading them back and modifying
existing contents of the file.
a+
Searches a file. If the file exists, loads it into memory and sets up a pointer which
points to the first character in it. Returns NULL if unable to open file.
Operations possible: Reading existing contents, appending new contents,
appending new contents to end of file. Cannot modify existing contents.
Note
The header file stdio.h defines the macro NULL which is defined to be a null pointer.
It is very important to ensure that a valid file pointer has been returned You must check
the value returned by fopen ( ) to be sure that it is not NULL.
For example, the proper way to open a file called myfile.txt for text input is shown
below.
FILE *fp;
111
112
another=getche ();
}
fclose (fp);
return 0;
}
113
int age;
float bs;
fp=fopen (EMPLOYEE.DAT,r);
if (fp==NULL)
{
printf(cannot open file);
exit(1);
}
while(fscanf (fp,%s%d%f, name,&age,&bs)!=EOF)
printf(%s %d %f\n, name, &age, &bs);
fclose (fp);
return 0;
}
114
if(fp==NULL)
{
printf(cannot open file);
exit(1);
}
while(another==Y)
{
printf(\n Enter name, age and basic salary: );
scanf(%s%d%f, e.name,&e.age,&e.bs);
fprintf (fp, %s %d %f\n,e.name,e.age,e.bs);
printf(\n Add another record (Y/N)? );
fflush(stdin),
another=getche ();
}
fclose (fp);
return 0;
}
In the above program, we are just reading data into a structure using scanf( ) and
dumping it into disk file using fprintf ( ). The user can input as many records as he
desires. The procedure ends when the user supplies N for the question Add another
record (Y/N)?.
The above program has two disadvantages:
(a) The numbers (basic salary) would occupy more number of bytes, since the file
has been occupied in text mode. This is because when the file is opened in text
mode, each number is stored as a character string.
(b) If the number of fields in the structure increase (say, by adding address, house
rent allowance etc), in that case writing structures using fprintf ( ), or reading
them using fscanf ( ) becomes quite clumsy.
Before we can eliminate these disadvantages, let us first complete the program that
reads employee records created by the above program.
/*Read records from a file using structure*/
115
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fp;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
fp = fopen (EMPLOYEE.DATA,w);
if(fp==NULL)
{
printf(cannot open file);
exit(1);
}
while(fscanf(fp, %s%d%f,e.name,&e.age,&e.bs)!= EOF)
printf(\n %s %d %f, e.name,e.age,e.bs);
fclose(fp);
return 0;
}
Expected output:
Kanja
34 1250.000000
Wanja
21 1300.000000
Mwashe 34 1400.000000
Using fread( ) and fwrite( ) functions
Let us now see a more efficient way of reading/writing records (structures). This makes
use of two functions fread ( ) and fwrite ( ).
Example: Program to write records to a file using fwrite ( )
/*Receiving records from the key board & writing them to a file in a
binary mode*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
main( )
{
116
FILE *fp;
char another = Y;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
fp=fopen (EMP.DAT,wb);
if(fp==NULL)
{
printf(cannot open file);
exit(1);
}
while (another ==Y)
{
printf(\n Enter name, age and basic salary: );
scanf(%s%d%f, e.name,&e.age,&e.bs);
fwrite(&e,sizeof(e), 1, fp);
printf( \n Add another record (Y/N)? );
fflush(stdin);
another=getche ( );
}
fclose (fp);
return 0;
}
117
fwrite (&e,sizeof(e),1,fp);
Here, the first argument is the address of the structure to be written to the disk.
The second argument is the size of the structure in bytes. Instead of counting the bytes
occupied by the structure ourselves, we let the program do it for us by using the
sizeof() operator which gives the size of variable in bytes. This keeps the program
unchanged in event of change in the elements of the structure. The third argument is
the number of such structures that we want to write at one time. In this case, we want to
write only one structure at a time. Had we had an array of structures, for example, we
might want have wanted to write the entire array at once.
The last argument is the pointer to the file we want to write to.
Example: Program to read records from a file using fread ( )
/*Read the records from the binary file and display them on VDU*/
#include <stdio.h>
#include <stdlib.h>
main( )
{
FILE *fp;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
fp=fopen (EMP.DAT, rb);
if(fp==NULL)
{
printf(cannot open file);
exit(1);
}
while (fread (&e, sizeof(e),1,fp)==1)
printf(%s %d % f \n,e.name,e.age,e.bs);
fclose (fp);
return 0;
}
118
The function fread () returns the number of records read. Ordinarily, this should
correspond to the third argument, the number of records we asked for - 1 in this case. If
we have reached the end of file, since fread() cannot read anything, it returns a 0.
Note
You can now appreciate that any database management application in C must make
use of fread () and fwrite () functions, since they store numbers more efficiently, and
make writing/reading of structures quite easy. Note that even if the number belonging to
the structures increases, the format of fread() and fwrite() remains the same.
119
FILE *fp,*ft;
char another, choice;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
char empname[40];
fp=fopen (EMP.DAT, rb+);
if (fp==NULL)
{
fp=fopen (EMP.DAT, wb+);
if(fp==NULL)
{
printf(cannot open file);
exit(1);
}
}
while(1)
{
printf(\n1. Add Records);
printf(\n2. List Records);
printf(\n3. Modify Records);
printf(\n4. Delete Records);
printf(\n0.Exit);
printf(\n\n Your choice );
choice=getche ();
switch(choice)
{
case 1:
fseek(fp,0,SEEK_END);
another = Y;
while(another==Y)
{
printf(\n Enter name,age and basic sal.
scanf(%s %d %f,e.name, &e.age, &e.bs);
fwrite (&e,sizeof(e), 1, fp);
printf(\n Add another Record(Y/N) );
fflush(stdin);
another = getche ( );
}
break;
case 2:
rewind(fp);
120
);
);
}
break;
case 4:
another =Y;
while(another==Y)
{
printf(\n Enter name of employee to delete
scanf(%s, empname);
ft = fopen (TEMP.DAT, wb);
rewind(fp);
while(fread (&e, sizeof(e),1,fp)==1)
{
if (strcmp(e.name,empname)!=0)
fwrite (&e, sizeof(e), 1, ft);
}
fclose (fp);
fclose (ft);
remove(EMP.DAT);
rename(TEMP.DAT,EMP.DAT);
fp = fopen (EMP.DAT, rb+);
printf(Delete another Record (Y/N)
121
);
);
fflush(stdin);
another = getche ();
}
break;
case 0:
fclose (fp);
exit(1);
}
}
}
To understand how this program works, you need to be familiar with the concept of
pointers.
A pointer is initiated whenever we open a file. On opening a file, a pointer is setup which
points to the first record in the file. On using the functions fread () or fwrite (), the
pointer moves to the beginning of next record. On closing a file the pointer is
deactivated.
The rewind () function places the pointer to the beginning of the file, irrespective of
where it is present right now.
Note that pointer movement is of utmost importance since fread always reads that
record where the file pointer is currently placed. Similarly, fwrite () always writes the
record where the file pointer is currently placed.
The fseek() function lets us move the file pointer from one place to another. In the
program above, to move the pointer to the previous record from its current position, we
used the function,
fseek(fp,-sizeof(e), SEEK_CUR);
Here, -sizeof (e) moves the pointer back by sizeof(e) bytes from the current position.
SEEK_CUR is a macro defined in stdio.h
Similarly, the following fseek() would place the pointer beyond the last record in the file.
in fact sizeof (e) or 0 are just the offsets which tell the compiler by how many bytes
should the pointer be moved from a particular position. The third argument could be
either SEEK_END, SEEK_CUR or SEEK_SET all these act as reference from which
the pointer should be offset. SEEK_END means move the pointer from the end of the
file, SEEK_CUR means move the file in reference to its current position and
SEEK_SET means move the pointer with reference to the beginning of the file.
122
if we wish to know where the pointer is positioned right now, we can use the function
ftell(). It returns this position as a long int which is an offset from the beginning of the
file. the value returned by ftell() can be used in subsequent calls to fseek(). A sample
call to ftell( ) is shown below:
position=ftell(fp);
where position is a long int,
Revision Exercise
1. Define an input file handle called input_file, which is a pointer to a type FILE.
2. Using input_file, open the file results.dat for read mode as a text file.
3. Write C statements which tests to see if input_file has opened the data file
successfully. If not, print an error message and exit the program.
4. What will be the output of the following programs?
(a)
#include<stdio.h>
main( )
{
char str[20];
FILE *fp;
fp = fopen (strcpy(str,ENGINE.C), W);
fclose (fp);
return 0;
}
(b)
#include<stdlib.h>
#include<stdio.h>
main( )
{
FILE *fp;
char c;
fp = fopen (strcpy(str,ENGINE.C), W);
fclose (fp);
fp = fopen (TRY.C,r);
if(fp=NULL)
{
printf(Cannot open file);
exit(1);
123
}
while((c = getch(fp)) ! = EOF)
putch(c);
fclose (fp);
return 0;
}
(c)
#include<stdio.h>
main( )
{
FILE fp,*fs,*ft;
char str[80];
/*TRIALContains only one line Its a round, round, round world */
fp = fopen (TRIAL.C, r);
while(fgets (str,80,fp) != EOF)
puts(str);
return 0;
}
6. Create a small database program to keep track of books in a library. Your program
should be menu driven and should allow a user to:
a.
b.
c.
d.
e.
124
Question One
(a)
What do you understand by structured programming?
(3 marks)
(b)
(c)
(d)
(4 marks)
(10 marks)
(2 marks)
(ii) What situation will make a keyword not to be recognised during the
compilation of a C program.
(1 mark)
Question Two
(a)
Distinguish between a simple variable and an array variable.
(b)
(c)
(d)
(2 marks)
Discuss four fundamental data types in C, giving examples and stating their
conventional storage requirements.
(12 marks)
(i)
What is a symbolic constant?
(2 marks)
(ii)
What is the advantage of using symbolic constants over direct constants?
(1 mark)
What is a storage class? Explain how any two of storage classes are used in C.
(3 marks)
125
Question Three
(a)
What is a structure?
(b)
(c)
(d)
(3 marks)
(i) Set up a suitable structure for an invoice that should hold the following details:
Element
Type
Invoice number
integer
Customer number
integer
Invoice date
structure
(with three integer elements; day, month, year)
Customer address
string (20 characters)
Item
structure
[with product code (integer), unit price (float)
quantity (float) , amount (double)]
Invoice Total
double
(7 marks)
(ii)
Write a declaration statement that would create a 5-element array variable
named invoice_file from the structure type in b(i).
(2 marks)
How is a structure different from a union?
(2 marks)
(i)
What is a user defined data type?
(2 marks)
(ii)
Using a user defined type named invoices set up the structure template in
b(i) above. Show how you can set two simple structure variables
invoice1, invoice2 using the new type.
(4 marks)
Question Four
(a)
Outline the stages of developing a working program in C.
(14 marks)
(b)
(c)
(i)
(2 marks)
(ii)
A program may compile successfully but fail to generate desired results.
Why?
(2 marks)
Kelly encountered the following error messages on compiling a program:
(i)
Misplaced else
(ii)
Statement missing
What advice would you offer him to debug the above errors?
(2 marks)
126
Question Five
(a)
(i) C programs are basically made up of functions, one of which is called main.
State three advantages offered by functions in C programs.
(3 marks)
(ii)
What is a function prototype?
(2 marks)
(b)
Write a program that requests two integers values and outputs the larger value
on the screen. Use a function to perform the comparison of the two integers to
determine the larger one. The main function should pass the entered values to
this function.
(10 marks)
(c)
Suggest the output of the following program.
#include <stdio.h>
int mult(int);
main()
{
int a, count;
for(count=1; count <=5; count++)
{
a = mult(count);
printf(\n %d, a);
}
}
int
{
mult(int in_value)
int prod;
prod =
in_value * in_value;
return(prod);
}
(5 marks)
Question Six
(a) Muajiri Company Ltd uses the following PAYE (Pay As You Earn) percentage tax
rates for all its employees salary categories.
Gross Salary (Ksh.)
1.
50,000 and above
2.
40,000 - 50,000
3.
35,000 - 40,000
4.
25,000 - 35,000
5.
16,000 - 25,000
6.
9,500 - 16,000
7.
Below 9500
(The rates are exclusive of the upper boundary salary figures for categories 2,3,4,5,6)
127
128
Question Seven
(a)
Give the meaning of each of the following terms, giving examples.
(i)
Escape sequence
(ii)
Recursion
(iii)
Binary operator
(iv)
Conditional expression
(8 marks)
(b)
(4 marks)
s and t are integers with values 500 and 800 and stored at memory locations
1200 and1205 respectively. ps and pt are integer pointers to s and t respectively.
Give the results of:
(i)
*ps + *pt
(ii)
*ps++
(iii)
(*pt)++
(3 marks)
(d)
Write a program using a for loop that counts down from 10 down to 0, displaying
only the even numbers in this range. The numbers should be displayed using a
pointer.
(5 marks)
Question Eight
(a)
State with examples, four types of operators used in C.
(4 marks)
(b)
List three types of unary operators. Give an expression in each case to show
how they are used.
(6 marks)
(c)
The following table shows Kenyas average exports (in tonnes) of five
commodities over four years.
1974
1975
1976
1977
(c)
TEA
18000
19450
23890
28820
COFFEE
20000
27000
29000
33452
SISAL
3400
4501
3890
3973
SUGAR
6500
7200
8100
8805
FRUITS
12780
13210
14300
15302
129