常量&变量
常量
1.一般常量,单字符,数字或字符串,如:0 , 3.14, ‘a’ , "hello world" 均为一般常量
2.标识符常量,用#define定义的常量,如:#define MAX 1 ,MAX即为标识符常量,其值为1
3.枚举常量,枚举类型是一种的值类型,它用于声明一组命名的常数。
变量
1.一般变量,由基础数据类型定义,如:int a = 0,float b = 3.14,char c = ‘c’,其中a,b,c均为一般变量
2.常变量,变量用const修饰,即为常变量,其值无法修改,但本质仍为变量
补充
对于指定数组长度,只能使用常量指定,而不能使用变量指定
附:
#define MAX 100
#include <stdio.h>
int main()
{
//常量
'3.14';
'a';
'\101';
//变量
int a = 0;
char b = 'a';
int arr[] = { 0 };
//常变量
const int a1 = 0;
const char b1 = 'b1';
const int arr1[] = { 0 };
//枚举常量
enum Gender
{
MALE,
FEMALE,
SECRETCY
};
//变量可赋值改变,不可用作指定数组长度,int arr[a] is wrong
a = 50;
//常变量不可修改值,a1 = 50 error
//但本质为变量,也不可用作指定数组长度,int arr[a1]error
//MAX为标识符常量,用法和常量类似
MAX;
//枚举常量,也为常量,默认值从0开始
MALE;
FEMALE;
SECRETCY;
printf("%d %d %d\n", MALE, FEMALE, SECRETCY);
return 0;
}