Home  >  Article  >  Backend Development  >  Storage classes in C

Storage classes in C

PHPz
PHPzforward
2023-09-11 18:33:03877browse

Storage classes in C

In C language, the characteristics of variables and functions are described by storage classes, such as the visibility and scope of variables or functions.

There are four types of storage classes in C language: automatic variables, external variables, static variables and register variables.

auto

The Auto storage class is the default storage class for all local variables. It is created when the function is called. When the function execution is completed, the variable is automatically destroyed.

They are also called local variables because they are local variables of the function. By default, the compiler assigns garbage values ​​to them.

Scope - Automatic variables are local variables of the function block.

Default value - Garbage value is the default initialization value.

Lifetime - The lifetime of an auto variable is limited by the block in which it is defined.

This is an example of auto variable in C language,

Example

Live demonstration

#include <stdio.h>
int main() {
   auto int a = 28;
   int b = 8;
   printf("The value of auto variable : %d</p><p>", a);
   printf("The sun of auto variable & integer variable : %d", (a+b));
   return 0;
}

Output

The value of auto variable : 28
The sun of auto variable & integer variable : 36

extern

External variables are also called global variables. These variables are defined outside the function. These variables are globally available throughout function execution. The value of global variables can be modified through functions.

Scope - They are not bound to any function. They are everywhere in the program, i.e. globally.

Default value - The default initialization value of global variables is zero.

Life cycle - > Until the end of program execution.

Here is an example of extern variable in C language,

Example

Live demonstration

#include <stdio.h>
extern int x = 32;
int b = 8;
int main() {
   auto int a = 28;
   extern int b;
   printf("The value of auto variable : %d</p><p>", a);
   printf("The value of extern variables x and b : %d,%d</p><p>",x,b);
   x = 15;
   printf("The value of modified extern variable x : %d</p><p>",x);
   return 0;
}

Output

The value of auto variable : 28
The value of extern variables x and b : 32,8
The value of modified extern variable x : 15

static

Static variables are initialized only once. The compiler retains this variable until the end of the program. Static variables can be defined inside or outside a function.

Scope - They are local variables to the block.

Default value - > The default initialization value is zero.

Life cycle - Until the end of program execution.

Here is an example of static variables in C language,

Example

Live demonstration

#include <stdio.h>
int main() {
   auto int a = -28;
   static int b = 8;
   printf("The value of auto variable : %d</p><p>", a);
   printf("The value of static variable b : %d</p><p>",b);
   if(a!=0)
   printf("The sum of static variable and auto variable : %d</p><p>",(b+a));
   return 0;
}

Output

The value of auto variable : -28
The value of static variable b : 8
The sum of static variable and auto variable : -20

register

Register variables tell the compiler to store variables in CPU registers instead of memory. Frequently used variables are kept in registers, where they have faster accessibility. We can never get the addresses of these variables.

Scope - They are limited to inside the function.

Default value - The default initialization value is garbage.

Lifecycle - Before the execution of the code block that defines it ends.

Here is an example of a register variable in C language:

Example

Online demonstration

#include <stdio.h>
int main() {
   register char x = &#39;S&#39;;
   register int a = 10;
   auto int b = 8;
   printf("The value of register variable b : %c</p><p>",x);
   printf("The sum of auto and register variable : %d",(a+b));
   return 0;
}

Output

The value of register variable b : S
The sum of auto and register variable : 18

The above is the detailed content of Storage classes in C. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete