C language Modifiers summarize const, static, volatile, auto, extern, register

Source: Internet
Author: User
Tags modifier modifiers strlen volatile

write in front

This afternoon a colleague asked "register" what is the role of the keyword? Oh, you're talking about "register", it's a function of ... My head is suddenly broken, I rub, what do you mean, so familiar with the strange feeling. C Language development time is not short, but it seems that no use of "register", but the role is still know, suddenly can't think up, 10,000 grass mud horse flew over.

In fact, in addition to the C language register , but also contains common,,, const , and static volatile auto extern Other modifiers, and now together to summarize the good.

Register modifier

Register, the register variable, tells the compiler that the variable it declares is used very frequently in the program, and the compiler tries to put this variable in the register as much as possible, so that the program executes more quickly. However, the compiler does not necessarily do this, and you can ignore this option.

Some points to note for register modifiers:

      • Variable must be a CPU-accepted type with a single value that is less than or equal to the length of an integer
      • Can only be used with local variables and function parameters, Global (register) variables are illegal
      • Regardless of whether the register variable is stored in the register, its address is inaccessible (Fetch &)
      • In fact, too much register declaration is not bad, register can be ignored
Const modifierconst modifier Common variable

Sometimes we want to define a variable whose value cannot be changed throughout the scope, such as defining a buffer size, which can be modified with a const.

// 定义常量 strlen
const int strlen = 4096;
// 试图修改 strlen 变量,编译器会报错
strlen = 2048;

General constants are initialized at the same time as they are defined, otherwise they cannot be assigned after they are defined, and are typically initialized in the following ways:

const int num = get_num(); // 运行时初始化
const int num = n; // 运行时初始化
const int num = 10; // 编译时初始化

Can a const variable really not be modified? Look at an example:

const int bufsize = 1024;
int *p = &bufsize;
*p = 2048;
printf("bufsize = %d\n", bufsize);

The print result is 2048 . In fact, the invariant nature of const-modified variables is that the program can not be modified by reference to the variable symbol bufsize, rather than the bufsize variable points to the memory data cannot be modified.

const Modify pointer variable

A const can be used with pointer variables, you can limit pointer variables, or you can restrict what pointer variables point to.

const int *ptr; // 指针指向内容不能修改
int const *ptr; // 与第1种等价
int* const ptr; // 指针ptr变量本身不能修改
const int* const ptr; // 指针变量和指针变量指向内容都不能修改

Const modifying function Parameters

In fact, the use of Const definition constants in the C language has no advantage, and can be used #define instead. A const is usually used in a function parameter, and when the parameter is a pointer, you can use the const limit to prevent the function from modifying the contents of the pointer.


size_t strlen(const char *s);
int strcmp(const char *s1, const char *s2);

There are const limitations in common C standard libraries, and const can be used in our custom functions to ensure the robustness of the program.

const type and non-const type conversions

When a pointer is similar, indicating that the pointer to the const char *str1 str1 content can not be modified, but if the str1 assignment to str2, then str2 not through the const limit, through the str2 can modify the pointer to the content, this loses the meaning of const, the compiler does not advocate to do so.

Const and non-const are two types that assign a non-const pointer to a const pointer, which the compiler accepts, and if you assign a const pointer to a non-const pointer, it will increase the permissions of the pointer variable, unsafe, and possibly the risk of writing. So we obey when we write the program, and the pointer type is as const as possible; You cannot assign a const pointer to a const pointer.

static modifier

The static modifier is the most widely used in the program, and it probably has several uses:

      • Modify local variables: Increases the life cycle of local variables and, if defined uninitialized, defaults to 0
      • Modifying global variables: Narrows the scope of global variables to restrict access in this module (file)
      • Modifier function: Reduces the function of functions, the limit function can only be called by this module
volatile modifier

Keyword volatile feeling is a bit contrary to register, indicating that the variable can be modified at any time, and the system of real-time requirements are very high, please read the contents from memory, do not directly copy the data in the register, it is possible that the data is old. Common usage scenarios include interrupt service programs and register-related operations for embedded systems.

extern modifier

The keyword extern is commonly used before variables and function declarations to indicate that the variable or function was defined elsewhere and is referenced here.
In the HELLO.C:

void hello()
{
printf("Hello.\n");
}

In the Main.c file:

extern void hello();
hello(); // 声明之后调用 hello 函数

When the Main.c file is compiled, tell the compiler to define it somewhere hello() else, just cite it and compile it at rest, and look for the actual defined function when the program is finally linked hello .

Auto Modifier

Keyword Auto can actually be understood as a description of the local variables, rarely in the program to show that a variable is auto.

Reprint: https://www.cnblogs.com/liwei0526vip/p/8620595.html

C language Modifiers summarize const, static, volatile, auto, extern, register

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.