Storage class in C programming

A storage class defines the scope (visibility) and the lifetime of a variable or / and functions in the C program. The preceding type specifications can be changed. Here are the storage classes that can be used in the C program.

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 local static 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:

First 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

4 ★ | 1 Vote

May be interested

  • Class (Class) in C #Class (Class) in C #
    when you define a class in c #, you define a blueprint for a data type. this does not really define any data, but it defines the meaning of that class name. that is, what an object of that class consists of, what activities can be performed on that object.
  • How to Crash a Class in CollegeHow to Crash a Class in College
    in many universities, classes can be impacted. colleges often do not have enough funding to supply enough professors to meet the demand. here are some ways to get into a class when all the spots are full. waitlist the class, if possible....
  • How to Create a CRUD App with Django's Class Based ViewersHow to Create a CRUD App with Django's Class Based Viewers
    let's learn how to build a task management app using crud principles and a class-based viewer in django.
  • Interface in C ++ (Abstract class)Interface in C ++ (Abstract class)
    an interface describes the behavior or capabilities of a class in c ++ without signing to a specific implementation of that class.
  • Set of multiple choice questions about programming with P12Set of multiple choice questions about programming with P12
    the programming test questions below will provide you with useful knowledge on this topic. if you want to study programming, you should not ignore the series of programming tests of network administration.
  • Beginners of computer programming need to focus on what?Beginners of computer programming need to focus on what?
    programming is very interesting and extremely useful. so what should beginners learn about computer programming? let's find out in the article below!
  • Set of multiple choice questions for programming with P15 prizeSet of multiple choice questions for programming with P15 prize
    programming is a discipline that attracts many young people interested. not only that job opportunities after graduation with this job are extremely open. so, if you love programming, you should not ignore the following quiz series of network administrator.
  • How to Write a Class in PythonHow to Write a Class in Python
    in python, classes can help to wrap up data and functionality at the same time. several classes have already been written for us in python 3, called builtins. here are a few: int (integer class), str (string class), list (list class). this...
  • Set of multiple choice questions about programming with P10 prizeSet of multiple choice questions about programming with P10 prize
    the programming questions below will give you lots of useful information. if you are interested in learning programming languages, the series of programming language topics will be very helpful for you.
  • Set of multiple choice questions about programming with P7 prizeSet of multiple choice questions about programming with P7 prize
    current programming is no longer strange to us. programming work is becoming hot and more interested. please join the network administrator to learn about programming skills through multiple-choice questions below.