Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Storage Class in C Programming

Explore Storage Class in C Programming with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers storage class in c programming with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

Auto

Register

Static

Extern

Storage Class Auto in C

The storage class Auto Is the default storage class for all local variables:

 { int diemthi ; auto int diemthi ; } 

The above example to define two variables in the same storage class, auto can be used inside the function, for example local variables.

The Class Stores the Register in C

The Register Class can be used to define local variables and can be stored in a registry instead of RAM. This means that this variable has a maximum size equivalent to the registration size.

 { register int hocphi ; } 

The register class is only used for variables that require quick access like counters. Note that the definition of 'register' does not mean that the variable can be stored in a register. It means that it can be stored in hardware-dependent registers and with certain restrictions.

Static Storage Class in C

The Static Storage class instructs the compiler to keep local variable values that exist during the program's lifetime instead of creating and destroying it each time it runs over that range. Therefore, create a LocalStatic Variable that allows them to store values with call functions.

This static class can be applied to global variables. When this happens, it causes the scope of the variable to be limited to the file it declares.

In programming C, when static is used for the class, it leads to only one copy of the declaration class shared by all objects using this class.

 #include /* phan khai bao ham */ void ham ( void ); static int biendem = 4 ; /* day la bien toan cuc */ main () { while ( biendem --) { ham (); } printf ( "===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } /* phan dinh nghia ham */ void ham ( void ) { static int i = 6 ; /* bien cuc bo static */ i ++; printf ( "i co gia tri la %d va biendem co gia tri la %dn" , i , biendem ); } 

You may not understand this example because you use global variables, which will be introduced in the next article. Compile and run the above C program to see the results:

The Class Stores Extern in C

Logos that store Extern Are used to refer to global variables that are seen by all program files.

When you have multiple files and you define local variables or functions, it will be used in other files. To understand this problem, extern is used to declare global variables or functions in other files.

The extern keyword is used when two or more files share the same variable or function as the example below:

Start by file: file1. C

 #include int biendem ; extern void ham_extern (); main () { biendem = 5 ; ham_extern (); } 

The second file: file2. C

 #include extern int biendem ; void ham_extern ( void ) { printf ( "biendem co gia tri la %dn" , biendem ); } 

Here, Extern Is the keyword used to declare Biendem In the second line where it is defined in the first file, Main. C. Now, if you are using a command prompt, you compile 2 files as follows:

 $gcc main . c support . c 

It will provide a. Out executable program, when this program is run it will print the following result:

 5 

According to Tutorialspoint

Previous article: Constant in programming C

Next lesson: Operator in programming C

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.