Latest web development tutorials

C storage class

Storage class definition C program variable / range (visibility) function and life cycle. These specifiers placed before they modify the type. Listed below are storage class C available in the program:

  • auto
  • register
  • static
  • extern

auto storage class

auto storage class is all local variables default storage class.

{
   int mount;
   auto int month;
}

The above example defines two variables with the same storage class, auto can only be used within a function, namely auto only modifies the local variable.

register storage class

register storage class is used to define stored in registers instead of RAM in the local variables.This means that the maximum size of the variable is equal to the size of the register (usually a word), and can not be applied to it unary '&' operator (because it has no memory location).

{
   register int  miles;
}

Need only register for quick access to variables, such as counter. It should also be noted that the definition of 'register' does not mean that variable will be stored in a register, it means that the variable may be stored in a register, depending on the limitations of hardware and implementation.

static storage class

static storage class instructs the compiler to keep the existence of local variables in the program's life cycle, without the need to enter it every time you go out of scope and be created and destroyed.Therefore, the use of static local variables can be modified to maintain the value of local variables between function calls.

static modifier can be applied to global variables. When the modified static global variable, the variable will limit the scope of the statement in its files.

Programming in C, when used onstatic data members of the class, all objects will result in only one copy of the class members are shared.

#include <stdio.h>
 
/* 函数声明 */
void func(void);
 
static int count = 5; /* 全局变量 */
 
main()
{
   while(count--)
   {
      func();
   }
   return 0;
}
/* 函数定义 */
void func( void )
{
   static int i = 5; /* 局部静态变量 */
   i++;

   printf("i is %d and count is %d\n", i, count);
}

Maybe you still can not comprehend this instance, because I have used functions and global variables, these two concepts so far not explain. Even if you do not fully understand, it does not matter, the subsequent chapters we will explain in detail. When the above code is compiled and executed, it produces the following results:

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

extern storage class

extern storage class is used to provide a reference to a global variable, the global variable for all the program files are visible.When you use 'extern', who can not initialize a variable, the variable name will point to a previously defined storage location.

When you have multiple documents and define a global variable or function that can be used in other documents, you can useexternin other files to get a variable or function defined reference. It can beunderstood,extern is used to declare a global variable or function in another file.

extern modifier is usually used when there are two or more files share the same global variables or functions of time, as follows:

First file: main.c

#include <stdio.h>
 
int count ;
extern void write_extern();
 
main()
{
   count = 5;
   write_extern();
}

The second file: support.c

#include <stdio.h>
 
extern int count;
 
void write_extern(void)
{
   printf("count is %d\n", count);
}

Here, the second fileexternkeyword is used tocountin the first statement has been defined in a file main.c. Now, compile both files, as follows:

 $gcc main.c support.c

This will producea.out executable program, when the program is executed, it will produce the following results:

5