0% found this document useful (0 votes)
8 views25 pages

Module 3 4

Structured Programming:module 4

Uploaded by

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

Module 3 4

Structured Programming:module 4

Uploaded by

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

CIT301 MODULE 3

MODULE 3 ELEMENTS OF STRUCTURED


PROGRAMMING

Unit 1 Overview of Structured Programming


Unit 2 Programming Language Paradigms

UNIT 1 OVERVIEW OF STRUCTURED


PROGRAMMING

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

In structured programming design, programs are broken into different


functions. These functions are also known as modules, subprograms,
subroutines or procedures. Structured programming minimizes the
chances of the function affecting another. It allows for clearer programs
code. It made global variables to disappear and replaced by the local
variables.

2.0 INTENDED LEARNING OUTCOMES (ILOS)

By the end of this unit, you should be able to:

 State the advantages and disadvantages of structured


programming
 Describe the examples of programming paradigms.

23
CIT301 MODULE 3

3.0 MAIN CONTENTS

3.1 Structured programming Concept

In structured programming design, programs are broken into different


functions these functions are also known as modules, subprogram,
subroutines or procedures.

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.

Structured programming minimizes the chances of the function affecting


another. It allows for clearer programs code. It made global variables to
disappear and replaced by the local variables. Due to this change one
can save the memory allocation space occupied by the global variable.
Its organization helps in the easy understanding of programming logic.
So that one can easily understand the logic behind the programs. It also
helps the newcomers of any industrial technology company to
understand the programs created by their senior workers of the industry.
The languages that support Structured programming approach are:
• C
• C++
• Java
• C#
• Pascal

3.2 Advantages of Structured programming

 It is user friendly and easy to understand.


 Similar to English vocabulary of words and symbols.
 It is easier to learn.
 They require less time to write.
 They are easier to maintain.
 These are mainly problem oriented rather than machine based.
 Program written in a higher-level language can be translated into
many machine languages and therefore can run on any computer
for which there exists an appropriate translator.
 It is independent of machine on which it is used, i.e., programs
developed in high level languages can be run on any computer.

24
CIT301 MODULE 3

3.3. Disadvantages of Structured Programming

 Structured programming codes implemented in high-level


language has to be translated into the machine language by
translator and thus a price in computer time is paid.
 The object code generated by a translator might be inefficient
compared to an equivalent assembly language program.
 Data type are proceeds in many functions in a structured program.
When changes occur in those data types, the corresponding change
must be made to every location that acts on those data types within
the program. This is really a very time- consuming task if the
program is very large.
 In a structured program, each programmer is assigned to build a
specific set of functions and data types. Since different
programmers handle separate functions that have mutually shared
data type, other programmers in the team must reflect the changes
in data types done by the programmer in data type handled.
Otherwise, it requires rewriting several functions.

SELF-ASSESSMENT EXERCISE

Describe how structured programming can lead to programming


efficiency.

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

Structured programming design allows a program to be subdivided into


different functions. These functions can also be called modules,
subprogram, subroutines and procedures. Each function performs a
particular task. These functions are logically combined to carry out the
desired task.

25
CIT301 MODULE 3

5.0 SUMMARY

In this unit, you have learnt structured programming concept, Advantages


and Disadvantages of Structured programming. Some programming
languages that support Structured programming approach are also listed.

4.0 TUTOR-MARKED ASSIGNMENT

5.0 REFERENCES/FURTHER READING

Fundamentals of Structured Programming, Lubia Vinhas, 2016

Structured Programming with C++ by Kjell Backman, 2012

26
CIT301 MODULE 3

UNIT 2 PROGRAMMING PARADIGMS

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

A programming paradigm, or programming model, is an approach to


programming a computer based on a mathematical theory or a coherent
set of principles. Examples of these paradigms are imperative, functional,
logical, object-oriented paradigms and others.

2.0 INTENDED LEARNING OUTCOMES (ILOS)

By the end of this unit, you should be able to:

 State the various programming paradigms


 Describe each of the named programming paradigms and
programming languages associated with it.

3.0 MAIN CONTENTS

3.1 Programming paradigms

Solving a programming problem requires choosing the right concepts. All


but the smallest toy problems require different sets of concepts for
different parts of the program. A programming paradigm, or
programming model, is an approach to programming a computer based
on a mathematical theory or a coherent set of principles. It is a way of
conceptualizing what it means to perform computation and how tasks to
be carried out on the computer should be structured and organized.
Programming languages are used to realize programming paradigms.
Examples of programming paradigms are: imperative, functional, logical,
object-oriented. Most popular languages are imperative and use

27
CIT301 MODULE 3

structured programming techniques. Structured programming techniques


involve giving the code you write structures, these often involve writing
code in blocks such as sequence (code executed line by line), selection
(branching statements such as if..then..else, or case) and repetition
(iterative statements such as for, while, repeat, loop).

3.2 Imperative paradigm

This paradigm is based on the ideas of a Von Neummann architecture. A


command has a measurable effect on the program and the order of
commands is important. First do this and next do that. Its main
characteristics are incremental change of the program state (variables) as
a function of time; execution of commands in an order governed by
control structures; and the use of procedures, abstractions of one or more
actions, which can be called as a single command. Examples: Fortran,
Algol, Basic, C, Pascal.

3.3 Functional paradigm

This paradigm is based on mathematics and theory of functions. The


values produced are non-mutable and plays a minor role compared to
imperative program. All computations are done by applying functions
with no side effects. Functions are firsts class citizens. Evaluate an
expression and use the resulting value for something. Example: Haskell.

3.4 Logical paradigm

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.

3.5 Object-oriented paradigm

Data as well as operations are encapsulated in objects. Information hiding


is used to protect internal properties of an object. Objects interact by
means of message passing. In most object-oriented languages, objects are
grouped in classes and classes are organized as hierarchies. Examples:
C++, Java.

28
CIT301 MODULE 3

3.6 Other Paradigms

Other paradigms include Visual paradigm, Constraint based paradigm,


Aspect oriented paradigm and Event-oriented paradigm.

SELF-ASSESSMENT EXERCISE

i. List the examples of object-oriented programming language


ii. Differentiate with examples, the difference between functional and
object-oriented programming paradigms.

Solution
1) List the examples of object-oriented programming language
 Java
 C++
 C#
 Ruby
 Python
 TypeScript
 PHP

2) Differentiate with examples, the difference between functional and


object-oriented programming paradigms
Object-oriented programming, or simply referred to as “OOP”, is
a software programming model based on the concept of objects,
instead of just functions and procedures. OOP is so designed that
real world concepts can be programmed in a computer program.
As the name suggests, OOP uses objects in programming which
are organized into classes, allowing individual objects to be
grouped together. Each object in OOP is responsible for a set of
tasks. So, various tasks in the program are performed, by invoking
the operations defined on corresponding objects. Although, the
fundamental features of OOP were invented in the 1960s, it wasn’t
until the 1980s that object-oriented languages actually started
getting the attention. OOP is a revolutionary idea and there are a
number of reasons why it has become the dominant programming
paradigm in the last few decades.
While
Functional programming is a programming methodology that emphasizes
on the use of function calls as the primary programming construct. It
provides practical approaches to problem solving in general and insights
into many aspects of computing. It is a style of programming where you
focus on the evaluation of expressions rather than the execution of
commands. You use expressions to transform the data in the functional
programming approach which ideally doesn’t contain side effects. Like
the name implies, it uses functions as building

29
CIT301 MODULE 3

blocks to create new functions. Functional programming is generally


regarded as a programming paradigm that can be applied in many
languages, even those that weren’t intended to be used with that paradigm.
In functional languages, there is no fixed execution order meaning the
order does not affect the final result. Higher order functions are also
important in functional programming.

4.0 CONCLUSION

A programming paradigm, or programming model, is an approach to


programming a computer based on a mathematical theory or a coherent
set of principles.

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.

6.0 TUTOR-MARKED ASSIGNMENT

7.0 REFERENCES/FURTHER READING

Fundamentals of Structured Programming, Lubia Vinhas, 2016

Structured Programming with C++ by Kjell Backman, 2012

30
CIT301 MODULE 4

MODULE 4 STRUCTURED PROGRAMMING WITH C

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

C programming language is a structure-oriented programming language,


developed at Bell Laboratories in 1972 by Dennis Ritchie. It is defined
with C Types taxonomy. C language is popular because it is reliable,
simple and easy to use among others. It is also characterized with supports
for loose typing and extensive use of function calls. The C programming
language is used for developing system applications that forms a major
portion of operating systems such as Windows, UNIX and Linux.

2.0 INTENDED LEARNING OUTCOMES (ILOS)

By the end of this unit, you should be able to:

 Give a brief history of C Programming Language.


 Explain the taxonomy of C programming types
 Explain the importance of studying C programming language.
 Describe the characteristics and uses of C Program.

31
CIT301 MODULE 4

3.0 MAIN CONTENTS

3.1 Brief History of C

 The C programming language is a structure-oriented programming


language, developed at Bell Laboratories in 1972 by Dennis
Ritchie.
 C programming language features were derived from an earlier
language called “B” (Basic Combined Programming Language –
BCPL)
 C language was invented for implementing UNIX operating
system.
 In 1978, Dennis Ritchie and Brian Kernighan published the first
edition “The C Programming Language” and is commonly known
as K&RC.
 In 1983, the American National Standards Institute (ANSI)
established a committee to provide a modern, comprehensive
definition of C. The resulting definition, the ANSI standard, or
“ANSI C” was completed late 1988.
 Many of C’s ideas & principles were derived from the earlier
language B, thereby naming this new language “C”.

3.2 Taxonomy of C Types

 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

3.3 Why is C Popular?

 It is reliable, simple and easy to use.


 C is a small, block-structured programming language.
 C is a portable language, which means that C programs written on
one system can be run on other systems with little or no
modification.

32
CIT301 MODULE 4

 C has one of the largest assortments of operators, such as those


used for calculations and data comparisons.
 Although the programmer has more freedom with data storage, the
languages do not check data type accuracy for the programmer.

3.4 Why Study C?

 By the early 1980s, C was already a dominant language in the


minicomputer world of Unix systems. Since then, it has spread to
personal computers (microcomputers) and to mainframes.
 Many software houses use C as the preferred language for
producing word processing programs, spreadsheets, compilers,
and other products.
 C is an extremely flexible language—particularly if it is to be used
to write operating systems.
 Unlike most other languages that have only four or five levels of
precedence, C has 15.

3.5 Characteristics of a C Program

 Middle level language.


 Small size – has only 32 keywords
 Extensive use of function calls- enables the end user to add their
own functions to the C library.
 Supports loose typing – a character can be treated as an integer &
vice versa.
 Structured language
 Low level (Bit Wise) programming readily available
 Pointer implementation - extensive use of pointers for memory,
array, structures and functions.
 It has high-level constructs.
 It can handle low-level activities.
 It produces efficient programs.
 It can be compiled on a variety of computers

3.6 Uses of C

 The C programming language is used for developing system


applications that forms a major portion of operating systems such
as Windows, UNIX and Linux. Below are some examples of C
being used:
 Database systems
 Graphics packages
 Word processors

33
CIT301 MODULE 4

 Spreadsheets
 Operating system development
 Compilers and Assemblers
 Network drivers and Interpreters

SELF-ASSESSMENT EXERCISE

i. State the uses of C Programming language


ii. Explain the reasons C language is popular.
iii. List the application packages that C language is considered useful
tools for developments.
Solution:
1) State the uses of C Programming language
C is used for developing system applications such as:
 Database Systems
 Language Interpreters
 Compilers and Assemblers
 Operating Systems
 Network Drivers
 Word Processors

1) Explain the reasons C language is popular


There are various reasons why C has become popular. The following are
some of the reasons:
 C can be learnt quickly.
 C language is reliable, simple and easy to use.
 C language is a structured language.
 Modern programming concepts are based on C.
 It can be compiled on a variety of computer platforms
 C has one of the largest assortments of operators, such as those
used for calculations and data comparisons.
 Universities preferred to add C programming in their courseware.

2) List the application packages that C language is considered useful


tools for developments.
C programming language can be used for the development of the
following application packages:
 Inventory systems
 Medical diagnostic systems
 Expert systems
 Accounting systems
 Scheduling systems
 etc

34
CIT301 MODULE 4

4.0 CONCLUSION

C programming language is a structure-oriented programming language,


developed at Bell Laboratories in 1972 by Dennis Ritchie. C programs are
reliable, simple and easy to use among others. C programming language
is not a strongly typed language. It makes use of extensive function calls.
The C programming language is used for developing system applications
that forms a major portion of operating systems such as Windows, UNIX
and Linux etc.

5.0 SUMMARY

In this unit you learnt a brief history of C programming language,


taxonomy of C types, the need to study C programming language. you
also learnt some characteristics of C program and it uses.

6.0 TUTOR-MARKED ASSIGNMENT

7.0 REFERENCES/FURTHER READING

Fundamentals of Structured Programming, Lubia Vinhas, 2016

Structured Programming with C++ by Kjell Backman, 2012

35
CIT301 MODULE 4

UNIT 2 C PROGRAM DESIGN

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

The structure of a C program is a protocol (rules) to the programmer,


which he has to follow while writing a C program. A number of files are
used in a C Program. Examples of these files are: source, object, header
and executable files.

2.0 INTENDED LEARNING OUTCOMES (ILOS)

By the end of this unit, you should be able to:

 Illustrate the structure of a C Programming language.


 Explain the contents of the C program structure.

3.0 MAIN CONTENTS

3.1 C Program Structure

The structure of a C program is a protocol (rules) to the programmer,


which he has to follow while writing a C program. The general basic
structure of C program is shown in the code below.Based on this structure,
we can write a C program. Example:
/* This program accepts a number and displays it to the user*/
#include <stdio.h>
void main(void)
{ int number;
printf( "Please enter a number: " );
scanf( "%d", &number );
printf( "You entered %d", number );
return 0;
}

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

 If the compiler does not find one of these characters where it


expects to see one, then it will produce an error.
 scanf:In other programming languages, the printing and reading
functions are a part of the language. In C this is not the case; instead
they are defined as standard functions which are part of the
language specification, but are not a part of the language itself.
 The standard input/output library contains a number of functions
for formatted data transfer; the two we are going to use are scanf
(scan formatted) and printf (print formatted).
 printf:The printf function is the opposite of scanf.
 It takes text and values from within the program and sends it out
onto the screen.
 Just like scanf, it is common to all versions of C and just like
scanf, it is described in the system file stdio.h.
 The first parameter to a printf is the format string, which contains
text, value descriptions and formatting instructions.

3.2 Files Used in a C Program

 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

i. Explain all the reserved words used in the description of a C


program structure.
ii. Describe the types of files used in a C Program

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.

Describe the types of files used in a C Program


C programming language supports two types of files and they are as
follows...
1. Text Files (or) ASCII Files
2. Binary Files
Text Files (or) ASCII Files
 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.

40
CIT301 MODULE 4

 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.
Binary 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.

4.0 CONCLUSION

To write a C program certain must be followed. First of you must be


conversant with it structure and content, otherwise you will not be able
write a reasonable C program.

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.

6.0 TUTOR-MARKED ASSIGNMENT

7.0 REFERENCES/FURTHER READING

Fundamentals of Structured Programming, Lubia Vinhas, 2016

Structured Programming with C++ by Kjell Backman, 2012

41
CIT301 MODULE 4

UNIT 3 EXECUTING A C PROGRAM

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

Program compilation and execution processes are divided into several


steps, namely: preprocessing, compilation, assembly, linking and loading.
In each of these input and output are defined during the compilation and
execution process depending on the operating systems e.g., Linux.

2.0 INTENDED LEARNING OUTCOMES (ILOS)

By the end of this unit, you should be able to:

 Explain the processes involved in compilation and execution of C


program.
 Describe the sample input/ output steps used in program
compilation and execution.

3.0 MAIN CONTENTS

3.1 Compilation and Execution of a C Program

 The compilation and execution process of C can be divided into


several steps:
 Preprocessing - Using a Preprocessor program to convert C source
code in expanded source code. "#includes" and "#defines"
statements will be processed and replaced source codes in this step.
 Compilation - Using a Compiler program to convert C expanded
source to assembly source code.

42
CIT301 MODULE 4

 Assembly - Using an Assembler program to convert assembly


source code to object code.
 Linking - Using a Linker program to convert object code to
executable code. Multiple units of object codes are linked to
together in this step.
 Loading - Using a Loader program to load the executable code
into CPU for execution.

Sample I/O steps


Here is a simple table showing input and output of each step in the
compilation and execution process:
Input Program Output
source code > Preprocessor > expanded source code
expanded source code > Compiler > assembly source code
assembly code > Assembler > object code
object code > Linker > executable code
executable code > Loader > execution

3.2 Commonly used Programs for execution on Linux


System

Below are examples of commonly used programs for different


compilation and execution steps on a Linux system:
 "cpphello.c -o hello.i" - Preprocessor preprocessing hello.c and
saving output to hello.i.
 "cc1hello.i -o hello.s" - Compiler compiling hello.i and saving
output to hello.s.
 "as hello.s -o hello.o" - Assembler assembling hello.s and saving
output to hello.o.
 "ldhello.o -o hello" - Linker linking hello.o and saving output to
hello.
 "load hello" - Loader loading hello and running hello.

3.3 Pictorial Diagram of C Compilation and Execution

A pictorial diagram showing the compilation and execution of a C


program is shown following.

43
CIT301 MODULE 4

SELF-ASSESSMENT EXERCISE

i. Illustrate the compilation and execution of a C program with


diagram only.
ii. State the examples of commonly used programs for different
compilation and execution steps on a Linux system

Illustrate the compilation and execution of a C program with


diagram only.

44
CIT301 MODULE 4

1 State the examples of commonly used programs for different


compilation and execution steps on a Linux system

 "cpphello.c -o hello.i" - Preprocessor preprocessing hello.c and


saving output to hello.i.
 "cc1hello.i -o hello.s" - Compiler compiling hello.i and saving
output to hello.s.
 "as hello.s -o hello.o" - Assembler assembling hello.s and saving
output to hello.o.
 "ldhello.o -o hello" - Linker linking hello.o and saving output to
hello.
 "load hello" - Loader loading hello and running hello.

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

6.0 TUTOR-MARKED ASSIGNMENT

7.0 REFERENCES/FURTHER READING

Fundamentals of Structured Programming, Lubia Vinhas, 2016

Structured Programming with C++ by Kjell Backman, 2012

46
CIT301 MODULE 4

47

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy