Can const constants be modified?

One, in the c++ compiler:

Compile with .cppfile

#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 .cfile

#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

Guess you like

Origin blog.csdn.net/weixin_46060711/article/details/128106952
Recommended