Home  >  Article  >  Backend Development  >  Explain the life cycle of variables in C language

Explain the life cycle of variables in C language

WBOY
WBOYforward
2023-09-02 19:37:071547browse

Explain the life cycle of variables in C language

The storage class specifies the scope, life cycle and binding of the variable.

To fully define a variable, it is necessary to mention not only its "type" but also its storage class.

A variable name identifies a physical location in computer memory where a set of bits is allocated to store the variable's value.

The storage class tells us the following factors -

  • Where are the variables stored (in memory or CPU registers)?
  • If there is no initialization, what is the initial value of the variable?
  • What is the scope of a variable (the scope of the variable that can be accessed)?
  • What is the life cycle of a variable?

Lifetime

The lifetime of a variable defines the duration for which the computer allocates memory for it (the duration between memory allocation and deallocation).

In C language, variables can have automatic, static or dynamic life cycle.

  • Automatic - Create variables with automatic lifecycle. Each time, their manifesto was met and destroyed. Additionally, their blocks will also exit.
  • static - A variable is created the first time the declaration is executed. It is destroyed when execution stops/terminates.
  • Dynamic - Variable memory is allocated and released through memory management functions.

Storage Class

There are four storage classes in C language-

tr>
Storage Class Storage Area Default initial value Life cycle Scope Keyword
Auto Memory Until control remains in the block Until control remains in the block Local Automatic
Registers CPU Registers Garbage Value Until control remains in the block Local Register
static memory zero value between function calls local static
External Memory Garbage value Entire program execution Global External

Example

The following is a C program for the automatic storage class-

Live Demo

#include<stdio.h>
main ( ){
   auto int i=1;{
      auto int i=2;{
         auto int i=3;
         printf ("%d",i)
      }
      printf("%d", i);
   }
   printf("%d", i);
}

Output

When executing the above program, the following output will be produced-

3 2 1

Example

The following is a C program for the external storage class-

Live demonstration

#include<stdio.h>
extern int i =1; /* this &lsquo;i&rsquo; is available throughout program */
main ( ){
   int i = 3; /* this &lsquo;i&#39; available only in main */
   printf ("%d", i);
   fun ( );
}
fun ( ) {
   printf ("%d", i);
}

Output

When executing the above program, the following output is produced -

3 1

The above is the detailed content of Explain the life cycle of variables in C language. 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