0% found this document useful (0 votes)
27 views18 pages

1st Unit (1) PPS

The document provides an overview of digital computer systems, detailing components such as memory, CPU, I/O devices, and storage, alongside the functions of operating systems and types of operating systems. It also explains programming tools like assemblers, compilers, interpreters, loaders, and linkers, as well as the concept of algorithms with examples. The document serves as an educational resource on computer architecture and software development.

Uploaded by

ghazalialmiza7
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)
27 views18 pages

1st Unit (1) PPS

The document provides an overview of digital computer systems, detailing components such as memory, CPU, I/O devices, and storage, alongside the functions of operating systems and types of operating systems. It also explains programming tools like assemblers, compilers, interpreters, loaders, and linkers, as well as the concept of algorithms with examples. The document serves as an educational resource on computer architecture and software development.

Uploaded by

ghazalialmiza7
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/ 18

UNIT-1

1. Introduction to components of a Digital computer system: Digital Computers are mainly


general purpose computers that represent and store data in discrete quantities or numbers.
In these computers, all processing is done in terms of binary form. A digital computer is a
programmable machine which reads the binary instruction and processes the data which
are presented in binary form. If the User enters data in decimal or character form then it is
converted into binary form (0‘s and 1‘s). In other words we can say a digital computer that
works with numbers that are represented by the digits 0 and 1.

A digital computer is represented by the following block diagram:

Components of computer system are:

1.1 Memory: Memory is the best essential components of a computer because computer
can‘t perform the simple tasks. Computer memory is of two basic types – Primary and
Secondary memory. Random Access Memory (RAM) is volatile memory and Read Only
Memory (ROM) is non-volatile memory.

1.1(a) Random Access Memory (RAM) :


The primary memory is referred to as random access memory (RAM) due to the random
selection of memory locations. It performs both read and write operations on memory.
Primary memory select any part of memory when user want to save the data in memory
but that may not be store permanently on that location if computer is turn off. Primary
Memory also called as volatile memory because the memory can‘t store the data
permanently.
1.1(b) Read Only Memory (ROM) :
ROM is permanent memory location that offers huge types of standards to save data. But it
works with read only operation. No data lose happen whenever power supply failure
during the ROM memory work in computers.

Note:
Cache Memory: Cache memory is a small, fast memory unit located close to the CPU. It
stores frequently used data and instructions that have been recently accessed from the
main memory. Cache memory is designed to minimize the time it takes to access data by
providing the CPU with quick access to frequently used data.

Mr. Abhinav Gupta 1


1.2 Processor or Central Processing Unit(CPU):
CPU is used to perform the calculations and information processing on the data that is
entered through the input device.
It is divided into three parts---
1.2(a) Control Unit- Stores the instruction set which specifies the operations to be
performed by computer. It transfers the data and instructions to the ALU for an
arithmetic or logical operation. It controls communication and co-ordination between
input/output devices. It reads and tells instructions and determines the sequence for
processing the data.
1.2(b) Arithmetic Logical Unit (ALU)- ALU performs arithmetical or logical
operations on the data received. In this component, Arithmetic operations include addition,
subtraction and multiplication where Logical operations include AND, OR, or
EXCLUSIVE-OR.
1.2( c) Registers- Registers are the high-speed temporary storage locations in the
CPU made from electronic devices such as transistors, flip-flops, etc. So, registers can be
thought as CPU‘s working memory.

1.3 I/O (Input/ Output) Device:


1.3(a) Input Device
It is used for accepting the data on which the operations are to be performed.
e.g. keyboard, mouse, track ball, stylus, touch screen, an audio input unit etc.
1.3(b) Output Device
It is used for providing the output of a program that is obtained after performing the
operations specified in a program.
e.g. Monitor, printer, speaker, plotter (vector graphics) etc.

1.4 Storage: This component is used to store the data or the work done by you. These
storage devices communicate in both directions with CPU. You can send the data to these
devices or the CPU can receive the data as well. It is also known as auxiliary
storage, secondary storage and external storage which is used to store a large amount of
data at lesser cost than primary storage. Secondary storage does not lose the data when
there is no power supply. Secondary storage is also known as non-volatile (means
permanent).
Some of the storage devices are: Hard disk drive, Floppy Disk Drive, DVD/CD, External
USB drive, Pen-drives, Memory cards

2. Operating System: An operating is an interface that provides information to user with


computer hardware. The American Natural Standard Institute (ANSI) defines operating
system as—
―Operating system is the software which controls the execution of computer
programs and provides scheduling, debugging, input/output control, storage management,
data management and other related services for efficient functioning of computer system.‖

2.1(a) Purpose of operating System


Operating System is essential software, purpose of which is to activate the computer and
 perform internal management functions. and
 provide services.

Mr. Abhinav Gupta 2


Internal management functions are the functions that have to be essential performed to
make the computer work. Whereas, services are group of commands and effectiveness that
operating system provides to its users to have better control over computer.
Block diagram shows the basic structure of operating system in figure as----

2.2(b) Management functions of Operating System


Functions that an operating system performs are:

(1) Process Management Functions


Programs that are to be executed and their data, initially reside within computer‘s
memory. For executing them they are taken to the processor, arithmetic and logical
operations are performed within the processor then result of processing are transferred
back to the memory and processor is set free for performing other tasks. All these
processor related operations are called process management functions.

(2) Memory Management Functions


All the programs that are to be executed and the result that are to be printed, have to be
essentially present in memory. When any value comes to memory, it is contain in the some
free portion of memory. If there is no free space then swapping procedure is started.

(3) Information Management Functions


Permanent storage of information within computer is an essential requirement of any
computer. So, -data is written on the secondary memory devices. We have to ensure that
new data being written on the device does not overwrite previously written data and it is
easily retrievable in its original form.

(4) Device Management Functions


Keyboard, monitor, floppy disk, hard disk etc. are few devices that are part of almost all
the computers. In computer there functionality is controlled by hardware and software.
For example, when a command is given by keyboard then keyboard controller card
(hardware) converts it into computer readable form and operating system (software)
executes it.

2.3(c ) Types of Operating System


Here is an overview of the different types of operating systems.

(1) Real-time Operating System: A real time operating system is the one which services on-
line external processes having strict timing response. It is a multitasking operating system
that wants at executing real-time applications. The main object of real-time operating
system is rapid response and reliability. Windows CE, OS-9, Symbian and LynxOS are
some of the commonly known real-time operating systems.
e.g. satellite control system, weather forecasting, patient monitoring system.

Mr. Abhinav Gupta 3


(2) Multi-user Operating Systems: Computer operating systems of this type allow multiple
users to access a computer system simultaneously. Time-sharing systems can be classified
as multi-user systems as they enable a multiple user access to a computer through time
sharing.
e.g. Linux, UNIX and OpenVMS (Virtual Memory System) are examples of multi-user
operating systems.

(3) Single-user Operating Systems


Single user operating systems can be split into two types:

Single user, single tasking operating system only has to deal with one person at a time,
running one user application at a time.
An example of this kind of operating system would be found on an old mobile phone. There
can only be one user using the mobile and that person is only using one of its applications
at a time.

Single user, multi-tasking operating system would be found on a personal computer. The
operating system is designed mainly with a single user in mind, but it can deal with many
applications running at the same time.
For example, you might be writing an essay, while searching the internet and downloading
a video file.
e.g. Windows, Linux, Mac OS are examples of single-user operating systems.

(4) Distributed Operating System: An operating system that manages a group of


independent computers and makes them appear to be a single computer is known as a
distributed operating system. Distributed computations are carried out on more than one
machine. When computers in a group work in cooperation, they make a distributed system.
e.g. Amoeba, Plan9 and LOCUS (developed during the 1980s) are some examples of
distributed operating systems.
(5) Mobile Operating System: Mobile OS is definitely an important mention in the list of
operating system types. A mobile OS controls a mobile device and its design supports
wireless communication and mobile applications. It has built-in support for mobile
multimedia formats.
e.g. Blackberry OS, Google's Android and Apple's i-phone OS are some of the most known
names of mobile operating systems.

3. Assembler:
The software that converts assembly language programs to an object file or machine
language program is called Assembler. Each line in assembly language translates to a single
machine code instruction. It also allocates the area of main storage as well as indicates
invalid source language instructions. Assemblers are generally simpler to write than
compilers for high level languages, and have been available since the 1950s.

4. Compiler:
A compiler is a computer program (or set of programs) that translates source
program written in a programming language (such as C, C++) into another target language
(such as machine code, object code) that a CPU can execute. The most common reason for
wanting to transform source code is to create an executable program. The name
"compiler" is generally used for programs that translate source code from a high-level
programming language to a lower level language (e.g., assembly language or machine
code).

Mr. Abhinav Gupta 4


 If the compiled program can run on a computer whose CPU or operating system is
different from the one on which the compiler runs then compiler is known as
a cross-compiler.
 A program that translates from a low level language to a higher level one is a de-
compiler.

5. Interpreter:
An interpreter is a program which translates statements of a program into machine code.
It translates only one statement of the program at a time. It reads only one statement of
program, translates it and executes it. Then it reads the next statement of the program
again translates it and executes it. In this way it proceeds further till all the statements are
translated and executed. On the other hand, a compiler goes through the entire program
and then translates the entire program into machine codes. A compiler is 5 to 25 times
faster than an interpreter.
By the compiler, the machine codes are saved permanently for future reference. On the
other hand, the machine codes produced by interpreter are not saved. An interpreter is a
small program as compared to compiler. It occupies less memory space, so it can be used in
a smaller system which has limited memory space.

6. Loader:
Loader is a program that loads machine codes of a program into the system
memory. A loader is the part of an Operating System that is responsible for loading
programs. It is one of the important stages in the process of starting a program. Because it
loads programs into memory and prepares them for execution. Loading a program
involves reading the contents of executable file into memory. Once loading is complete, the
operating system starts the program by passing control to the loaded program code. All
operating systems that support program loading have loaders. In many operating systems
the loader is permanently resident in memory.

7. Linker:
Linking is the final stage of compilation. It takes one or more object files or libraries as
input and combines them to produce a single (usually executable) file. In high level
languages, some built in header files or libraries are stored. These libraries are predefined
and these contain basic functions which are important for executing the program. These
functions are linked to the libraries by a program called Linker. If linker does not find a
library of a function then it informs to compiler and then compiler generates an error. Not
built in libraries, it also links the user defined functions to the user defined libraries.
Usually a longer program is divided into smaller subprograms called modules. And these
modules must be combined to execute the program. The process of combining the modules
is done by the linker.

Mr. Abhinav Gupta 5


DIFFERNCE BETWEEN COMPILER AND INTERPRETER

Compiler Interpreter

In case of no error, the Compiler converts Execution of source statements one by one.
the whole source code to Machine Code.

The compiler saves the Machine Language The Interpreter does not save the Machine
in form of Machine Code on disks. Language.

Compiled codes run faster than Interpreted codes run slower than
Interpreter. Compiler.

Linking-Loading Model is the basic The Interpretation Model is the basic


working model of the Compiler. working model of the Interpreter.

The compiler generates an output in the The interpreter does not generate any
form of (.exe). output.

Any change in the source program after the Any change in the source program during
compilation requires recompiling the entire the translation does not require
code. retranslation of the entire code.

Errors are displayed in Compiler after Errors are displayed in every single line.
Compiling together at the current time.

It does not require source code for later It requires source code for later execution.
execution.

Object code is permanently saved for No object code is saved for future use.
future use.

C, C++, C#, etc are compiler-based. Python, Perl, MATLAB, etc are interpreter-
based.

Mr. Abhinav Gupta 6


8. Algorithm:
An algorithm is a complete, detailed and precise step-by-step process for solving problem.
Algorithm is necessary because they instruct the computer what specific steps it needs to
solve out a problem.

Characteristic of Algorithm:
1. Input: it may accept 0 or more inputs.
2. Output: It should provide at least 1 output.
3. Definiteness: Each instruction must be clear, well defined and precise. There should be no
ambiguity.
4. Finiteness: It should be a sequence of finite instructions.
5. Effectiveness: This means that operations must be simple and carried out in finite time.

Algo1. Write an algorithm for checking whether a number is odd or even.


Step1. Start
Step2. Read the number, say x
Step3. Divide the number by 2
Step4. If remainder is 1 then print x is odd
Step5. Else print x is even
Step6. Stop

Algo2. Write an algorithm to find the factorial of a given positive integer number.
Step1. Start
Step2. Read the number, say x
Step3. Set fact=1
Step4. Repeat step 5 and 6 until (x==1)
Step5. fact=fact * x
Step6. x=x-1
Step7. print the final value of fact
Step8. Stop

Algo3. Write an algorithm to find the largest 3 no‘s.


Step1. Start
Step2. Read the numbers, say a,b,c
Step3. If (a>b) and (a>c) then print ‗a is greater‘
Step4. Else if (b>a) and (b>c) then print ‗b is greater‘
Step5. Else print ‗c is greater‘
Step6. Stop

Algo4. Write an algorithm to print the Fibonacci series.


Step1. Start
Step2. Assign A=0, B=1, i=3,n
Step3. Print A=0 and B=1.
Step4 Repeat step 5 to 9 until (i<=n)
Step5. Sum=A+B
Step6. A=B
Step7. B=Sum
Step8. Print Sum
Step9. i=i+1
Step10. Stop

Mr. Abhinav Gupta 7


Algo5. Write an algorithm to find whether the given integer number is prime or not.
Step1. Start
Step2. Read the number, say x
Step3. Set i=2
Step4. Repeat step 5 and 6 until i<=(x/2)
Step5. If (x%i==0) then goto step 8
Step6. i=i+1
Step7. Print ‗x is prime‘ and goto step 9
Step8. Print ‗x is not prime‘
Step9. Stop
Algo6. Write an algorithm to find sum of prime numbers from 10 to 99.
Step1. Start
Step2. Set x=10, sum=0
Step3. Repeat step 4 to 9 til x<=99
Step4. Set i=2
Step5. Repeat step 6 and 7 until (i<=x/2)
Step6. If ( x % i ==0) then goto 9
Step7. Else i=i+1
Step8. Sum=sum+x
Step9. x=x+1
Step10.Print sum
Step11. Stop
9. Flowcharts:
Flowcharts are nothing but the graphical representation of the data or the algorithm for a
better understanding of the code visually. It displays step-by-step solutions to a problem,
algorithm, or process. It is a pictorial way of representing steps that are preferred by most
beginner-level programmers to understand algorithms, thus it contributes to solve the
issues in the algorithm. A flowchart is a picture of boxes that indicates the process flow
sequentially. To draw a flowchart, certain rules need to be followed which are followed by
all professionals to draw a flowchart and are widely accepted all over the countries.
Flow charts symbol:

1. Start and End: It Show the beginning or ending of an algorithm.

2. Input and Output statement:


A parallelogram is used to show any input/output such as reading
from a keyboard or writing on system console.

3. Condition checking statement:


F T
condition
A diamond box is used to show selection statement as--

False Action True Action

4. Flow lines:
It show the actions order in an algorithm.

5. Assignment statement: It show using a rectangle as-- Variable expression


e.g. c=a+b

Mr. Abhinav Gupta 8


Example1. Draw a flowchart to add two numbers entered by user.

Example2. Draw flowchart to find the largest among three different numbers entered by
user.

Example3. Draw a flowchart to find the Factorial of a positive number.

Mr. Abhinav Gupta 9


Example4. Draw a flowchart to find all the roots of a quadratic equation ax2+bx+c=0

Example5. Draw a flowchart to find the Fibonacci series till n term.

Try yourself………

10. Pseudocode:
It is a simple way of writing programming code in English. Pseudocode is not actual
programming language. It uses short statement to write code for programs before you
actually create it in a specific language. Once you know what the program is about and
how it will function, then you can use pseudocode to create statements to achieve the
required results for your program.
Examples:

1. Pseudo code to print result.

If student's grade is greater than or equal to 60


Print "passed"
else
Print "failed"
endif

Mr. Abhinav Gupta 10


2. Pseudo code to calculate factorial n

X=n
Y=1
while X !=1 do
Y=Y*X
X=X-1
return Y

3. Pseudocode to check whether number is odd or even

START
DISPLAY "Enter the Number - "
READ number
IF number MOD 2 == 0 THEN
DISPLAY "Number is Even"
ELSE
DISPLAY "Number is Odd"
END IF
STOP

11. Structure of C program:


The basic structure of a C program is divided into 6 parts which makes it easy to read,
modify, document, and understand in a particular format. C program must follow the
below-mentioned outline in order to successfully compile and execute. Debugging is easier
in a well-structured C program.
C is a programming language developed at AT & T Bell Laboratories of USA in 1972. It
was designed and written by DENNIS RITCHIE.
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are
mentioned below:
 Documentation
 Preprocessor Section
 Definition
 Global Declaration
 Main() Function
 Sub Programs
1. Documentation
This section consists of the description of the program or the name of the program. It is
specified at the start of the program in the form of comments. Documentation can be
represented as:
// description, name of the program, programmer name, date, time etc.
or
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program and this
will not interfere with the given code. Basically, it gives an overview to the reader of the
program.

Mr. Abhinav Gupta 11


2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the
program. A copy of these multiple files is inserted into our program before the process of
compilation.

Example:
#include<stdio.h>
#include<math.h>

3. Definition
Preprocessors are the programs that process our source code before the process of
compilation. There are multiple steps which are involved in the writing and execution of
the program. Preprocessor directives start with the ‗#‘ symbol. The #define preprocessor is
used to create a constant throughout the program. Whenever this name is encountered by
the compiler, it is replaced by the value of defined code.

Example:
#define row 3

4. Global Declaration
The global declaration section contains global variables, function declaration. Variables
and functions which are declared in this scope can be used anywhere in the program.

Example:
int num = 18;

5. Main() Function
Every C program must have a main function. The main() function of the program is
written in this section. Operations like declaration and execution are performed inside the
curly braces of the main program. The return type of the main() function can be int as well
as void too. void() main tells the compiler that the program will not return any value. The
int main() tells the compiler that the program will return an integer value.

Example:
void main()
or
int main()

6. Sub Programs
User-defined functions are called in this section of the program. The control of the
program is shifted to the called function whenever they are called from the main or outside
the main() function. These are specified as per the requirements of the programmer.

Example:
int sum(int x, int y)
{
return (x+y);
}

Mr. Abhinav Gupta 12


Structure of C Program with example
Example: Below C program to find the sum of 2 numbers:

/*
description: program to find sum. */

#include <stdio.h> // Preprocessor Section

#define X 20 // Definition

int sum(int y); // Global Declaration

void main(void) // Main() Function


{
int y = 55;
printf("Sum: %d", sum(y));
}

int sum(int y) // Subprogram


{
return y + X;
}

Output
Sum: 75

Characteristics of C:
 Structured programming language
 General purpose programming language
 No right formats
 High level language and also called middle level language
 Platform dependent

12. Writing and Executing the first C program:


To write the first c program, open the C console and write the following code:
#include <stdio.h>
int main(){
printf("Hello C Language");
return 0;
}
 #include <stdio.h> includes the standard input output library functions. The printf()
function is defined in stdio.h .
 int main() The main() function is the entry point of every program in c language.
 printf() The printf() function is used to print data on the console.
 return 0 The return 0 statement, returns execution status to the OS. The 0 value is
used for successful execution and 1 for unsuccessful execution.

Mr. Abhinav Gupta 13


How to compile and run the c program
There are 2 ways to compile and run the c program, by menu and by shortcut.
 By menu
Now click on the compile menu then compile sub menu to compile the c program.
Then click on the run menu then run sub menu to run the c program.
 By shortcut
press ctrl+f9 keys compile and run the program directly.

You will see the following output on user screen.

13. Errors in C
13.1 Syntax errors –
Errors that occur when you break the rules of writing C syntax are known as syntax
errors. This compiler error indicates something that must be fixed before the code can be
compiled. All these errors are detected by compiler and thus are known as
compile-time errors.

Most frequent syntax errors are:


 Missing Parenthesis (})
 Printing the value of variable without declaring it
 Missing semicolon

13.2 Logical errors –


On compilation and execution of a program, desired output is not obtained when certain
input values are given. These types of 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 only depend on the logical thinking of the programmer and are easy to detect
if we follow the line of execution and determine why the program takes that path of
execution.

// logical error : a semicolon after loop


for(i = 0; i < 3; i++);

Mr. Abhinav Gupta 14


13.3 Linker Errors:
These errors occurs when after compilation we link the different object files with main‘s
object using RUN the program. These are errors generated when the executable of the
program cannot be generated. This may be due to wrong function prototyping, incorrect
header files. One of the most common linker error is writing Main() instead of main().
// linker error
#include<stdio.h>
void Main() // Here Main() should be main()
{
int a = 10;
printf("%d", a);
}

14. Object and Executable Code:


Source code is the C program that you write in your editor and save with a ‗ .C ‗ extension.
Which is un-compiled (when written for the first time or whenever a change is made in it
and saved)?

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

Executable code 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.

15. Components of C Language:


You have seen the basic structure of a C program, so it will be easy to understand other
basic building blocks of the C programming language.
Tokens in C
A C program consists of various tokens and a token is either a keyword, an identifier, a
constant, a string literal, or a symbol.
For example, the following C statement consists of five tokens −

printf("Hello, World! \n");

The individual tokens are −


printf
(
"Hello, World! \n"
)
;

Semicolons
In a C program, the semicolon is a statement terminator. That is, each individual statement
must be ended with a semicolon. It indicates the end of one logical entity.

Given below are two different statements −


printf("Hello, World! \n");
return 0;

Mr. Abhinav Gupta 15


Comments
Comments are like helping text in your C program and they are ignored by the compiler.
They start with /* and terminate with the characters */ as shown below −
/* my first program in C */

Identifiers
A C identifier is a name used to identify a variable, function, or any other user-defined
item. An identifier starts with a letter A to Z, a to z, or an underscore ‗_‘ followed by zero
or more letters, underscores, and digits (0 to 9).

C does not allow punctuation characters such as @, $, and % within identifiers. C is a case-
sensitive programming language.

Thus, Manpower and manpower are two different identifiers in C. –

Keywords
The following list shows the reserved words in C. These reserved words may not be used as
constants or variables or any other identifier names.
e.g.- int, char, float, switch, break, union, void, auto, while, double, do, case etc.

Whitespace in C
A line containing only whitespace, possibly with a comment, is known as a blank line, and a
C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline characters and
comments.
Therefore, in the following statement −
int age;
there must be at least one whitespace character (usually a space) between int and age for
the compiler to be able to distinguish them.

16. Standard input/output in C:


The statement #include<stdio.h> consists the value of standard input-output which
contains stdin, stdout and stderr.

C language provides the functions getc(), getchar(), putc(), putchar(), scanf(), printf() for
character input- output and gets(), puts() for string based input-output. The scanf() and
printf() functions accepts ―a format specifier string‖ and ―a list of variables‖. The format
specifier string is a character string which specifies the data type of each variable to be
input or output.
In C, keyboard is considered as standard input device and monitor is standard output
device.

Mr. Abhinav Gupta 16


17. Fundamental/ Primitive data type:

TYPE KEYWORDS FORMAT RANGE SIZE(in byte)


SPECIFIER
Boolean bool %zu 0-1 1bit or 1 byte
Character char %c 0 to 255 1 byte or 8 bits
Integer int %d -32768 to +32767 2 bytes
Short short %hd -32768 to +32767 2 bytes
Long long %ld -2147483648 4 bytes
to+2147483647
Unsigned unsigned int %u 0 to 65535 2 bytes
Single Floating float %f -3.4 e 38 to +3.4 e 38 4 bytes
Point
Double double %lf -1.7 e 308 to +1.7 e 8 bytes
Floating Point 308
Note: void is null type data type.

Assigning the Value:


Data_Type Variable_Name=constant;
e.g. int a=10;
int rate,quantity,amount=100;

18. Storage classes:

To fully define a variable we need-

a) The type of variable (i.e. int, char, float atc.)


b) The storage class of a variable
Storage classes provides the complete information about default value of variable, where it
would get stored, what will the life of variable and what will be the scope of variable.

There are 4 types of Storage classes—

TYPES OF KEYWORD DEFAULT STORAGE SCOPE OF LIFE OF DECLARATION


STORAGE VALUE VARIABLE VARIABLE
CLASS
AUTOMATIC auto Garbage Memory Local to block Till control auto int k;
remains in
the block in
which
it is declared
REGISTER register Garbage Register of Local to block ― register int r;
CPU
STATIC static 0 Memory Local to block Till program static int c;
is run and is
valid in
various
function calls
EXTERNAL extern 0 Memory Global from point Till program extern int a;
of declaration execution
does not
come to an
end

Mr. Abhinav Gupta 17


NOTE: C Compiler automatically converts the storage class from register to automatic
because no. of CPU registers is limited (e.g. microcomputer has only 16 registers.)

Example of Automatic:
#include<stdio.h>
void main()
{
auto int i;
printf(―%d‖,i);
}
O/P: -28762 (garbage value)

Example of Register:
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr(); Output: Hello
register int k; Hello
for(k=0;k<5;k++) Hello
printf(―\n Hello‖); Hello
getch(); Hello
}

Example of static:
#include<stdio.h>
void main()
{ clrscr(); Output: 1
static int count;
count=count+1; // default value of count =0
printf(― %d‖, count);
}
Example of extern:
#include<stdio.h>
increment();
decrement();
extern int i=5;
void main()
{
printf(―%d\t‖,i); // i=5
increment ();
decrement ();
printf(―%d‖,i);
}
increment ()
{ i=i+1;
printf(―%d\t‖,i); // i=6
}
decrement ()
{ i=i-1;
printf(―%d\t‖,i); // i=5 Output: 5 6
5 5
}

Mr. Abhinav Gupta 18

You might also like