What are Storage Classes in C ?

Answer Posted / debasis roy

There are 4 storage classes in c
1. Auto
2. Static
3. Extern
4. Register

1. Auto :
A variable,defined normally without any storage specification is treated as automatic variable.
Example :

#include<stdio.h>
void main()
{
int num; //declaration of a variable num
num=7; //assign an integer to num
printf("%d",num);
}

output
7

2.Static :
A variable is declared as static when it needs to remember it's previous value.
To declare a variable as static an explicit mention of the word 'static' is necessary.
It can be used in recursion , a function that need to remember it's previous call's change etc.

Example:
#include<stdio.h>
void see_the_change(int);
void main()
{
see_the change(5);
see_the_change(60);
see_the_change(70);
}
void see_the_change(int vulejaoa)
{
static int capture;
cature=vulejaoa;
capture++;
printf("%d ",capture);
}

output :
6 7 8

3.Extern :
when a variable needs to be reused by different functions extern variables are used.
If we define something in a function and declare it externally then we can use it in that particular scope of funcion block.

Example :

#include<stdio.h>
int m;
void in_a();
void in_b();
void main()
{
extern int m;
m=10;
printf("%d",m);
in_a();
in_b();
}
void in_a()
extern int m;
m=19;
printf("%d",m);
}
void in_b()
{
extern int m;
m=52;
printf("%d",m);
}

4.Register :
Normally one should avoid this type as if your processor lacks more number of registers then system would have been slow down.
But if you have a processor having sufficient number of registers and you need to run it in less time, i.e without wasting any time, then you should go for a register variable.
As register variable declaration stores number stored in registers like A,B,C,D,E or H-L pair and eventually those are stored in cache memory, program can access it very fast. If all registers are used then the numbers will be stored in main memory.

Example :

#include<stdio.h>
void main()
{
register int m;
m=12;
printf("%d",m);
}

Is This Answer Correct ?    7 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is array a primitive data type in c?

560


Explain the difference between exit() and _exit() function?

605


Is it valid to address one element beyond the end of an array?

656


How can I remove the trailing spaces from a string?

591


a function gets called when the function name is followed by a a) semicolon (;) b) period(.) c) ! d) none of the above

837






What is pragma c?

584


In which header file is the null macro defined?

816


What is typeof in c?

579


how can I convert a string to a number?

569


A float occupies 4 bytes in memory. How many bits are used to store exponent part? since we can have up to 38 number for exponent so 2 ki power 6 6, 6 bits will be used. If 6 bits are used why do not we have up to 64 numbers in exponent?

1748


What is an lvalue?

603


How are variables declared in c?

573


Write a program to maintain student’s record. Record should not be available to any unauthorized user. There are three (3) categories of users. Each user has its own type. It depends upon user’s type that which kind of operations user can perform. Their types and options are mentioned below: 1. Admin (Search Record [by Reg. No or Name], View All Records, Insert New Record, Modify Existing Record) 2. Super Admin (Search Record [by Reg. No or Name], View All Records, Insert New Record, Modify Existing Record, Delete Single Record) 3. Guest (Search Record [by Reg. No or Name], View All Records) When first time program runs, it asks to create accounts. Each user type has only 1 account (which means that there can be maximum 3 accounts). In account creation, following options are required: Login Name: <6-10 alphabets long, should be unique> Password: <6-10 alphabets long, should not display characters when user type> Confirm Password: Account Type: Login Name, Password and Account Type should be stored in a separate file in encrypted form. (Encryption means that actual information should be changed and Decryption means that Encrypted information is changed back to the actual information) If any of the above mentioned requirement(s) does not meet then point out mistake and ask user to specify information again. When Program is launched with already created accounts, it will ask for user name and password to authenticate. On successful authentication, give options according to the user’s type.

1485


Explain what is wrong with this program statement?

593


Using which language Test cases are added in .ptu file of RTRT unit testing???

3538