C语言的常量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qiantanlong/article/details/78488597

常亮的使用是比较频繁的,在 C语言中,有两种简单的定义常量的方式:

  1. 使用 #define 预处理器。
  2. 使用 const 关键字。
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>

#define Height 10
const int Width = 20;

void main() {
	const Age = 30;
	printf("%d,%d,%d\n",Height,Width,Age);
	getchar();
}

#define Height 10

第一种方式:是将Height定义成一种符号,此时Height只是10的别名,在编译期间用10去取代Height的值;

const int Width = 20;

第二种方式:是将Width定义成变量,但告诉编译器它的值是固定不变的,如果在程序中试图去修改它的值,在编译时会报错;


猜你喜欢

转载自blog.csdn.net/qiantanlong/article/details/78488597
今日推荐