Learn C Language

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

Storage Class In C

« Previous Page                                                                                                                               Next Page »

The scope or visibility of variables within a c program is called storage classes in c programming.

In other words, you can say that the lifetime of variables is defined by a storage class. Storage classes in c programming tell the compiler about the execution life of variables and functions within a program. There are four types of storage classes in c programming.

Types Of Storage Class?

There are four types of storage class in c:

  • auto storage class
  • register storage class
  • static storage class
  • extern storage class

Auto Storage Class

The auto storage class is the default storage class for all local variables. The most important thing is ‘auto’ storage class can only be used for local variables or you can say it can be used within the function. Though, there is no difference between declaring a variable directly or by prefixing the variable name by auto within a function.

In the above example, there is no difference between both variables and this is a valid representation.

Invalid representation of auto storage class?

The auto storage class can only be used for local variables. Variable b is not a local variable instead it is a global variable. Hence the above representation is an invalid representation.

Register Storage Class?

The local variables that are stored inside a register instead of RAM(Random Access Memory) are of the register storage class.  These variables are prefixed with the register keyword. In other words, you can say that these variables have a maximum size equal to the register size (usually one word) and can’t have the unary ‘&’ operator applied to it (as it does not have a memory location).

The register should only be used for variables that require quick access such as counters. It’s most important to notice that defining ‘register’ does not mean that the variable will be stored in a register. Instead, it means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

Static Storage Class?

The static storage class tells the compiler to keep a local variable in existence during the lifetime of the execution of program instead of creating and destroying it each time it comes into and goes out of scope.

Therefore, making local variables static allows them to maintain their values between function calls. The static modifier may also be applied to global variables. When this is done, it causes that variable’s scope to be restricted to the file in which it is declared.

OUTPUT

Extern Storage Class?

The extern storage class is used to give a reference to a global variable that is visible to all the program files. When you use ‘extern’, the variable cannot be initialized. However, it points to the variable name at a storage location that has been previously defined.

When you have several files in your project and you have defined a global variable or function, which is also be used in other files, then extern will be used in another file to provide the reference of defined variable or function.

First File first.c

Second File second.c

Here, extern is used to declaring count in the second file, whereas it has its definition in the first file, first.c.

OUTPUT: externVariable is 5

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

« Previous Page                                                                                                                               Next Page »

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts

CoursePot.Com

Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

Pin It on Pinterest

Share This