c++ 类型限定符_C语言类型限定符

c++ 类型限定符

Type qualifiers are the keywords that can be prepended to variables to change their accessibility i.e. we can say type qualifiers are used to change the properties of variables.

类型限定词是可以附加到变量以更改其可访问性的关键字,即可以说类型限定词用于更改变量的属性。

类型限定符的类型 (Types of type qualifiers)

There are two Type Qualifiers variable in C programming language,

C编程语言中有两个Type Qualifiers变量,

  1. const qualifier

    const限定词

  2. volatile qualifier

    挥发性限定词

1)const限定词 (1) const qualifier)

The const qualifier is used to declare a variable to be read-only (constant), its value may not be changed, and it can be declared by using const keyword. It helps to prevent accidental change of the values.

const限定符用于将变量声明为只读(常数),其值不能更改,并且可以使用const关键字声明。 这有助于防止意外更改值。

To declare a constant, include the const keyword before or after the data type. Syntax to declare a constant is,

要声明常量,请在数据类型之前或之后包含const关键字。 声明常量的语法是,

const data_type constant_name = value;
or 
data_type const constant_name = value;

Example:

例:

#include <stdio.h>

int main()
{
    // first method
    const float pi = 3.14f;

    // second method
    char const default_string[] = "None";

    // printing the values
    printf("pi = %f\n", pi);
    printf("default_string = %s\n", default_string);

    return 0;
}

Output:

输出:

pi = 3.140000
default_string = None

2)挥发性限定词 (2) volatile qualifier)

The volatile qualifier is used to declare a variable that can be changed explicitly i.e. it tells the compiler that the variable's value may change at any time. It is very useful for embedded programming to keep the updated value that can be updated from the various interrupts. To declare a volatile variable, we use the volatile keyword.

volatile限定符用于声明可以显式更改的变量,即,它告诉编译器该变量的值可以随时更改。 对于嵌入式编程而言,保持可以从各种中断中进行更新的更新值非常有用。 要声明一个volatile变量,我们使用volatile关键字。

To declare a volatile variable, include the volatile keyword before or after the data type. Syntax to declare a volatile variable is,

要声明volatile变量,请在数据类型之前或之后包含volatile关键字。 声明一个volatile变量的语法是

volatile data_type variable_name;
or 
data_type volatile variable_name;

Example:

例:

#include <stdio.h>

int x = 0;
volatile int y = 0;

int main()
{
    y = 0;

    // Here, the compiler optimizes the code and
    // 'else' part will be optimized because the
    // variable (x) will never be other than 0
    if (x == 0) {
        printf("x is zero\n");
    }
    else {
        printf("x is not zero\n");
    }

    // Here, the compiler never optimize 'else' part
    // because the variable (y) is a volatile variable
    if (y == 0) {
        printf("y is zero\n");
    }
    else {
        printf("y is not zero\n");
    }

    return 0;
}

Output:

输出:

x is zero
y is zero


翻译自: https://www.includehelp.com/c/type-qualifiers-in-c-language.aspx

c++ 类型限定符

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值