Module 3 4
Module 3 4
CONTENTS
1.0 Introduction
2.0 Intended Learning Outcomes (ILOs)
3.0 Main Contents
3.1 Structured Programming Concept
3.2 Advantages of Structured Programming
3.3 Disadvantages of Structured Programming
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
23
CIT301 MODULE 3
Each function is design to do a specific task with its own data and logic.
Information can be passed from one function to another function through
parameters. A function can have local data that cannot be accessed outside
the function’s scope. The result of this process is that all the other different
functions are synthesized in another function. This function is known as
main function. Many of the high-level languages support structured
programming.
24
CIT301 MODULE 3
SELF-ASSESSMENT EXERCISE
Solution:
Structured programming is a programming paradigm aimed at improving
the clarity, quality, and development time of a computer program by
making extensive use of the structured control flow constructs of selection
(if/then/else) and repetition (while and for), block structures, and
subroutines. It provides tools for efficient coding, save memory usage and
program performance. It provides pointers which has a lot of advantages
as more efficient code, faster execution and memory saving, higher
productivity can be achieved. It provides recursive function which also
makes for efficiency.
4.0 CONCLUSION
25
CIT301 MODULE 3
5.0 SUMMARY
26
CIT301 MODULE 3
CONTENTS
1.0 Introduction
2.0 Intended Learning Outcomes (ILOs)
3.0 Main Contents
3.1 Imperative Paradigm
3.2 Functional Paradigm
3.3 Logical Paradigm
3.4 Object Oriented Paradigm
3.5 Other Paradigms
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
27
CIT301 MODULE 3
The logic paradigm fits well when applied in problem domains that deal
with the extractionof knowledge from basic facts and relations. Is based
on axioms, inference rules, and queries. Program execution becomes a
systematic search in a set of facts, making use of a set of inference rules.
Examples: Prolog and List.
28
CIT301 MODULE 3
SELF-ASSESSMENT EXERCISE
Solution
1) List the examples of object-oriented programming language
Java
C++
C#
Ruby
Python
TypeScript
PHP
29
CIT301 MODULE 3
4.0 CONCLUSION
5.0 SUMMARY
In this unit you have learnt some programming paradigms. These include
Imperative, Functional, Logical, Object-Oriented Paradigms etc. These
Paradigms are briefly discussed. The programming languages associated
with these Paradigms are also discussed.
30
CIT301 MODULE 4
Unit 1 Overview of C
Unit 2 C Program Design
Unit 3 Executing a C Program
UNIT 1 OVERVIEW OF C
CONTENTS
1.0 Introduction
2.0 Intended Learning Outcomes (ILOs)
3.0 Main Contents
3.1 Brief History of C
3.2 Taxonomy of C Types
3.3 Why Study C?
3.4 Why is C Popular?
3.5 Characteristics of C program
3.6 Uses of C
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
31
CIT301 MODULE 4
Scalar types
Arithmetic types
Integral types: char, short, int, long
Floating-point types: float, double, long double
Pointer types
Aggregate types
Array types
Structure types
Union types
Function types
Void types
32
CIT301 MODULE 4
3.6 Uses of C
33
CIT301 MODULE 4
Spreadsheets
Operating system development
Compilers and Assemblers
Network drivers and Interpreters
SELF-ASSESSMENT EXERCISE
34
CIT301 MODULE 4
4.0 CONCLUSION
5.0 SUMMARY
35
CIT301 MODULE 4
CONTENTS
1.0 Introduction
2.0 Intended Learning Outcomes
3.0 Main Contents
3.1 C Program Structure
3.2 Files Used in A C Program
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
36
CIT301 MODULE 4
Explanation:
#include: The part of the compiler which actually gets your
program from the source file is called the preprocessor.
#include <stdio.h>:#include is a pre-processor directive. It is not
really part of our program, but instead it is an instruction to the
compiler to make it do something. It tells the C compiler to include
the contents of a file (in this case the system file called stdio.h).
The compiler knows it is a system file, and therefore must be
looked for in a special place, by the fact that the filename is
enclosed in <> characters
<stdio.h>: stdio.h is the name of the standard library definition file for all
STanDard Input and Output functions.
The program will almost certainly want to send information to the screen
and read things from the keyboard, and stdio.h is the name of the
file in which the functions that we want to use are defined.
The function we want to use is called printf. The actual code of printf will
be tied in later by the linker.
The ".h" portion of the filename is the language extension, which denotes
an include file.
void:This literally means that this means nothing. In this case, it is
referring to the function whose name follows. Void tells C
compiler that a given entity has no meaning and produces no error.
main:In this example, the only function in the program is called
main. A C program is typically made up of large number of
functions. Each of these is given a name by the programmer and
they refer to each other as the program runs. C regards the name
main as a special case and will run this function first i.e. the
program execution starts from main.
(void): This is a pair of brackets enclosing the keyword void.
It tells the compiler that the function main has no parameters.
A parameter to a function gives the function something to work on.
{ (Brace): This is a brace (or curly bracket). As the name implies,
braces come in packs of two - for every open brace there must be
a matching close one. Braces allow us to group pieces of program
together, often called a block.A block can contain the declaration
of variable used within it, followed by a sequence of program
statements.
In this case the braces enclose the working parts of the function main.
; (semicolon): The semicolon marks the end of the list of variable
names, and also the end of that declaration statement.All
statements in C programs are separated by ";" (semicolon)
characters. The ";" character is actually very important. It tells the
compiler where a given statement ends.
37
CIT301 MODULE 4
Source File- This file contains the source code of the program. The
file extension of any c file is .c. The file contains C source code
that defines the main function & maybe other functions.
Header File- A header file is a file with extension .h which contains
the C function declarations and macro definitions and to be shared
between several source files.
Object File- An object file is a file containing object code, with an
extension .o, meaning relocatable format machine code that is
usually not directly executable. Object files are produced by an
assembler, compiler, or other language translator, and used as
input to the linker, which in turn typically generates an executable
or library by combining parts of object files.
Executable File- The binary executable file is generated by the
linker. The linker links the various object files to produce a binary
file that can be directly executed.
SELF-ASSESSMENT EXERCISE
Solution:
Explain all the reserved words used in the description of a C program
structure.
38
CIT301 MODULE 4
1. volatile
This keyword is needed so as to create volatile objects. These volatile
objects have the ability to get modified in the unknown or unmentioned
method through hardware.
2. auto
This keyword is used to declare the automatic variables.
3. char
char keyword is used to declare the character variable. Variables that are
of type char are of 1-byte length. They can get signed (it is by default
unless we use the compiler option ‘-funsigned-char’ or ‘unsigned’), which
implies they have got a range of -128 to 127 and 0 to 255, respectively.
4. double and float
Both keywords double, as well as float, are needed for declaration of
floating type variables.
5. const
We can declare an identifier to be constant through the usage of the const
keyword.
6. if and else
We use if and else so as to make decisions in C programming.
7. break and continue
The break statement would make the program jump out of the most inner
and enclosing loop in an explicit manner. The continue is used for
statements skipping certain statements that are inside the loop.
8. enum
In C programming enumeration types get declared through keyword
enum.
9. extern
The extern keyword indicates that the identifier has benn defined
somewhere else. It also indicates that in fact storage as well as the initial
value, or function body has been defined somewhere else, mostly in the
10. return
Return is used for exiting the function. It would exit from the current
function that is executing immediately and return to the calling routine. It
can optionally return value too.
11. sizeof
sizeof is used for returning the size of expression or type of it. It is used
for returning the size in bytes.
13. int
int keyword is used for declaration of the integer type variable.
14. register
This keyword is used for the creation of the register variables that are
much faster as compared to the normal variables.
15. static
This keyword is used for the creation of a static variable. The static
variables’ values persist until the end of the program. It tells that the
39
CIT301 MODULE 4
function or the element is only known inside the scope of the current
compilation. Also, if we use the static keyword along with the variable
which is local to the function, it would allow the last value of the variable
to get preserved in successive calls to that function.
16. struct
struct keyword is used for the declaration of the structure. The structure
is used for holding the variables of varied data types under one name. Just
like the union, it groups the variables into a single record. Also, the
struct-type-name is considered to be the optional tag name which points
to structure type. The variables of a structure are data definitions, and they
are optional. Although both are optional, one of the two must appear.
17. union
Union keyword is needed for grouping the varied types of a variable under
one name.
18. void
This keyword denotes that the function won’t be returning any value.
19. typedef
This keyword is required so as to associate a type along with an identifier
in an explicit manner.
20. short, long, signed and unsigned
The short, long, signed as well as unsigned keywords are the type of
modifiers which alters the meaning of the base data type in order to yield
the new type.
21. for
In total, there exist 3 kinds of loops in C. The for loop in C is written using
the keyword for.
22. switch, case and default
We use switch as well as case statements whenever the block of
statements needs to be executed among various blocks.
23. do-while loop
do is used along with a while to make a different form of repetition of the
statement.
24. while
It is used for repeating the execution when the condition is true.
40
CIT301 MODULE 4
4.0 CONCLUSION
5.0 SUMMARY
In this unit you have been exposed to the structure of a C program and
some programming elements such #include, stdio.h, void, main, printf,
scanf, return etc. These elements are adequately explained in the unit.
Some of the files used in C program are also outlined.
41
CIT301 MODULE 4
CONTENTS
1.0 Introduction
2.0 Intended Learning Outcomes
3.0 Main Contents
3.1 Compilation and Execution of a C Program
3.2 Commonly used Programs for execution on Linux System
3.3. Pictorial Diagram of C Compilation and Execution
4.0 Conclusion
5.0 Summary
6.0 Tutor-Marked Assignment
7.0 References/Further Reading
1.0 INTRODUCTION
42
CIT301 MODULE 4
43
CIT301 MODULE 4
SELF-ASSESSMENT EXERCISE
44
CIT301 MODULE 4
4.0 CONCLUSION
After writing a C program, you will need to compile and execute it. This
involves some steps which has been heighted in this unit.
5.0 SUMMARY
In this unit, you have been taken through the compilation and execution
steps. These include preprocessing, compilation, assembly, linking and
loading. these steps are illustrated with a diagram for clearer
understanding.
45
CIT301 MODULE 4
46
CIT301 MODULE 4
47