您的位置:首页 > 其它

防止编译器优化的关键词volatile

2016-01-08 17:24 225 查看
不使用volatile的情况下:
#include<stdio.h>
#include<stdlib.h>
int main()
{
const int n = 10;
int*p = (int*)&n;
*p = 20;
printf("%d\n", n);
printf("%d\n", *p);
system("pause");
return 0;
}

结果:
20
20
请按任意键继续. . .
#include<stdio.h>
#include<stdlib.h>
int main()
{
volatile const int n = 10;
int*p = (int*)&n;
*p = 20;
printf("%d\n", n);
printf("%d\n", *p);
system("pause");
return 0;
}
结果:
10
20
请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息