c_cpp C / C ++中常量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C / C ++中常量相关的知识,希望对你有一定的参考价值。

#include<stdio.h>

int main(){
	const int c = 0;
	int* p = (int*)&c;
	
	printf("Begin...\n");
	*p = 5;
	printf("c = %d\n",c);
	printf("End...\n");
	return 0;
} 
#include<iostream>
using namespace std;

void f(){
	#define a 3		//此处为预处理,在编译之前已经将所有a都进行了替换
	const int b = 4;
}

void g(){
	printf("a = %d\n",a); //此处a已经进行了替换
	//printf("b = %d\n",b);
}

int main(){
	const int A = 1;
	const int B = 1;
	int array[A + B] = {0};//此处若用C语言编译器,则报错。因为数组大小定义使用变量
	int i = 0;
	for(i = 0;i < (A + B);++i)
		printf("array[%d] = %d\n",i,array[i]);
	f();
	g();
	return 0;
}

以上是关于c_cpp C / C ++中常量的主要内容,如果未能解决你的问题,请参考以下文章