Prog Lecture 1
Prog Lecture 1
Learning Outcomes
1. Understanding the Basics: You will gain a solid understanding of the fundamental concepts
of C programming, including variables, data types, operators, control structures (such as
loops and conditionals), functions, and basic input/output operations. You will be able to
write simple C programs that demonstrate your comprehension of these foundational
elements.
2. Programming Logic: You will develop your programming logic and problem-solving skills
through various exercises challenges. You will learn how to break down problems into
smaller, manageable tasks and implement algorithms using C programming constructs.
What is Programming?
The purpose of programming is to create a program that exhibits a certain desired behavior.
The process of writing source code often requires expertise in many different subjects, including
knowledge of the application domain, specialized algorithms and formal logic.
The lowest-level programming language (except for computers that utilize programmable
microcode) Machine languages are the only languages understood by computers.
While easily understood by computers, machine languages are almost impossible for humans to
use because they consist entirely of numbers. Programmers, therefore, use either a high-level
programming language or an assembly language.
An assembly language contains the same instructions as a machine language, but the instructions
and variables have names instead of being just numbers.
Page 1 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
Programs written in high-level languages are translated into assembly language or machine
language by a compiler.
Assembly language programs are translated into machine language by a program called an
assembler.
Assembly Language?
A programming language that is once removed from a computer's machine language. Machine
languages consist entirely of numbers and are almost impossible for humans to read and write.
Assembly languages have the same structure and set of commands as machine languages, but they
enable a programmer to use names instead of numbers.
Each type of CPU has its own machine language and assembly language, so an assembly language
program written for one type of CPU won't run on another. In the early days of programming, all
programs were written in assembly language.
Now, most programs are written in a high-level language such as FORTRAN or C. Programmers
still use assembly language when speed is essential or when they need to perform an operation that
isn't possible in a high-level language.
What is a Compiler?
The compiler derives its name from the way it works, looking at the entire piece of source code
and collecting and reorganizing the instructions.
Thus, a compiler differs from an interpreter, which analyzes and executes each line of source code
in succession, without looking at the entire program.
The advantage of interpreters is that they can execute a program immediately. Compilers require
some time before an executable program emerges.
Page 2 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
However, programs produced by compilers run much faster than the same programs executed by
an interpreter.
Every high-level programming language (except strictly interpretive languages) comes with a
compiler.
In effect, the compiler is the language, because it defines which instructions are acceptable.
Because compilers translate source code into object code, which is unique for each type of
computer, many compilers are available for the same language.
For example, there is a FORTRAN compiler for PCs and another for Apple Macintosh computers.
In addition, the compiler industry is quite competitive, so there are actually many compilers for
each language on each type of computer.
More than a dozen companies develop and sell C++ compilers for the PC.
What is an Interpreter?
A program that executes instructions written in a high-level language. There are two ways to run
programs written in a high-level language.
The most common is to compile the program; the other method is to pass the program through an
interpreter.
An interpreter translates high-level instructions into an intermediate form, which it then executes.
In contrast, a compiler translates high-level instructions directly into machine language.
The advantage of an interpreter, however, is that it does not need to go through the compilation
stage during which machine instructions are generated.
Page 3 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
This process can be time-consuming if the program is long. The interpreter, on the other hand, can
immediately execute high-level programs.
For this reason, interpreters are sometimes used during the development of a program, when a
programmer wants to add small sections at a time and test them quickly.
In addition, interpreters are often used in education because they allow students to program
interactively.
Both interpreters and compilers are available for most high-level languages.
However, BASIC and LISP are especially designed to be executed by an interpreter. In addition,
page description languages, such as PostScript, use an interpreter.
Every PostScript printer, for example, has a built-in interpreter that executes PostScript
instructions.
Such languages are considered high-level because they are closer to human languages and further
from machine languages.
In contrast, assembly languages are considered low-level because they are very close to machine
languages.
The main advantage of high-level languages over low-level languages is that they are easier to
read, write, and maintain.
Ultimately, programs written in a high-level language must be translated into machine language
by a compiler or interpreter.
Now there are dozens of different languages, including Ada, Algol, BASIC, COBOL, C, C++,
FORTRAN, LISP, Pascal, and Prolog.
Page 4 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
3. Special Purpose – CSL for simulation apps, ADA for real-time apps, SQL (Sequential
Query Language) for databases
4. General Purpose – BASIC (Beginners All Purpose Symbolic Instruction Code), Pascal, C,
C++ - popular for training beginner of programming because is nearer to English language.
Java, Small talk – popular for object oriented programming.
What is C Programming?
It is a procedural programming language, which means that programs written in C are composed
of a series of steps or procedures to be executed in order.
C is known for its efficiency, flexibility, and low-level control over computer hardware, making it
suitable for systems programming and embedded systems.
1. Efficiency: C allows for direct memory manipulation and provides low-level access to
computer hardware, enabling efficient execution of programs.
2. Portability: C programs can be written to be highly portable, meaning they can be compiled
and executed on different computer systems with little or no modification.
3. Modularity: C supports modular programming, allowing code to be organized into reusable
modules, functions, and libraries.
4. Syntax: The syntax of C is relatively simple and compact, making it easy to read and write
C code.
5. Standard Library: C comes with a standard library that provides a set of functions and
macros for common operations, such as input/output handling, string manipulation, and
memory allocation.
C has had a significant influence on the development of many other programming languages, and
it is still widely used today in various domains, including operating systems, embedded systems,
game development, and high-performance computing.
Why Use C?
1. Efficiency: C is a low-level language that provides direct access to memory and hardware
resources. This allows programmers to write highly efficient code with fine-grained control
over system resources. C programs can be optimized for speed and memory usage, making
it suitable for performance-critical applications.
Page 5 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
2. Portability: C programs can be compiled and executed on different platforms with minimal
modifications. The C language is standardized, and compilers are available for various
operating systems and hardware architectures. This portability makes it easier to develop
software that can run on different devices and platforms.
3. Hardware Control: C allows programmers to directly manipulate hardware components,
such as memory addresses and I/O ports. This level of control is essential for system-level
programming, device drivers, and embedded systems, where interaction with hardware is
necessary.
4. Extensive Libraries: C has a rich set of libraries that provide pre-written functions and
modules for common tasks, such as file handling, string manipulation, and mathematical
operations. These libraries make development faster and more convenient by providing
ready-to-use solutions.
5. Procedural Language: C follows a procedural programming paradigm, which breaks down
programs into smaller, modular functions. This approach promotes code reusability,
maintainability, and readability. Procedural programming is well-suited for developing
structured and organized codebases.
6. Legacy Code: C has been around for several decades and has a vast codebase in many
industries. Many existing systems and libraries are written in C, making it necessary to use
C for maintaining and integrating with legacy code.
7. Learning and Understanding: C is often taught as an introductory language in computer
science and programming courses. Learning C helps in understanding fundamental
programming concepts, memory management, and the inner workings of a computer.
While C has many advantages, it's important to note that it is a lower-level language compared to
more modern languages.
This means that programming in C often requires more manual memory management and attention
to detail, which can make it more prone to certain types of errors if not handled carefully.
Nonetheless, the efficiency, control, and portability offered by C make it a popular choice for
various applications.
Procedural programming is a programming paradigm that focuses on organizing code into a series
of procedures or functions.
Page 6 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
These procedures are executed in a sequential manner, where one procedure calls another to
accomplish a particular goal.
1. Procedures: Procedures are self-contained blocks of code that encapsulate a specific set of
instructions. They take input, perform operations, and produce output. Procedures can be
reused and called multiple times from different parts of the program.
2. Sequential Execution: Procedural programming follows a linear execution flow, where
instructions are executed in the order they appear in the code. The program starts from the
beginning and proceeds sequentially, with control flowing from one procedure to the next.
3. Modularity: Procedural programming emphasizes modularity and code reusability.
Breaking down a program into smaller procedures allows for easier maintenance,
debugging, and testing. It also promotes code organization and makes it more manageable
as the program grows.
4. Data and Procedure Separation: Procedural programming separates data and procedures.
Data is typically stored in variables, and procedures operate on this data by passing it as
arguments or returning values.
5. Limited Code Reuse: Procedural programming lacks some of the code reuse mechanisms
found in other paradigms, such as object-oriented programming. Reusability is mainly
achieved through the use of procedures, where common functionality can be extracted into
separate procedures and called from different parts of the program.
6. Global Data: In procedural programming, data can be shared globally across procedures,
allowing for communication and data exchange between different parts of the program.
However, excessive use of global data can lead to potential issues, such as code complexity
and difficulty in understanding and debugging.
Procedural programming is often used for developing small to medium-sized programs, command-
line utilities, and scripts.
However, it's important to note that procedural programming is just one of many programming
paradigms, and different paradigms have different strengths and weaknesses.
Modularity is generally desirable, especially in large, complicated programs. Inputs are usually
specified syntactically in the form of arguments and the outputs delivered as return values.
Page 7 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
Scoping is another technique that helps keep procedures strongly modular. It prevents the
procedure from accessing the variables of other procedures (and vice-versa), including previous
instances of itself, without explicit authorization.
Less modular procedures, often used in small or quickly written programs, tend to interact with a
large number of variables in the execution environment, which other procedures might also
modify.
The focus of procedural programming is to break down a programming task into a collection of
variables, data structures, and subroutines, whereas in object-oriented programming it is to break
down a programming task into classes with each "class" encapsulating its own methods
(subroutines).
The most important distinction is whereas procedural programming uses procedures to operate on
data structures, object-oriented programming bundles the two together so an "object", which is an
instance of a class, operates on its "own" data structure.
Nomenclature varies between the two, although they have similar semantics:
The principles of modularity and code reuse in practical functional languages are fundamentally
the same as in procedural languages, since they both stem from structured programming. So for
example:
Procedures correspond to functions. Both allow the reuse of the same code in various parts
of the programs, and at various points of its execution.
By the same token, procedure calls correspond to function application.
Functions and their invocations are modularly separated from each other in the same
manner, by the use of function arguments, return values and variable scopes.
Page 8 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
The main difference between the styles is that functional programming languages remove or at
least deemphasize the imperative elements of procedural programming. The feature set of
functional languages is therefore designed to support writing programs as much as possible in
terms of pure functions:
Many functional languages, however, are in fact impurely functional and offer
imperative/procedural constructs that allow the programmer to write programs in procedural style,
or in a combination of both styles. It is common for input/output code in functional languages to
be written in a procedural style.
From this point of view, logic programs are declarative, focusing on what the problem is, rather
than on how to solve it.
However, the backward reasoning technique, implemented by SLD resolution, used to solve
problems in logic programming languages such as Prolog, treats programs as goal-reduction
procedures.
Experienced logic programmers use the procedural interpretation to write programs that are
effective and efficient, and they use the declarative interpretation to help ensure that programs are
correct.
Page 9 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
This leads to programs written with only the following code structures:
Many applications described as coded with Structured Programming, are really built with a
Procedural Programming Language, that may be full, partially or none structured.
This involves specifying the program’s output, input, variables, constants and the general steps
(tasks) needed to transform the inputs into the outputs.
A program’s design is a step-by-step description of how the above steps (in part 1) should be
exactly carried out so as to solve the problem. It represents the exact logic of the solution to the
problem. It includes all the appropriate formulae to be used in the solution. The design is also
known as an algorithm.
It should be clear, precise and easy to follow. This is important in avoiding errors, as well
as making the design modifiable by future programmers.
A program’s design should also show the exact logic of the program. For the calculations
that need to be done, it should show the appropriate sequence of formulae.
It should also be general i.e. not specific to a programming language, such that it can be
converted into a program using any programming language.
Page 10 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
Two most used tools for designing a program are pseudo codes and flowcharts diagrams.
(a) Pseudo Codes – are English-like statements that look similar to many procedural-
programming languages.
e.g.
Input x
(b) Flowchart Symbols – are symbols used to design a program. They are a diagrammatic way
of representing the program’s logic.
Symbols Meaning
Begin, End
Input, Output
The algorithm produced in step (2.) above is then converted into actual program (code) using an
appropriate programming language e.g. C++.
The actual statements of the program are also known as the code of the program.
Page 11 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
4. Compile (or convert it into a machine language program), Test and run
This involves making changes to improve the functionality of the program (e.g. to improve on the
program), to remove possible previously undetected errors, to cater for newly discovered user
needs, or to cater for changed technology.
EXAMPLE
SOLUTION
(ii) Inputs
(iii) Tasks
Constant
BEGIN
pi=3.14
INPUT Radius
Area=pi*Radius*Radius
Circumference=2*pi*Radius
END
Begin
pi=3.14
Input Radius
Area=pi*Radius*Radius
Circumference=2*pi*Radius
End
Example:
#include <stdio.h>
#define PI 3.14
Page 13 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming
int main()
scanf("%lf", &Radius);
Circumference = 2 * PI * Radius;
printf("\n Area of the circle: %lf Circumference of the circle is: %lf\n", Area, Circumference);
return 0;
Page 14 of 14