0
点赞
收藏
分享

微信扫一扫

常量(c语言)

常量分为字面常量、const修饰的常变量、#define定义的标识符常量、枚举常量。

字面常量

#include <stdio.h>
int main()
{
//字面常量-直观写出来的值
3;
3.14;
}

const修饰的常变量

#include <stdio.h>
int main()
{
//const - 常属性
//const修饰的常变量
const int num = 0;
printf("%d\n",num);

//常变量是指num仍然是变量,但是具有常属性
const int n = 10;
int arr[n] = {0}; //不能用变量定义数组
//失败的原因是n仍是变量,具有常属性,n不能被改变
return 0;
}

#define定义的标识符常量

#include <stdio.h>
//#define定义的标识符常量
#define MAX 100
int main()
{
int arr[MAX] = {0};//可以用来定义数组
return 0;
}

枚举常量

枚举-一一列举

枚举关键词--enum

#include <stdio.h>

enum Sex
{
MALE,
FEMALE,
SECRET
}; //MALE、FEMALE、SECRET就是枚举常量
int main()
{
//enum Sex s = MALE;
printf("%d\n",MALE); //0
printf("%d\n",FEMALE); //1
printf("%d\n",SECRET); //2
return 0;
}


举报

相关推荐

0 条评论