Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
communitycreator

What are storage classes in C?

221910308050

Each C programming variable has two features:

  • the storage class
  • the type

Type is the variable’s data type. The scope, visibility, and lifetime of a variable are defined by the storage class.

Storage classes

  1. automatic
  2. external
  3. static
  4. register

Automatic

  • Scope: The auto storage class defined variable is the location of the within function block.

  • Default: Default Introductory Value – any altered value, i.e., waste value.

  • Lifetime: Until the end of the variable function/method block.

The default variable is an automated variable that is declared within a function without the specification of any storage class. It is created when a function is called and automatically destroyed when the function is executed. Automatically speaking, the local variable may also be called because it is local to a function. The compiler assigns them default waste value.

External

  • Scope: Global in all parts of the program. These variables are not linked to any function and are always available.

  • Default: 0zero

  • Lifetime: You can access global variables until the program does not complete execution.

A global variable is declared outside of any function. Throughout program execution, global variables remain available. By default, the global variable’s initial value is 0. One thing about the global variable is that any function in the program can alter its values.

Static

  • Scope: Local for the block that defines the variable

  • Default: 0zero

  • Lifetime: Until it’s running the entire program, the compiler will continue/save the variable until the end of a program with a static variable.

The statistical variable is initialized only once and is present until the end of the program. This is done instead of creating and killing a variable every time it enters into the program. Depending on the location of the declaration, a static variable may be internalremains within the defined function or externalremain confined to the file scope.

Register

  • Scope: Local to the declared function.

  • Default: any random value.

  • Lifetime: Until the end of the function/method block that defines a variable.

Instead of a memory, inform the compiler to save the variable in a CPU register. The variables in Register are more quickly accessible than normal. In general, the variables used are maintained in records. There are, however, only a few variables in the registers. In a very short time, a registry storage class application can be used with loops, where the variable can be used in the program many times.

Which storage class should be used and when?

The following points should be taken into account while using the storage classes to improve the speed of the program execution and carefully use the space occupied by the variable. If we want the variable’s value to remain the same when we call it using a different function called, use the static storage class. Only those variables that are used often in our program should be used with the registry store class. The registers of the CPU are limited and should therefore be used with great attention. For almost every function in the program, we should only use external or global storage classes for those variables if we have nothing to do with the storage clause listed above.

RELATED TAGS

c
communitycreator
RELATED COURSES

View all Courses

Keep Exploring