violatile关键字

#include<stdio.h>
int main()
{
    volatile const int a =100;
    volatile const int b =200;
    int* p = (int*)&a;
    *p = 123;
    p = (int*)&b;
    *p = 456;
    printf("%d %d",a,b);
    //使用g++编译器编译输出是100,200
    //使用gcc编译器编译输出是123,456
    //因为c++编译器做了优化,把const变量直接用常量值代替了
    //为了防止出现这种情况
    //volatile const int a =100;
    //volatile const int b =100;
    //volatile关键字 表示每次都要从内容中取值
    return 0;
}

上一篇:爬取网页数据


下一篇:JavaScript运算符简单了解