ICS2102 C Introduction
ICS2102 C Introduction
programming
ICS2102 Introduction to computer programming
In another subject in this class, you have already learned an important lesson of
computing: Software (programs) rules the hardware (the physical machine).
It is the software that determines what any computer can do. Without software, computers
would just be expensive paperweights. The process of creating software is called
programming, and that is the main focus of this lecture.
Computer programming is a challenging activity. Good programming requires an ability to
see the big picture while paying attention to minute detail.
Not everyone has the talent to become a first-class programmer, just as not everyone has
the skills to be a professional athlete.
However, virtually anyone can learn how to program computers. With some patience and
effort on your part, this course will help you to become a programmer. There are lots of
good reasons to learn programming.
Programs
Computer programs, known as software, are instructions to the
computer.
You tell a computer what to do through programs. Without
programs, a computer is an empty machine. Computers do not
understand human languages, so you need to use computer
languages to communicate with them.
Programs are written using programming languages.
A computer program is a sequence of instructions written using a
Computer Programming Language to perform a specified task by
the computer.
Computer programming
Computer programming is the process of designing and writing
computer programs.
The two important terms that we have used in the above
definition are −
1. Sequence of instructions
2. Computer Programming Language
Computer programming
To understand these terms, consider a situation when someone asks you
about how to go to a nearby KFC. What exactly do you do to tell him the
way to go to KFC?
You will use Human Language to tell the way to go to KFC, something as
follows:
First go straight, after half kilometre, take left from the red light and then
drive around one kilometre and you will find KFC at the right.
Computer programming
Here, you have used English Language to give several steps to be taken to reach
KFC. If they are followed in the following sequence, then you will reach KFC:
1. Go straight
2. Drive half kilometre
3. Take left
4. Drive around one kilometre
5. Search for KFC at your right side
Now, try to map the situation with a computer program. The above sequence of
instructions is actually a Human Program written in English Language, which
instructs on how to reach KFC from a given starting point. This same sequence could
have been given in Spanish, Hindi, Arabic, or any other human language, provided the
person seeking direction knows any of these languages.
In summary
• A computer program is also called a computer software, which can range from
two lines to millions of lines of instructions.
• Computer program instructions are also called program source code and computer
programming is also called program coding.
• A computer without a computer program is just a dump box; it is programs that
make computers active.
A programming language is a vocabulary and a set of grammatical rules for
instructing a computer or computing device to perform specific tasks.
The term programming language usually refers to high-level languages, such as
Python, C, C++, R, Java, C#, Swift, etc. Each programming language has a unique
set of keywords (words that it understands) and a special syntax for organizing
program instructions.
Programming Languages
Machine Language Assembly Language High-Level Language
• Machine language is a set of primitive instructions built
into every computer. The instructions are in the form of
binary code, so you have to enter binary codes for various
instructions.
• Program with native machine language is a tedious
process. Moreover the programs are highly difficult to
read and modify. For example, to add two numbers, you
might write an instruction in binary like this:
1101101010011010
8
Programming Languages
Machine Language Assembly Language High-Level Language
• Assembly languages were developed to make programming easy.
Since the computer cannot understand assembly language,
however, a program called assembler is used to convert assembly
language programs into machine code.
• For example, to add two numbers, you might write an instruction
in assembly code like this:
ADDF3 R1, R2, R3
9
Programming Languages
Machine Language Assembly Language High-Level Language
• The high-level languages are English-like and easy to learn and program. For
example, the following is a high-level language statement that computes the
area of a circle with radius 5:
area = 5 * 5 * 3.1415;
10
Popular High-Level Languages
Language Description
Ada Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
language was developed for the Department of Defense and is used mainly in defense projects.
BASIC Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily
by beginners.
C Developed at Bell Laboratories. C combines the power of an assembly language with the ease of
use and portability of a high-level language.
C++ C++ is an object-oriented language, based on C.
C# Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.
COBOL COmmon Business Oriented Language. Used for business applications.
FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications.
Java Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-
independent Internet applications.
Pascal Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a
simple, structured, general-purpose language primarily for teaching programming.
Python A simple general-purpose scripting language good for writing short programs.
Visual Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop
Basic graphical user interfaces.
11
Uses of Computer Programs
Today computer programs are being used in almost every field, household,
agriculture, medical, entertainment, defence, communication, etc. Listed below are a
few applications of computer programs:
‾ MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are
examples of computer programs.
‾ Computer programs are being used to develop graphics and special effects in movie
making.
‾ Computer programs are being used to perform Ultrasounds, X-Rays, and other
medical examinations.
‾ Computer programs are being used in our mobile phones for SMS, Chat, and voice
communication.
Interpreting/Compiling Source Code
• A program written in a high-level language is called a
source program or source code. Because a computer cannot
understand a source program, a source program must be
translated into machine code for execution.
• The translation can be done using another programming tool
called an interpreter or a compiler.
13
Interpreting Source Code
• An interpreter reads one statement from the source code, translates it to the
machine code or virtual machine code, and then executes it right away, as
shown in the following figure.
• Note that a statement from the source code may be translated into several
machine instructions.
14
Compiling Source Code
• A compiler translates the entire source code into a
machine-code file, and the machine-code file is then
executed, as shown in the following figure.
15
Operating Systems
16
Algorithm
2. If you know C, you will have no problem learning other popular programming
languages such as Java, Python, C++, C#, etc, as the syntax is similar
4. C is very fast, compared to other programming languages, like Java and Python
Line 1: #include <stdio.h> is a header file library that lets us work with
input and output functions, such as printf() (used in line 4). Header files add
functionality to C programs.
Don't worry if you don't understand how #include <stdio.h> works. Just
think of it as something that (almost) always appears in your program.
Line 2: A blank line. C ignores white space. But we use it to make the code
more readable.
Line 3: Another thing that always appear in a C program is main(). This is
called a function. Any code inside its curly brackets {} will be executed.
Line 4: printf() is a function used to output/print text to the screen. In our
example, it will output "Hello World!".
Example explained
Note that: Every C statement ends with a semicolon ;
Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
Remember: The compiler ignores white spaces. However, multiple lines makes
the code more readable.
Line 5: return 0 ends the main() function.
Line 6: Do not forget to add the closing curly bracket } to actually end the main
function.
C Statements
1. printf("Hello World!");
3. return 0;
The first statement is executed first (print "Hello World!" to the screen).
Then the second statement is executed (print "Have a good day!" to the screen).
And at last, the third statement is executed (end the C program successfully).
C Output (Print Text)
When you are working with text, it must be wrapped inside double
quotations marks "".
If you forget the double quotes, an error occurs:
Many printf Functions
You can use as many printf () functions as you want. However, note that it does not
insert a new line at the end of the output:
C New Lines
Syntax Errors
– Detected by the compiler
Runtime Errors
– Causes the program to abort
Logic Errors
– Produces incorrect result
53