Unit 1 PPS
Unit 1 PPS
Computer:
C : Common
O : Operating
M : Machine
P : Particularly
U : Used for
T : Trade
E : Education
R : Research
Characteristics of Computer:
1. Speed: - computer is a very fast machine. It can perform any task in few seconds.
2. Accuracy: - Computers are highly accurate machine and they never make mistakes. Errors can
occur in a
Computer, but these are mainly due to human rather than technological weaknesses.
3. Reliability: - computers can be constructed and programmed in such a manner that they would
always yield
guaranteed results.
4. Power of remembering: - A computer can store and recall any amount of information because of
its secondary storage (a type of detachable memory) capability. It saves the effort of inputs every
time, which is useful for many computations in computer. The facility of the storage of data is on
the temporary basis as well a long term basis.
5. Versatility:- Computer is useful everywhere in different respects such as in business, for personal
use at home, in schools for educational purpose, in scientific and engineering application and so on.
Computer can be used for generic as well as specialized purpose.
6. No feeling: - computers are devoid of emotions. They have no feelings because they are machine.
1
C Programming – UNIT - I
Storage Units
The storage units of a digital computer hold data and instructions that have been entered through the
input units before and whilst they are being processed. They preserve the intermediate and final results
before they are sent to the output devices. They also save the data for the later use. The various storage
devices of a computer system are divided into two categories:
2. Secondary Storage.
->It stores several programs, documents, data bases etc. The programs that you run on the
computer are first transferred to the primary memory before it is actually run. Whenever the
results are saved, again they get stored in the secondary memory. The secondary memory is
slower and cheaper than the primary memory.
->Speed of this memory is slow. But we can use a vest amount of secondary memory to story long
size data. ->Cost of Secondary memory is low.
->Some of the commonly used secondary memory devices are Hard disk, CD, DVD, etc.
Input units
Digital computers need to receive data and instruction in order to solve any problem so they need to input
data and instructions. Input can be entered from a keyboard, a mouse pointing device, a USB stick and
the various types of photo storage cards used by digital cameras. Input can also be downloaded from the
2
C Programming – UNIT - I
Output Units
The output units of a computer provide the information and results of an arithmetical computation - or
some other kind of data processing, such as a search - to the outside world. Commonly used output units
are printers and visual display units (VDUs), also known simply as "screens". Output data can also be sent
to USB sticks and other types of data storage cards, such as those used by digital cameras. Output can
also be uploaded to the Internet via a communications device.
All output peripheral devices perform the following functions:
3
C Programming – UNIT - I
TYPES OF COMPUTERS
● Analog Computers
● Digital Computers
● Hybrid Computers
Analog Computer :
Hybrid Computers :
4
C Programming – UNIT - I
● For example, in hospital’s ICU, analog devices might measure the patients temperature, blood
pressure and other vital signs.
Digital Computer
● Super Computers
● Mainframe Computers
● Mini Computers
● Micro Computers
Super Computers :
● They are the best in terms of processing capacity and also the most expensive ones.
● These computers can process billions of instructions per second.
● Normally, they will be used for applications which require intensive numerical computations
such as stock analysis, weather forecasting etc.
● Other uses of supercomputers are scientific simulations, (animated) graphics, fluid dynamic
calculations, nuclear energy research, electronic design etc.
● Perhaps the best known super computer manufacturer is Cray Research.
● Some of the "traditional" companies which produce super computers are Cray, IBM and HP.
Mainframe Computers :
● Mainframe computers can also process data at very high speeds vi.e., hundreds of million
instructions per second and they are also quite expensive.
● Most of the mainframe computers have capacities to host multiple operating systems and
operate as a number of virtual machines.
● Large organizations use mainframes for highly critical applications such as bulk data processing
and ERP.
● Normally, they are used in banking, airlines and railways etc for their applications.
● They can substitute for several small servers
Mini Computers :
● Mini computers are lower to mainframe computers in terms of speed and storage capacity.
● They are also less expensive than mainframe computers.
5
C Programming – UNIT - I
● Some of the features of mainframes will not be available in mini computers. Hence, their
performance also will be less than that of mainframes.
Micro Computers :
● A computer with a microprocessor and its central processing unit is known as a microcomputer.
● They do not occupy space as much as mainframes do.
● When supplemented with a keyboard and a mouse, microcomputers can be called personal
computers.
● A monitor, a keyboard and other similar input-output devices, computer memory in the form of
RAM and a power supply unit come packaged in a microcomputer.
● These computers can fit on desks or tables and prove to be the best choice for single-user tasks.
● They are further classified into
⮚ Desktop computers
⮚ Laptop Computers
Primary Storage:
1. Access Time: This is the time required to locate and retrieve stored data from the storage unit in
response to a program instruction. A fast access time is preferred.
2. Storage capacity: It is the amount of data that can be stored in the storage unit. A large capacity is
desired.
3. Cost per bit of storage: Main goal is to minimize the cost of per bit storage.
1. RAM [Link]
1. RAM:
● Stands for Random access Memory.
● It stores temporary data into memory.
● When the power supply is switched off, the information stored inside the RAM is Lost.
● It stores essential data required to run programs, such as information and calculation.
● It is also referred to as read/write memory because information can be read from a RAM
chip and also be written into it.
● There are two type of RAM
1. S-RAM (Static RAM)
● Made by flip-flops , an electronic device ,which stores a bit as a voltage.
● Stores temporary data.
● Costly-cost of per bit storage is high
● High speed
6
C Programming – UNIT - I
2. ROM:
● Stands for Red only memory
● typically used in computers to permanently hold data
● The information from the memory can only be read and it is not possible to write fresh
information into it
● It is supplied by the computer manufacturer and user can not modify the program
stored inside the ROM.
● When the power supply is switched off, the information stored inside the RAM is Lost.
● Read Only Memory is constructed from "hard-wire logic," in a similar way to the
processor, meaning that it cannot be reprogrammed or changed. This is because it is
designed to perform a specific function, and does not need to be altered.
● Example of ROM is a commercial CD purchased from a store; the manufacturers do not
want you to alter what is stored on the disk.
● Type of ROM
1. Programmable Read only memory (PROM): User can write programs at one
time and read it many times. Once the chip has been programmed the recorded
information cannot be changed.
Cache Memory
● Cache (pronounced cash) memory is extremely fast memory that is built into a computer’s central
processing unit (CPU), or located next to it on a separate chip.
● The CPU uses cache memory to store instructions that are repeatedly required to run programs,
improving overall system speed. As the microprocessor processes data, it looks first in
the cache memory.
Registers
● Register is a special- purpose [Link] uses these registers to handle the process of execution
effectively and efficiently.
● They are a part of the central processing unit but cannot be considered as a part of main memory.
● They can hold only one piece of data at a time.
● Registers receive the information, hold it temporarily and pass it to other as directed by the control
unit.
● The number of registers varies from computer to computer, each one designed to perform a
specific function. Registers have a specific storage capacity normally 14 registers are present in a
microcomputer. The most commonly used registers are:
1. Accumulator: stores the result of the last processing step of the ALU.
2. Instruction register: holds the instruction while it is being executed.
3. Address register: holds the location of the next piece of data.
7
C Programming – UNIT - I
Secondary Storage :
● Secondary memory (or secondary storage) is the slowest and cheapest form of memory. It cannot
be processed directly by the CPU. It must first be copied into primary storage (also known
as RAM ).
● Secondary memory devices include magnetic disks like hard drives and floppy disks ; optical
disks such as CDs and CDROMs ; and magnetic tapes, which were the first forms of secondary
memory.
● Hardware
The physical component of a computer is called hardware. Thus the input, storage,
processing, and output devices are hardware. Ex:- monitor, keyboard, mouse etc..
● Software
Computer cannot do anything on its own. It must be instructed to do the job. Hence it is
necessary to specify a sequence of instructions that a computer must perform to solve the
problem.
A sequence of instruction given to a computer to perform a particular task is called a
program. And set of programs written for a computer is called software.
8
C Programming – UNIT - I
Type of software:
1. Application Software
2. System Software
1. Application Software
Application software is a set of one or more programs designed to carry out operations for
specific applications.
Or we can say Applications software includes programs that do real work for users.
For example, word processors, spreadsheets, and database management systems.
2. System Software
System software is a set of one or more programs designed to control the operation of a
computer system.
Systems software includes the operating system and all the utilities that enable the
computer to function. For example Operating System, compiler, assembler, utilities etc
❖ Operating System:
● An Operating System (O.S.) is a set of programs that is used to manage the various resource and
overall operations of a computer installation.
● Its prime objective is to improve the performance and efficiency of a computer system and
increase facility.
● OS is responsible for the smooth and efficient operation of the entire computer system.
● It makes the computer system user friendly.
● It is considered the backbone of a computer, managing both software and hardware resources.
● An O.S. provides an environment for the execution of the program.
● It works as a layer between hardware and application program.
User
Application Program
Operating System
Hardware
1. Process Management :
2. Memory Management :
3. Input / Output Management:
4. File Management :
5. Network Management :
6. Secondary storage management
9
C Programming – UNIT - I
O.s. provides services for the convenience of the programmer, to make the programming task
easier. Some of these services are.
1. Program execution services
2. I/O operation services
3. File System services
4. Communication services
5. Error Detection services
6. Protection services
7. Accounting Services
● Multiprocessing OS refers to a computer system’s ability to support and utilizing more than
one computer processor at a time.
● Multiprocessing is used to describe interconnected computers with two or more independent
CPUs that have the ability to simultaneously execute several programs.
● A multiprocessing operating system allows a program to run on more than one central
processing unit (CPU) at a time
10
C Programming – UNIT - I
● An operating system that is capable of allowing multiple software processes to run at the
same time.
● Multitasking is the ability to execute more than one task at the same time.
● In Multitasking only one CPU is involved, but it switches from one program to another so
quickly that it gives the appearance of executing all the programs at the same time.
● Ex: Linux , Windows 2000
● There are many applications that require an immediate response from the computer.
● Real time means immediate response from the computer.
● Real-time operating systems are designed to allow computers to process and respond to
input immediately.
● Usually, general-purpose operating systems, such as disk operating system (DOS), are not
considered real time, as they may require seconds or minutes to respond to input.
● Real-time operating systems are typically used when computers must react to the
consistent input of information without delay.
● Used in stock market quotation, searching a criminal data file for a possible suspect may all
be actions that need to be done without delay.
7. Distributed System:
● A recent trend in computer system is to distribute computation among several processors.
In contrast to tightly coupled system the processors do not share memory or a clock.
Instead each processor has its own memory and clock.
● The processors communicate with one another through various communication lines such as
high speed buses or telephone lines.
● These systems are usually referred to as distributed system or loosely coupled system.
● The processors in a distributed system may vary in size and function.
● They may include small micro processors, work stations, mini computers and large general
purpose computer systems.
● These processors are referred to by a number of different names, such as sites, nodes,
computers, and so on depending on the context in which they mentioned.
● There are variety of reasons for building distributed system, the major ones being:
o Resource Sharing.
o Computation Speedup.
o Reliability and
o Communication.
❖ Flow Chart:
11
C Programming – UNIT - I
• Different flow chart symbols have different meanings. The most common flow chart symbols
are:
• Terminator: An oval flow chart shape indicating the start or end of the process.
• Process: A rectangular flow chart shape indicating a normal process flow step.
• Decision: A diamond flow chart shape indication a branch in the process flow.
• Connector: A small, labeled, circular flow chart shape used to indicate a jump in the process
flow. (Shown as the circle with the letter “A”, below.)
• Data: A parallelogram that indicates data input or output (I/O) for a process.
• Document: Used to indicate a document or report (see image in sample flow chart below).
❖ Algorithm:
● Algorithm may be formally defined as a sequence of instructions designed in such a way that if the
instructions are executed in the specified sequence, the desired result will be obtained.
● An algorithm is a list of steps for solving a problem.
● It is a sequence of finite instructions, used for calculation and data processing.
● It takes some value or set of values as input and produces some values as output.
● It specifies how to compute the results for any given set of arguments.
Characteristics of an algorithm
❖ Header Files
● These are the files that are included at the top of any program.
● If we use any function inside a program, then the header file containing declaration or definition of
that function. Like printf() is defined in stdio.h. So, we must include it (by #include in order to use
printf ()).
● You request the use of a header file in your program by including it, with the C preprocessing
directive `#include'.
● The header file is included (copy/pasted) during the preprocessing stage and is compiled as part of
the program being written during compilation phase.
12
C Programming – UNIT - I
Including a header file produces the same results as copying the header file into each source file that
needs it. Such copying would be time-consuming and error-prone. With a header file, the related
declarations appear in only one place. If they need to be changed, they can be changed in one place, and
programs that include the header file will automatically use the new version when next recompiled. The
header file eliminates the labor of finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
Library Files
● These are the files which contains the definition (how it will work) of the functions which you are
using in your program.
● Compiler uses these files in order to define the functions which have been used in the program and
had been declared inside the header file. Like, printf() has its complete definition ,like how it will
work etc. in an I/O library
● The compiler uses that library to get the machine code for the functions like printf().
1. Header files are TEXT files while library files are BINARY. This means, we can read and modify the
header file but not the library!
2. Header file is in C language while the library is in machine language!
3. Header file has to be included by the programmer while the compiler automatically relates the library
file(s) with the program!
13
C Programming – UNIT - I
1. Documentation Section : This section consists of comment lines which include the name of
programmer, the author and other details like time and date of writing the program.
Documentation section helps anyone to get an overview of the program.
2. Link Section : The link section consists of the header files of the functions that are used in the
program. It provides instructions to the compiler to link functions from the system library.
3. Definition Section : All the symbolic constants are written in definition section. Macros are
known as symbolic constants.
4. Global Declaration Section : The global variables that can be used anywhere in the program
are declared in global declaration section. This section also declares the user defined functions.
5. main() Function Section : It is necessary to have one main () function section in every C
program. This section contains two parts, declaration and executable part. The declaration part
declares all the variables that are used in executable part. These two parts must be written in
between the opening and closing braces { }. Each statement in the declaration and executable
part must end with a semicolon (;). The execution of program starts at opening braces and
ends at closing braces.
6. Subprogram Section :The subprogram section contains all the user defined functions that are
used to perform a specific task. These user defined functions are called in the main() function.
14
C Programming – UNIT - I
● Errors are the problems or the faults that occur in the program, which makes the behavior of
the program abnormal
● These errors are detected either during the time of compilation or execution
● Thus the errors should be removed before compiling and executing.
● Programming errors are also known as the bugs or faults, and the process of removing these
bugs is known as debugging.
Syntax errors:
● Syntax errors are also known as the compilation errors as they occurred at the compilation
time
● Syntax errors occur when violate the rules of writing C syntax
● Syntax errors must be fixed before the compilation of the code.
● Most frequent syntax errors are:
• Missing Parenthesis (})
• Missing semicolon
● Example
#include<stdio.h>
void main()
{
int x = 10 // semicolon missed
printf("%d", x);
}
Logical Errors:
● The errors which provide incorrect output but appear to be error free are called logical errors.
● These are one of the most common errors done by beginners of programming.
● These errors solely depend on the logical thinking of the programmer and are easy to detect
if we follow the line of execution step by step.
15
C Programming – UNIT - I
#include<stdio.h>
void main()
{
int x = 10,y=20,z;
z=x*y; //by mistake using * in place of +(Logical error)
printf("%d", x);
}
Object Code-
The computer does not understand the source program or the source code. Therefore, the compiler
converts the source program into an object program. It contains the instructions for the machine to
perform. These instructions are in the form of binary digits. The machine understands this object
code. Therefore, the computer can execute this code. Generally, the object code is specific to the
system architecture. Extension of object code is .obj.
Executable code-
Executable code is a file or a program that indicates tasks according to encoded instructions.
Executable code (also called the Binary) is the output of a linker after it processes the object code.
A machine code file can be immediately executable (i.e., runnable as a program), or it might require
linking with other object code files (e.g. libraries) to produce a complete executable program.
Extension of executable code is .exe.
Sometimes possible to consider a bytecode or scripting language instructions as an
executable code.
❖ components of C language
● Any alphabet, digit or special symbol, used to represent information is denoted by character.
● The characters in C are grouped into four categories:-
16
C Programming – UNIT - I
● Most versions of the language also allow certain other characters, such as @ and $,
to be included within strings and comments.
● C uses certain combinations of these characters, such as \b,\n and \t, to represent
special conditions such as backspace, new line, and horizontal tab, respectively.
● These character combinations are known as escape sequences.
● A data type, in programming, is a classification that specifies which type of value a variable
has and what type of mathematical, relational or logical operations can be applied to it
● We can use several basic types of data values such as character, integer, float, double etc.
3. Variable / identifier :
● Variable or identifier is a name of a memory area which is used to store the value
● A variable is an entity whose value can change during program execution.
● Variable names are names given to locations in memory.
● These locations can contain integer, real or character constants.
4. Constants:
5. Keyword :
● Keywords are the words whose meaning has already been explained to the C compiler
● The keywords cannot be used as variable names because if we do so we are trying to assign
a new meaning to the keyword, which is not allowed by the computer.
● There are 32 keywords available in C.
● Rules to be followed for all programs written in C:
o All keywords are in lower case.
o C is case sensitive so int is different from Int.
17
C Programming – UNIT - I
What is Language :
• In the same way when a user wants to communicate with a computer ,the language that he/she uses for this
purpose is known as programming language .
• Like BASIC, COBOL, C, C++, JAVA ……
• At the same time we know that computer understands only one language that is MACHINE LANGUAGE which is
in 0 & 1 format.
• It is very difficult for us to learn this language.
• For Example two persons belonging to two different countries , want to communicate then there may be a
problem of common communication medium.
• To solve this problem there is a requirement of translator.
Machine Language:
• It is in 0 and 1 format
• No need to translate.
• Programs are less understandable
• Difficult to write large programs
• Debugging is most difficult.
• Programs are machine dependent
18
C Programming – UNIT - I
Assembly Language:
Types of Translator:
1. Assembler
2. Complier
3. Interpreter
Assembler:
Compiler:
Interpreter:
19
C Programming – UNIT - I
• Interpreter scans one line at a time of source program if there is any syntax error , the execution of program
terminates immediately.
• It takes more time to execute.
• Machine code produced by interpreter is not saved in any file . So we need to interpret the file each time(BASIC
is an interpreter based language)
Loader:
● Loader is a operating system utility that copies program from a storage device to main memory, where they
can be executed.
● It is a part of O.S. and used to load the program.
● Most loaders are transparent i.e. you can not directly execute them,but the O.S. use them when necessary.
Linker:
Generation of Language
Introduction of ‘C’:
● C was developed by Dennis Ritchie at AT & T bell laboratory of USA in 1972.
20
C Programming – UNIT - I
● C is the result of a development process that started with an older language called BCPL. BCPL was developed by
Martin Richards, and it influenced a language called B, which was invented by Ken Thompson. B led to the
development of C in the 1970s.
● It is a case sensitive Language.
● Extension of ‘C’ program file is “.C” .
● It is also called a Middle Level Language.
1. ‘C’ KEYWORDS:
● Keywords are the words whose meaning has already been explained to the C compiler
● The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to
the keyword, which is not allowed by the computer.
● There are 32 keywords available in C.
● Rules to be followed for all programs written in C:
o All keywords are in lower case.
o C is case sensitive so int is different from Int.
o The keywords cannot be used as variable names because if we do so we are trying to assign a new
meaning to the keyword, which is not allowed by the computer.
● List of C programming keywords.
21
C Programming – UNIT - I
2. IDENTIFIERS :
● Identifiers refer to the naming of programming elements like variables, functions and arrays.
● These are user-defined names and consist of sequence of letters and digits, with a letter as a first character.
● Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used.
● The underscore character is also permitted in identifiers. It is usually used as a link between two words in
long identifiers.
● Some legal identifier :
arena, s_count
marks40
class_one
● Some illegal identifiers:
o 1stsst
o oh!god
o start….end
● The number of characters in the variable that are recognized differs from compiler to compiler.
● An identifier cannot be the same as a C keyword.
3. VARIABLE :
● Variables are named locations in memory that are used to hold a value that may be modified by the
program.
● Unlike constants that remain unchanged during the execution of a program.
● A variable may take different values at different times during execution.
● The syntax for declaring a variable is –
DataType IdentifierName ;
Example. int num;
long int sum , a;
4. CONSTANT :
22
C Programming – UNIT - I
● Constants are named locations in memory that are used to hold a value that cannot be modified by the
program.
● Constants remain unchanged during the execution of a program.
● const keyword is used to declare constant data.
● The syntax for declaring a constant is –
const DataType IdentifierName ;
Example. const float PI = 3.14f;
● Types of C Constants
C constants can be divided into two major
categories:
o Primary Constants
o Secondary Constants
5. OPERATORS :
1. Unary operator(+,-,++,--)
2. Binary operator
a. Arithmetic operators (+,-,*,/,%)
b. Relational operators (<,>,<=,>=,==,!=)
c. Logical operators(&&(logical AND),||(logical OR),!(logical Not))
d. Assignment operators (=)
e. Bitwise operators (~,>>,<<,^,&,|)
• C supports bitwise operators for manipulation of data at bit level.
• Bitwise operators may not be applied to float or double.
23
C Programming – UNIT - I
3. Ternary operator
for example:
void main()
{
int a=11, b=20;
( a==b) ? printf(“both r Equal”) : printf(“Not Equal”) ;
}
6. SPECIAL OPERATOR :
● ! , @, #, $ , & , * , …..
● These all symbols are called Special Symbols.
● Every symbol has its special meaning in different respect at different place that’s why it is called Special
Symbols.
Data Type:
The C language supports a number of data types, all of which are necessary in writing
24
C Programming – UNIT - I
programs. The basic data types of C language are defined by the ANSI standard are listed in diagram.
In addition to these data types, some of them may be used with a modifier that affects
the characteristics of the data object. These modifiers are listed in Table.
Modifier Description
long Forces a type int to be 4 bytes (32 bits) long and forces a type double to be larger than a double
(but the actual size is implementation defined). Cannot be used with short.
short Forces a type int to be 2 bytes (16 bits) long. Cannot be used with long.
unsigned Causes the compiler (and CPU) to treat the number as containing only positive values. Because a
16-bit signed integer can hold values between –32,768 and 32,767, an unsigned integer can hold
values between 0 and 65,535. The unsigned modifier can be used with char, long, and short
(integer) types.
The essence of all the data types that we have learnt so far has been captured in Table.
25
C Programming – UNIT - I
❖ Storage Class:
To fully define a variable we need to mention :
1. Type of variable i.e ( int, char, float )
2. The storage class of a variable
The storage class is associated with a variable which decide what would be the default value of variable, where it would get
stored, what will be the life of variable, and what will be the scope of variable.
Moreover, a variable’s storage class tells us:
● Where the variable would be stored.
● What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the default initial
value).
● What is the visibility of the variable; i.e. in which functions the value of the variable would be available.
● What is the scope or life of the variable; i.e. how long would the variable exist.
Keyword : auto
Default value : garbage
Storage : memory
Scope of variable : Local to block
Life of variable : till control remains in the block in which it is declared.
Ex: auto int x;
Ex:
26
C Programming – UNIT - I
Void main()
{
register int r;
for(r=1;r<=10;r++)
Printf(“%d”,r);
}
Output:1,2,3,4,5,6,7,8,9,10
Note : If any CPU register is not available to store data then ‘c’ compiler automatically convert the storage
class from register to automatic, because number of CPU registers are limited..
Ex:
void main()
{
static int x;
printf(“%d”,x);
x++;
if(x<=10)
main();
27
C Programming – UNIT - I
}
Output:0,1,2,3,4,5,6,7,8,9,10
28