C Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

CSCI-101

Computer Fundamentals
and C Programming
Dr. Kunwar Pal
Dept. of CSE
NIT Jalandhar
What Is A Computer?
A computer is an electronic device, operating
under the control of instructions (software)
stored in its own memory unit, that can
accept data (input), manipulate data
(process), and produce information (output)
from the processing.
Generally, the term is used to describe a
collection of devices that function together
as a system.

2
Hardware Devices that comprise a computer
system

Monitor
Speaker
(output)
(output) System unit
(processor, memory…)

Printer
(output)

Storage devices
(CD-RW, Floppy,
Hard disk, zip,…)
Mouse
(input)
Scanner
Keyboard
(input)
(input)

3
Major Components of a
Computer System
Processor (CPU)
◦ Runs program instructions
Main Memory
◦ Storage for running programs and current data
Secondary Storage
◦ Long-term program & data storage (hard disk, CD, etc)
Input Devices
◦ Communication from the user to the computer(e.g. keyboard,
mouse)
Output Devices
◦ Communication from the computer to the user (e.g. monitor,
printer, speakers)
Block Diagram of Computer

5
Computer Components: Top Level
View
The CPU
The CPU is a silicon chip that contains millions of tiny electrical

components.

■The CPU’s three main parts are:


■ Control Unit
■ Arithmetic Logic Unit (ALU)
Performs
■ Registers
Arithmetic / Logic calculations and
Unit decisions

Coordinates
Control Unit
processing steps

Small, fast
Registers storage areas
for
instructions
and data
Registers
▪ Registers are small, fast memory within the CPU
▪ Different registers hold different things like instructions and addresses of instructions,
data (operands) and results of operations
▪These are of two types

1. Special Purpose Registers contain specific information the CPU needs.


▪Instruction
Register (IR) contains the actual instruction which is currently
being executed by the CPU.
▪The Program Counter (PC) contains the address of the next instruction to be
executed by the program.
2. General Purpose Registers hold:
▪ the operands for arithmetic and logical operations (ie. the values on
which the operation will be performed)
▪ the results of such operations
▪ So General Purpose Registers are used for holding and manipulating
data used by the CPU
Memory
Computer Memory : millions/billions of on/off charges
Bits: 0 or 1
Bytes: Groups of 8 bits, A byte is the smallest unit of storage.
(Can hold one text character)
Words: Groups of bits/bytes (8, 16, 32, 64-bits)

Storage is usually too large to be expressed in bytes or words.


▪ Kilobyte (KB) = 1024 bytes (210 bytes)
▪ Megabyte (MB) = 1024 x 1024 bytes or one million bytes (220 bytes)
▪ Gigabyte (GB) = 1024 x 1024 x 1024 bytes or 1 trillion bytes (230 bytes)
▪ Terabyte (TB) = 1024 x 1024 x 1024 x 1024 bytes one quadrillion bytes
(240 bytes)
MEMORY CLASSIFICATION
Registers
2ns
Cache (I, II, III)
Volatile SRAM
DRAM
Primary SDRAM
(Semiconductor – RAM EDRAM
chip). Main Memory
EDO
Memory Types & FLASH RAM
Storage Devices PROM
Non-volatile EPROM
ROM EEPROM
Tape
Magnetic memory HD, Zip Disk
Secondary FDD
(Devices)
Optical
CD-ROM, CD-R, CD-RW
Memory DVD-ROM, DVD-R 5ms
DVD- RW
PRIMARY MEMORY: RAM AND ROM
▪ RAM (Random Access Memory) is volatile (temporary). Programs
and data can be written to and erased from RAM as needed. This
means that RAM does not retain its bit configuration when the
power is turned off, but ROM does.
• SRAM: static RAM
• DRAM: dynamic RAM

▪ ROM (Read Only Memory) is nonvolatile (permanent). The


contents in locations in ROM cannot be changed. It holds
instructions that run the computer when it is first turned on (BIOS)
▪ The CPU accesses each location in memory by using a unique
number, called a memory address.
Main Memory

Each memory cell 5248 Each memory cell stores a


has a numeric 5249 set number of bits (some
10011010
address, which 5250 computers use 8 bits/one
uniquely identifies its 5251 byte, others use words)
location 5252
5253 A word is stored in
5254 consecutive
5255 memory bytes.
5256
Memory types– ROM
MROM: Masked ROM
◦ read-only memory (Pre-programmed)
▪ PROM: programmable ROM
● Re-programmed by using a special device called a PROM
programmer, generally written once
▪ EPROM: erasable PROM
● Use ultraviolet light to erase data, Re-programmed by
PROM
▪ EEPROM: electronically EPROM
● Can be erased using electronic impulses (higher
voltages)
● EEPROMs are used to store a computer system's BIOS
Program
A computer program is a collection
of instructions that performs a specific task
when executed by a computer.
Most computer devices require programs to
function properly.

Program is a sequence of instruction


along with data

14
Programming Language
A programming language is a formal
language, which comprises a set of
instructions that produce various kinds
of output.

Programming languages are used


in computer programming to
implement algorithms.

15
Programming paradigms
(models)
Unstructured

Structured

Object oriented

16
Unstructured programming
Writing small and simple programs consisting
of only one main function in the program.

Uses goto statement to jump from any


statement to any other statement (spagetti
code)

17
Structured programming
A program is broken down into small
independent tasks that are small enough to
be understood easily, without having to
understand the whole program at once.

When these tasks are completed, they are


combined together to solve the problem.

Example – C language

18
Object oriented
programming
A style of computer programming which
focuses on objects (actors) and their private
functionality.

Objects are variables of user defined data


types known as classes which contain
functions and variables. Objects interact by
using message passing.

C++ and Java are examples of OOP.

19
Components in
Programming

20
What is?
Editor

Translator
◦ Complier
◦ Assembler
◦ Interpreter
Linker
Loader
Translator
A translator is a programming language
processor
◦ that converts a computer program from one
language to another.
It takes a program written in source code and
converts it into machine code.
It discovers and identifies the error during
translation.

22
23
Complier
A compiler is a program that accepts a
source program in a “high-level language “and
produces a corresponding object program.
Assembler
Input to an assembler is an assembly
language program.
Output is an object program plus information
that enables the loader to prepare the object
program for execution.
Loader
A Loader is a routine that loads an object
program and prepares it for execution.
In general, the loader must load, relocate and
link the object program.
Loader is a program that places programs
into memory and prepares them for
execution. The loader places into memory the
machine language version of the user’s
program and transfers control to it.
The main difference between compiler interpreter and
assembler is that compiler converts the whole high
level language program to machine language at a
time while interpreter converts high level language
program to machine language line by line and
assembler converts assembly language program to
machine language.

27
First Program
#include<stdio.h>
/*first program*/
int main()
{
printf(“Hello-CP”);
return 0;
}

28
Second Program
#include<stdio.h>
/*first program*/
int main()
{
int a;
a=4+7;
printf(“value=%d”,a);
return 0;
}

29
How a simple program (C/C++)
executes??

You might also like