C as an imperative language
C program at a glance
Computer Hardware
Components
Components of a PC
Introduction
What is computer hardware?
Computer devices in physical form
Comprised of 4 components:
Input devices
Central Processing Unit (CPU)
Output devices
Memory
Chapter 2
Introduction (contd)
Processor
Control
Control
Unit
Unit
Arithmetic
Arithmetic
Logic Unit
(ALU)
Logic
Unit (ALU)
Instructions
Data
Information
Input
Devices
Data
Memory
Instructions
Data
Information
Storage
Devices
Chapter 2
Information
Output
Devices
Input Device
What is input?
Input device is any hardware component that allows users to enter data
and instructions
Chapter 2
Data
Unprocessed text,
numbers, images,
audio, and video
Instructions
Programs
Commands
User responses
Chapter 2
Words
Numbers
Punctuation
symbols and
special function commands
into the computers memory
Chapter 2
Output Device
What is output?
Chapter 2
Chapter 2
LCD monitor
Chapter 2
Output Devices
An interface by which the computer conveys the
output to the user
Example: monitor, printer
11
Main Memory
A semiconductor device which stores the
information necessary for a program to run.
2 types
ROM (Read Only Memory)
Contains information that is necessary for the
computer to boot up
The information stays there permanently even when
the computer is turned off.
12
CPU
What is the central processing unit (CPU)?
Processor
Control
Control
Unit
Unit
Arithmetic
Arithmetic
Logic
Unit (ALU)
(ALU)
Logic Unit
CPU (contd)
Register
Stores location
from where instruction
was fetched Stores data
Stores
instruction while it is
while ALU
being decoded
computes it
Stores results
of calculation
Chapter 2
Registers
Temporarily store instructions or data fetched from
memory
15
Storage Devices
A magnetic device used to store a large amount
of information.
Store the software components or data needed
for the computer to execute its tasks.
Could be read only or writable.
Example: Hard drive, CD ROM, floppy disks
16
Network Devices
Connect a computer to the other computers.
Enable the users to access data or execute
programs remotely.
Example: modem, Ethernet card
17
Natural language
Our everyday-language; spoken and written
Not 100% needed to understand:
Do you want to buy this computer ? remains
comprehensible
18
19
Formal Language
Language with limited, defined, words
Each concatenation of words ('phrase') has a
single, clearly defined meaning
no (miss-)interpretation possible
20
Functional / Imperative
Language
Functional Language:
Tell what to do, but not how:
sum [1...10]
Imperative Language:
Tell what to do, but mainly how:
Take number 1 and add the next number to it;
then add the next number to the sum; and so on;
until you have reached 10 as number to be added.
Then print the sum of all numbers
Principles of Programming - NI July 2005
21
What is Programming?
Programming is instructing a computer to do something
for you with the help of a programming language
The two roles of a programming language:
Technical: It instructs the computer to perform tasks.
Conceptual: It is a framework within which we
organize our ideas about things and processes.
In programming, we deal with two kind of things:
Data - representing 'objects' we want to manipulate
Procedures -'descriptions' or 'rules' that define how to
manipulate data.
22
Programming Language
Formal Language used to communicate to a
computer.
A programming language contains instructions
for the computer to perform a specific action or a
specific task:
'Calculate the sum of the numbers from 1 to 10
'Print I like programming
'Output the current time'
23
Programming Language
Can be classified into as a special-purpose and
general-purpose programming languages.
Special-purpose : is design for a particular type of
application
Structured Query Language (SQL)
24
Machine Language
The only language that the processor actually
'understands
Consists of binary codes: 0 and 1
Example: 00010101
11010001
01001100
25
Assembly Language
Enables machine code to be represented in words and
numbers.
Example of a program in assembler language:
LOAD A, 9999
LOAD B, 8282
SUB B
MOV C, A
LOAD C, #0002
DIV A, C
STORE A, 7002
26
High-Level Language
Use more English words. They try to resemble English
sentences. Therefore, it is easier to program in these
languages.
The programming structure is problem oriented - does not
need to know how the computer actually executes the
instructions.
Processor independent - the same code can be run on
different processors.
Examples: Basic, Fortran, Pascal, Cobol, C, C++, Java
A high level language needs to be analyzed by the compiler
and then compiled into machine code so that it can be
executed by the processor.
Principles of Programming - NI July 2005
27
C Programming Language
Why 'C' ?
Because based on 'B'; developed at Bell Laboratories
Developed by Dennis Ritchie at Bell Laboratories in
the 1960s
In cooperation with Ken Thomson it was used for
Unix systems
The C Language was only vaguely defined, not
standardized, so that almost everyone had his own
perception of it, to such an extend that an urgent
need for a standard code was creeping up
Principles of Programming - NI July 2005
28
C Programming Language
cont
In 1983, the American National Standards Institute
29
C An Imperative Language
C is a highly imperative language
We must tell it exactly how to do what;
the means and functions to use;
which libraries to use;
when to add a new line;
when an instruction is finished;
in short: everything and anything
30
A Simple Program in C
#include <stdio.h>
main()
{
printf("I like programming in C.\n");
}
31
main()
{
Start of Segment
Function for printing text
End of statement
End of Segment
32
C Output
I like programming in C.
33
Summary
We have looked at some underlying hardware
We have seen some different types of languages;
the relevance of semantics and syntax.
34