字面常量
#include <stdio.h>
int main()
{
//字面常量
3.14;
1000;
return 0;
}
const 修饰的常变量
使用 const 修饰常变量时一定要注意两个点:
注意点#1:
使用 const 修饰的常变量的值是不可以更改的
#include <stdio.h>
int main()
{
//const 修饰的常变量注意点#1:
const float PI = 3.14;
PI = 3.145;
return 0;
}
注意点#2:
在C语言中不可以用 const 修饰的常变量作为数组的大小来定义数组
(事实上被 const 修饰的变量只是在语法层面限制了该变量不能直接改变,其本质上还是变量,C语言中的数组的要求是严格的常量。PS:在C++是能够使用 const 修饰的常变量来定义数组的)
#include <stdio.h>
int main()
{
//const 修饰的常变量注意点#2:
const int N = 500;
int arr[N];
return 0;
}
#define 定义的标识常量
#include <stdio.h>
#define MAX 100
int main()
{
printf("MAX = %d\n", MAX);
return 0;
}
枚举常量
枚举变量包含了限定的未来可能取的值。#include <stdio.h>
enum Sex//枚举变量
{
//内容包含枚举常量
male,
female,
secret
};
int main()
{
enum Sex Jake = male;
return 0;
}
枚举常量的默认是从0开始依次递加1