Home > Backend Development > C++ > body text

Explain the life cycle of variables in C language

WBOY
Release: 2023-09-02 19:37:07
forward
1556 people have browsed it

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);
}
Copy after login

Output

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

3 2 1
Copy after login

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);
}
Copy after login

Output

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

3 1
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template