0% found this document useful (0 votes)
13 views5 pages

Storage-Classes-in-C

Uploaded by

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

Storage-Classes-in-C

Uploaded by

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

Storage Classes in C

Storage classes in C are used to determine the lifetime, visibility, memory location, and initial
value of a variable. There are four types of storage classes in C 1.Automatic 2.External 3.Static
4.Register
Automatic
o Automatic variables are allocated memory automatically at runtime.
o The visibility of the automatic variables is limited to the block in which they are defined.

The scope of the automatic variables is limited to the block in which they are defined.
o The automatic variables are initialized to garbage by default.
o The memory assigned to automatic variables gets freed upon exiting from the block.
o The keyword used for defining automatic variables is auto.
o Every local variable is automatic in C by default.

Example 1

#include <stdio.h> . #include <stdio.h>


2. int main() 2. int main()
3. { 3. {
4. int a; //auto 4. int a = 10,i;
5. char b; 5. printf("%d ",++a);
6. float c; 6. {
7. printf("%d %c %f",a,b,c); // 7. int a = 20;
printing initial default value of 8. for (i=0;i<3;i++)
automatic variables a, b, and c. 9. {
8. return 0; 10.
9. } printf("%d",a); //
20 will be printed
Output:
3 times since it is
garbage garbage garbage the local value of
a 11. }
12. }
13. printf("%d
",a); // 11 will
be printed since
the scope of a =
20 is ended. 14.
}
Output:

11 20 20 20 11

Static o The variables defined as static specifier can hold their value between the multiple function calls.
o Static local variables are visible only to the function or the block in which they are defined.
o A same static variable can be declared many times but can be assigned at only one time.
o Default initial value of the static integral variable is 0 otherwise null. o The visibility of the
static global variable is limited to the file in which it has declared.
o The keyword used to define static variable is static.

Example 1

#include<stdio.h> #include<stdio.h>
2. static char c; 2. void sum()
3. static int i; 3. {
4. static float f; 4. static int a = 10;
5. static char s[100]; 5. static int b = 24;
6. void main () 6. printf("%d %d \n",a,b);
7. { 7. a++;
8. printf("%d %d %f %s",c,i,f); 8. b++;
// the initial default value of c, 9. }
i, and f will be printed. 9. } 10. void main()
11. {
Output: 12. int i;
0 0 0.000000 (null) 13. for(i = 0; i< 3; i++)
14. {
15. sum(); // The static
variables holds their value
between multiple function
calls. 16. }
17. }
Output:

10 24
11 25
12 26

Register

o The variables defined as the register is allocated the memory into the CPU registers
depending upon the size of the memory remaining in the CPU. o We can not dereference the
register variables, i.e., we can not use &operator for the register variable.
o The access time of the register variables is faster than the automatic variables. o The
initial default value of the register local variables is 0.
o The register keyword is used for the variable which should be stored in the CPU register.
However, it is compiler?s choice whether or not; the variables can be stored in the
register.
o We can store pointers into the register, i.e., a register can store the address of a variable.
o Static variables can not be stored into the register since we can not use more than one
storage specifier for the same variable.

#include <stdio.h> #include <stdio.h>


2. int main() 2. int main()
3. { 3. {
4. register int a; // variable a is 4. register int a = 0;
allocated memory in the CPU 5. printf("%u",&a); // This will give
register. The initial default va lue a compile time error since we
of a is 0. can not access the addres s of a
5. printf("%d",a); register variable.
6. } 6. } Output:

Output: main.c:5:5: error: address of register variable ?a?


requested
0 printf("%u",&a);
^~~~~~
External

o The external storage class is used to tell the compiler that the variable defined as extern is declared with an
external linkage elsewhere in the program.
o The variables declared as extern are not allocated any memory. It is only declaration and
intended to specify that the variable is declared elsewhere in the program.
o The default initial value of external integral type is 0 otherwise null. o We can only
initialize the extern variable globally, i.e., we can not initialize the external variable within
any block or method.
o An external variable can be declared many times but can be initialized at only once.
o If a variable is declared as external then the compiler searches for that variable to be
initialized somewhere in the program which may be extern or static. If it is not, then the
compiler will show an error.

#include <stdio.h> #include <stdio.h> #include <stdio.h>


2. int main() 2. int a; 2. int main()
3. { 3. int main() 3. {
4. extern int a; 4. { 4. extern int a; //
5. printf("%d",a); 5. extern int a; Compiler will
6. } // variable a is search here for a
defined globally, variable a
Output the memory will defined and
main.c:(.text+0x6): not be allocated initialized
undefined reference to to a 6. somewh ere in
`a' printf("%d",a); the pogram or
collect2: error: ld
returned 1 exit status 7. } not.
5. printf("%d",a);
Output
6. }
0
7. int a = 20;

Output

20

You might also like