0% found this document useful (0 votes)
5 views

ICS2102 C Introduction

The document provides an introduction to computer programming, emphasizing the importance of software in operating hardware and the process of programming itself. It covers various programming languages, including machine, assembly, and high-level languages, and discusses the significance of algorithms, pseudocode, and flowcharts in programming. Additionally, it introduces the C programming language, its applications, and basic syntax, along with examples of simple programs.

Uploaded by

alvinmuriuki714
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ICS2102 C Introduction

The document provides an introduction to computer programming, emphasizing the importance of software in operating hardware and the process of programming itself. It covers various programming languages, including machine, assembly, and high-level languages, and discusses the significance of algorithms, pseudocode, and flowcharts in programming. Additionally, it introduces the C programming language, its applications, and basic syntax, along with examples of simple programs.

Uploaded by

alvinmuriuki714
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

ICS2102 Introduction to computer

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

• The operating system (OS) is a program


that manages and controls a computer’s
activities.
• The popular operating systems for
general-purpose computers are
Microsoft Windows, Mac OS, and
Linux.
• Application programs, such as a Web
browser or a word processor, cannot run
unless an operating system is installed
and running on the computer.

16
Algorithm

 From programming point of view, an algorithm is a step-by-step procedure to


resolve any problem. An algorithm is an effective method expressed as a finite set
of well-defined instructions.
 Thus, a computer programmer lists down all the steps required to resolve a problem
before writing the actual code.
 Following is a simple example of an algorithm to find out the largest number from a
given list of numbers
Algorithm

 The above algorithm has been


written in a crude way to help
beginners understand the
concept.
 You will come across more
standardized ways of writing
computer algorithms as you
move on to advanced levels of
computer programming.
Pseudocode
 Pseudocode is an informal high-level description of the operating principle of a computer
program or other algorithm.
 Pseudocode is one method of designing or planning a program. Pseudo means false, thus
pseudocode means false code. A better translation would be the word fake or imitation.
Pseudocode is fake (not the real thing).
 It looks like (imitates) real code but it is NOT real code. It uses English statements to
describe what a program is to accomplish.
 It is fake because no compiler exists that will translate the pseudocode to any machine
language. Pseudocode is used for documenting the program or module design (also
known as the algorithm).
Pseudocode
A flowchart

 A flowchart is a type of diagram that


represents an algorithm, workflow or
process. The flowchart shows the steps
as boxes of various kinds, and their
order by connecting the boxes with
arrows.
 This diagrammatic representation
illustrates a solution model to a given
problem. Flowcharts are used in
analysing, designing, documenting or
managing a process or program in
various fields.
Algorithm, Pseudocode & Flowchart
Algorithm Flowchart and A program
C programming
 What is C?
C is a general-purpose programming language created by Dennis
Ritchie at the Bell Laboratories in 1972.
 It is a very popular language, despite being old. The main reason for
its popularity is because it is a fundamental language in the field of
computer science.
C is strongly associated with UNIX, as it was developed to write the
UNIX operating system.
Why Learn C?

1. It is one of the most popular programming languages in the world

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

3. C is very simple and easy to understand

4. C is very fast, compared to other programming languages, like Java and Python

5. C is very versatile; it can be used in both applications and technologies


C programming

 Difference between C and  Get Started With C


C++  To start using C, you need two
 C++ was developed as an things:
‾ A text editor, like Notepad, to
extension of C, and both
write C code
languages have almost the
‾ A compiler, like GCC, to
same syntax
translate the C code into a
 The main difference language that the computer will
between C and C++ is that understand
C++ support classes and  There are many text editors and
objects, while C does not compilers to choose from. In
this tutorial, we will use
an IDE (see below).
C Install IDE

 An IDE (Integrated Development Environment) is used to edit AND compile the


code.
 Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free,
and they can be used to both edit and debug C code.
 Note: Web-based IDE's can work as well, but functionality is limited.
 We will use Code::Blocks in our tutorial, which we believe is a good place to start.
 You can find the latest version of Codeblocks at http://www.codeblocks.org/.
Download the mingw-setup.exe file, which will install the text editor with a
compiler.
C QuickStart

 Let's create our first C file.


 Open Codeblocks and go to File > New > Empty File.
 Writethe following C code and save the file
as myfirstprogram.c (File > Save File as):
 myfirstprogram.c
 Syntax
 You have already seen the following code a couple of times in the first chapters.
Let's break it down to understand it better:
A program to find the sum of two
numbers
 #include <stdio.h>
 int main()
{
int x, y, z;
 printf("Enter two numbers to add\n");
scanf("%d%d", &x, &y);
 z = x + y;
 printf("Sum of the numbers = %d\n", z);
 return 0;
}
Example explained

 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

 A computer program is a list of "instructions" to be "executed" by a computer.


 In a programming language, these programming instructions are called statements.
 The following statement "instructs" the compiler to print the text "Hello World" to the
screen:
 It is important that you end the statement with a semicolon;
 If you forget the semicolon (;), an error will occur and the program will not run:
Many Statements

 Most C programs contain many statements.


 The statements are executed, one by one, in the same order as they are
written:
Example explained

 From the example above, we have three statements:

1. printf("Hello World!");

2. printf("Have a good day!");

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)

 Output (Print Text)


 To output values or print text in C, you can use the printf() function
Double Quotes

 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

 To insert a new line, you can use the \n character:


 You can also output multiple lines with a single printf () function.
However, this could make the code harder to read:
 Tip: Two \n characters after each other will create a blank line:
C Comments

 Comments can be used to explain code, and to make it more


readable. It can also be used to prevent execution when testing
alternative code.
 Comments can be singled-lined or multi-lined.
Single-line Comments

 Single-line comments start with two forward slashes (//).


 Any text between // and the end of the line is ignored by the compiler
(will not be executed).
 This example uses a single-line comment before a line of code:
C Multi-line Comments

 Multi-line comments start with /* and ends with */.


 Any text between /* and */ will be ignored by the compiler:
Single or multi-line comments?

 Itis up to you which you want to use. Normally,


we use // for short comments, and /* */ for
longer.
 Good to know: Before version C99 (released in
1999), you could only use multi-line comments
in C.
Programming Errors

 Syntax Errors
– Detected by the compiler
 Runtime Errors
– Causes the program to abort
 Logic Errors
– Produces incorrect result

53

You might also like