One, in the c++ compiler:
Compile with .cpp
file
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const int x = 10;
int *p = (int*)&x;
*p = 20;
printf("x=%d\n", x);
system("pause");
return 0;
}
The print result is: x=10
Second, in the c language compiler:
Compile with .c
file
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const int x = 10;
int *p = (int*)&x;
*p = 20;
printf("x=%d\n", x);
system("pause");
return 0;
}
The print result is: x=20
Summarize:
1. Using a C++ compiler, const constants cannot be modified
2. Using a C language compiler, const constants can be modified