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

CProgramming_unit1ver1.1

Uploaded by

kedodop600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CProgramming_unit1ver1.1

Uploaded by

kedodop600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Semester-1 B.Tech.

-ESC 103 Introduction


to UNIX and C Programming

Dr. Prakash Kumar


SARALA BIRLA UNIVERSITY
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

Module 1

Introduction to components of a computer system:


Every computer mainly consists of three basic components:-
1. Hardware
2. Software
3. User
1. Hardware
All physical components of the computer are called as computer hardware. A user can see, touch
and feel hardware of the computer. All hardware components perform any task based on the
instructions given by the computer software.
The computer hardware components are as follows...
a. Input Devices
Input devices are the devices that are used to send signals to the computer for performing
tasks. The receiver at the end is the CPU (Central Processing Unit), which has work to send
signals to the output devices. Some of the classifications of Input devices are:
 Keyboard Devices
 Pointing Devices
 Composite Devices
 Game Controller
 Visual Devices
 Audio Input Devices
Some of the input devices are described below.
i. Keyboard
The keyboard is the most frequent and widely used input device for entering data into a
computer. The keyboard layout is similar to that of a typical typewriter.
Generally, keyboards come in two sizes: 84 keys or 101/102 keys but currently keyboards
with 104 keys or 108 keys are also available for Windows and the Internet.
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

Fig: Keyboard
Types of Keys
 Numeric Keys: It is used to enter numeric data or move the cursor. It usually
consists of a set of 17 keys.
 Typing Keys: The letter keys (A-Z) and number keys (09) are among these keys.
 Control Keys: These keys control the pointer and the screen. There are four
directional arrow keys on it. Home, End, Insert, Alternate (Alt), Delete, Control (Ctrl),
etc., and Escape are all control keys (Esc).
 Special Keys: Enter, Shift, Caps Lock, Num Lock, Tab, etc., and Print Screen are
among the special function keys on the keyboard.
 Function Keys: The 12 keys from F1 to F12 are on the topmost row of the keyboard.
i. Mouse
The most common pointing device is the mouse. The mouse is used to move a little
cursor across the screen while clicking and dragging.

Fig: Mouse

ii. Joystick
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

A joystick is a pointing device that is used to move the cursor on a computer screen. A
spherical ball is attached to both the bottom and top ends of the stick.

Fig: Joystick
The joystick’s function is comparable to that of a mouse. It is primarily used in CAD
(Computer-Aided Design) and playing video games on the computer.
iii. Track Ball
Track Ball is an accessory for notebooks and laptops, which works on behalf of a mouse. It
has a similar structure to a mouse. Its structure is like a half-inserted ball and we use
fingers for cursor movement.

Fig: Track Ball


iv. Light Pen
A light pen is a type of pointing device that looks like a pen. It can be used to select a
menu item or to draw on the monitor screen.

Fig: Light Pen


v. Scanner
A scanner is an input device that functions similarly to a photocopier. The scanner collects
images from the source and converts them to a digital format that may be saved on a
disc.
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

Fig: Scanner
vi. Optical Mark Reader (OMR)
An Optical Mark Reader is a device that is generally used in educational institutions to
check the answers to objective exams. It recognizes the marks present by pencil and pen.
vii. Optical Character Reader (OCR)
OCR stands for optical character recognition, and it is a device that reads printed text.
OCR optically scans the text, character by character turns it into a machine-readable code,
and saves it to the system memory.
viii. Magnetic Ink Card Reader (MICR)
It is a device that is generally used in banks to deal with the cheques given to the bank by
the customer. It helps in reading the magnetic ink present in the code number and
cheque number.
ix. Bar Code Reader
A bar code reader is a device that reads data that is bar-coded (data that is represented
by light and dark lines).Bar-coded data is commonly used to mark things, number books,
and so on.
x. Web Camera
Because a web camera records a video image of the scene in front of it, a webcam is an
input device. Webcam is a computer-connected tiny digital video camera.
xi. Microphone
The microphone works as an input device that receives input voice signals and also has
the responsibility of converting it also to digital form. It is a very common device that is
present in every device which is related to music.
b. Output Devices
Output Devices are the devices that show us the result after giving the input to a computer
system. Output can be of many different forms like image, graphic audio, video, etc. Some
of the output devices are described below.
i. Monitor
Monitors, also known as Visual Display Units (VDUs), are a computer’s primary output
device. It creates images by arranging small dots, known as pixels, in a rectangular
pattern. The amount of pixels determines the image’s sharpness.
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

The two kinds of viewing screens used for monitors are described below.
 Cathode-Ray Tube (CRT) Monitor: Pixels are minuscule visual elements that make
up a CRT display. The higher the image quality or resolution, the smaller the pixels.
 Flat-Panel Display Monitor: In comparison to the CRT, a flat-panel display is a type
of video display with less volume, weight, and power consumption. Flat-panel displays
are currently used in calculators, video games, monitors, laptop computers, and
graphical displays.

Fig: Monitor
ii. Printer
Printers are output devices that allow you to print information on paper. There are
certain types of printers which are described below.
 Impact Printers
 Character Printers
 Line Printers
 Non-Impact Printers
 Laser Printers
 Inkjet Printers
Impact Printer
Characters are printed on the ribbon, which is subsequently crushed against the
paper, in impact printers. The following are the characteristics of impact printers:
 Exceptionally low consumable cost.
 Quite noisy
 Because of its low cost, it is ideal for large-scale printing.
Character Printers
Character Printer has the capability to print only one character at a time. It is of two
types.
 Dot Matrix Printer
 Daisy Wheel
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

Line Printers
Line Printers are printers that have the capability to print one line at a time. It is of
two types.
 Drum Printer
 Chain Printer
Non-Impact Printers
Characters are printed without the need for a ribbon in non-impact printers.
Because these printers print a full page at a time, they’re also known as Page
Printers. The following are the characteristics of non-impact printers:
 Faster
 They don’t make a lot of noise.
 Excellent quality
 Supports a variety of typefaces and character sizes
Laser Printers
Laser Printers use laser lights for producing dots which will produce characters on
the page.
Inkjet Printers
Inkjet printers are printers that use spray technology for printing papers. High-
quality papers are produced in an Inkjet printer. They also do color printing.
iii. Speakers
Speakers are devices that produce sound after getting a command from a
computer. Nowadays, speakers come with wireless technology also like Bluetooth
speakers.

iv. Projector
Projectors are optical devices that have the work to show visuals on both types of
screens, stationary and moving both. It helps in displaying images on a big screen.
Projectors are generally used in theatres, auditoriums, etc.
v. Plotter
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

Plotter is a device that helps in making graphics or other images to give a real
view. A graphic card is mandatorily required to use these devices. These are the
pen-like devices that help in generating exact designs on the computer.
vi. Headphones
Headphones are just like a speaker, which is generally used by a single person or it
is a single-person usable device and is not commonly used in large areas. These
are also called headsets having a lower sound frequency.
vii. Facsimile
A facsimile is a fax machine that consists of a scanner and printer, where the
scanner works as an input device and the printer works as an output device.
b. Memory
It is used to store data/information and instructions. It is a data storage unit or a data storage
device where data is to be processed and instructions required for processing are stored.
In general, computer memory is of three types:
 Primary memory
 Secondary memory
 Cache memory
Primary Memory
It is also known as the main memory of the computer system. It is used to store data
and programs or instructions during computer operations. It uses semiconductor technology
and hence is commonly called semiconductor memory.
Primary memory is of two types:
 RAM (Random Access Memory): It is a volatile memory. Volatile memory stores information
based on the power supply. If the power supply fails/ interrupted/stopped, all the data and
information on this memory will be lost. RAM is of two types:
 SRAM (Static RAM): SRAM uses transistors and the circuits of this memory are
capable of retaining their state as long as the power is applied.
 DRAM (Dynamic RAM): DRAM uses capacitors and transistors and stores the data
as a charge on the capacitors. It needs refreshing of charge on capacitor after a
few milliseconds. This memory is slower than SRAM.
 ROM (Read Only Memory): It is a non-volatile memory. Non-volatile memory stores
information even when there is a power supply failed/ interrupted/stopped.
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

ROM is of four types:


 PROM (Programmable Read Only Memory): This read-only memory is modifiable
once by the user. Its content can’t be erased once written.
 EPROM (Erasable Programmable Read Only Memory): EPROM is an extension to
PROM where you can erase the content of ROM by exposing it to Ultraviolet rays
for nearly 40 minutes.
 EEPROM (Electrically Erasable Programmable Read Only Memory): Here the written
contents can be erased electrically. You can delete and reprogrammed EEPROM up
to 10,000 times.
Secondary Memory
It is also known as auxiliary memory and backup memory. It is a non-volatile memory and used
to store a large amount of data or information. The data or information stored in secondary
memory is permanent, and it is slower than primary memory.
Types of Secondary Memory
1. Magnetic Tapes: Magnetic tape is a long, narrow strip of plastic film with a thin, magnetic
coating on it that is used for magnetic recording.
2. Magnetic Disks: A magnetic disk is a circular metal or a plastic plate and these plates are
coated with magnetic material. The disc is used on both sides. Bits are stored in magnetized
surfaces in locations called tracks that run in concentric rings. Sectors are typically used to break
tracks into pieces.

Fig: Magnetic Disc


Hard discs are discs that are permanently attached and cannot be removed by a single user.
3. Optical Disks: It’s a laser-based storage medium that can be written to and read. It is
reasonably priced and has a long lifespan. The optical disc can be taken out of the computer by
occasional users.
Types of Optical Disks
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

CD – ROM
 It’s called compact disk.
 Information is written to the disc by using a controlled laser beam to burn pits on the disc
surface.
 It has a highly reflecting surface, which is usually aluminum.
WORM-(WRITE ONCE READ MANY)
 A user can only write data once.
 The information is written on the disc using a laser beam.
 It is possible to read the written data as many times as desired.
 They keep lasting records of information but access time is high.
 It is possible to rewrite updated or new data to another part of the disc.
 Data that has already been written cannot be changed.
DVDs
 The term “DVD” stands for “Digital Versatile/Video Disc,” and there are two sorts of DVDs:
 DVDR (writable)
 DVDRW (Re-Writable)
 DVD-ROMS (Digital Versatile Discs): These are read-only memory (ROM) discs that can be
used in a variety of ways. When compared to CD-ROMs, they can store a lot more data. It’s
an optical memory that can read and write data.
 DVD-R: DVD-R is a writable optical disc that can be used just once.
Cache Memory
It is a type of high-speed semiconductor memory that can help the CPU run faster. Between the
CPU and the main memory, it serves as a buffer.

c. Processor
The full form of CPU is Central Processing Unit. It is a brain of the computer. All types of data
processing operations and all the important functions of a computer are performed by the CPU.
Parts of CPU
Now, the CPU consists of 3 major units, which are:
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

1. Registers/Memory Unit
2. Control Unit
3. ALU(Arithmetic Logic Unit)

Fig: Parts of CPU


Registers
It is one of a small set of data holding places that are part of the computer processor.
Control Unit
The control unit controls the operations of all parts of the computer but it does not carry out any
data processing operations.
ALU (Arithmetic Logic Unit)
ALU (Arithmetic Logic Unit) is responsible for performing arithmetic and logical functions or
operations. It consists of two subsections, which are:
 Arithmetic Section
 Logic Section

2. Computer Software Components:


Operating System
An Operating System (OS) is a collection of software that manages computer hardware
resources and provides common services for computer programs. The operating system is the
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

most important type of system software in a computer system. Examples of Operating Systems
are Windows, Linux, Mac OS, etc.
Application Software
Programs created to carry out particular duties or functions are referred to as application
software. Word processors,
Device Drivers
Device drivers are software applications that connect and interact with hardware devices on
behalf of the operating system. They serve as translators.
Compiler
Compiler is a translator which is used to convert programs in high-level language to low-level
language. It translates the entire program and also reports the errors in source program
encountered during the translation.
Interpreter
Interpreter is a translator which is used to convert programs in high-level language to low-level
language. Interpreter translates line by line and reports the error once it encountered during the
translation process.
It directly executes the operations specified in the source program when the input is given by the
user.
It gives better error diagnostics than a compiler.
Differences between compiler and interpreter

SI. Compiler Interpreter


No
1 Performs the translation of a Performs statement by statement
program as a whole. translation.
2 Execution is faster. Execution is slower.
3 Requires more memory as linking is Memory usage is efficient as no
needed for the generated intermediate object code is
intermediate object code. generated.
4 Debugging is hard as the error It stops translation when the first
messages are generated after error is met. Hence, debugging is
scanning the entire program only. easy.
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

5 Programming languages like C, C++ Programming languages


uses compilers. like Python, BASIC, and Ruby uses
interpreters.

Firmware
Software that is integrated with hardware devices is known as firmware. During system startup, it
initializes hardware and offers low-level control.

UNIX: An Overview
UNIX is a powerful Operating System initially developed by Ken Thompson, Dennis Ritchie at
AT&T Bell laboratories in 1970. It is prevalent among scientific, engineering, and academic
institutions due to its most appreciative features like multitasking, flexibility, and many more. In
UNIX, the file system is a hierarchical structure of files and directories where users can store and
retrieve information using the files.

The structure of Unix OS Layers is as follows:

Hardware -
This layer of UNIX consists of all hardware-related information in the UNIX environment.
Kernel -
The core of the operating system that's liable for maintaining the full functionality is named the
kernel. The kernel of UNIX runs on the particular machine hardware and interacts with the
hardware effectively.
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

The Shell -
The Shell is an interpreter that interprets the command submitted by the user at the terminal, and
calls the program you simply want.
Types of Shell in UNIX System:
o Bourne Shell: This Shell is simply called the Shell. It was the first Shell for UNIX OS. It is
still the most widely available Shell on a UNIX system.
o C Shell: The C shell is another popular shell commonly available on a UNIX system. The
C shell was developed by the University of California at Berkeley and removed some of
the shortcomings of the Bourne shell.
o Korn Shell: This Shell was created by David Korn to address the Bourne Shell's user-
interaction issues and to deal with the shortcomings of the C shell's scripting quirks.
Application Programs Layer -
It is the outermost layer that executes the given external applications. UNIX distributions typically
come with several useful applications programs as standard. For Example: emacs editor, StarOffice,
xv image viewer, g++ compiler etc.

Working with Unix simple commands

o cal: This command is used to show the calendar.


o ls: It lists the contents of the directories.
o date: It is used to show the system time and date.
o banner: It is used to show a large banner on a standard output.
o who: This command is used to show the user's list logged in currently.
o whoami: This command is used to show the user id logged in currently.
o touch: It creates a new file or upgrades its timestamp.
o cat: This command is used to concatenate files and display them on stdout.
$ cat > pfile1
Hello SBU
^D
o copy: This command is used to copy files.
$ cp text1 text2
o mv: This command is used to rename files or move files.
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

$ mv text1 text2
o rm: This command is used to remove directories and files.
o mkdir: It creates a directory.
o cd: It changes the directory.
o pwd: It prints the current working directory.
o ps: It shows a snapshot of every current process.
o clear: It clears the terminal screen.

o man: It is an interface to work with online reference manuals.

Algorithm:
An algorithm is a process or set of rules which must be followed to complete a particular task.
This is basically the step-by-step procedure to complete any task.

Characteristics of Algorithm
1. An algorithm should be defined clearly.
2. An algorithm should produce at least one output.
3. An algorithm should have zero or more inputs.
4. An algorithm should be executed and finished in finite number of steps.
5. An algorithm should be basic and easy to perform.
6. Each step started with a specific indentation like, “Step-1”,
7. There must be “Start” as the first step and “End” as the last step of the algorithm.

Algorithm to find greatest between 3 numbers


Step 1: Start
Step 2: Take 3 numbers as input, say A, B, and C.
Step 3: Check if(A>B and A>C)
Step 4: Then A is greater
Step 5: Print A
Step 6: Else
Step 7: Check if(B>A and B>C)
Step 8: Then B is greater
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

Step 9: Print B
Step 10: Else C is greater
Step 11: Print C
Step 12: End

Pseudo code to find greatest between 3 numbers

BEGIN
NUMBER A, B, C
INPUT A
INPUT B
INPUT C
IF A>B
IF A>C THEN
OUTPUT “A is greatest”
ELSE
OUTPUT “C is greatest”
ENDIF
ELSE
IF B > C THEN
OUTPUT “B is greatest”
ELSE
OUTPUT “C is greatest”
ENDIF
ENDIF
END

Flowchart:
A flowchart is a picture of the separate steps of a process in sequential order.
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

Flowchart to find greatest between 3 numbers

//C Program to find greatest between three numbers


#include<stdio.h>
int main()
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

{
int A,B,C;
printf("Input A:");
scanf("%d",&A);
printf("Input B:");
scanf("%d",&B);
printf("Input C:");
scanf("%d",&C);
if(A>B)
{
if(A>C)
{
printf("A is greatest");
}
else
{
printf("C is greatest");
}

}
else
{
if(B>C)
{
printf("B is greatest");
}
else
{
printf("C is greatest");
}
}
}

Types of Errors in C
There are five different types of errors in C.
 Syntax Error
 Run Time Error
 Logical Error
 Semantic Error
 Linker Error

1. Syntax Error
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

Syntax errors occur when a programmer makes mistakes in typing the code's syntax correctly or
makes typos. Syntax errors are sometimes also called compilation errors because they are always
detected by the compiler. Generally, these errors can be easily identified and rectified by
programmers.
The most commonly occurring syntax errors in C language are:
Missing semi-colon (;)
Missing parenthesis ({})
Assigning value to a variable without declaring it
2. Runtime Error
Errors that occur during the execution (or running) of a program are called Runtime Errors. These
errors occur after the program has been compiled successfully.
Runtime errors can be a little tricky to identify because the compiler cannot detect these errors.
They can only be identified once the program is running. Some of the most common run time
errors are: number not divisible by zero, array index out of bounds, string index out of bounds,
etc.
3. Logical Error
Sometimes, we do not get the output we expected after the compilation and execution of a
program. Even though the code seems error free, the output generated is different from the
expected one. These types of errors are called Logical Errors.
4. Semantic Error
Errors that occur because the compiler is unable to understand the written code are called
Semantic Errors. A semantic error will be generated if the code makes no sense to the compiler,
even though it is syntactically correct.

Object code is the output of a compiler after it processes the source code. The object code is
usually a machine code, which can be understood directly by a specific type of CPU (central
processing unit).
Executable code (also called the Binary code) is the output of a linker after it processes the object
code.

Note:
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming

What is Volatile and Non Volatile memory?


Answer:
Volatile memory is used to store information based on power supply. If the power supply is off,
all the data and information on the memory will be lost. E.g. RAM.
Non-volatile memory is used to store information even when the power supply is off. E.g. ROM.

______eof_Module1________

You might also like