2_C语言中的数据类型 (一)2.1.常量和字符串常量

时间:2023-03-08 22:00:26

2.1 常量就是在程序中不可变化的量,常量在定义的时候必须给一个初值。

1.1.1          #define

定义一个宏常量

1.1.2          const

定义一个const常量

2.2       字符串常量

“hello world”

对于#define类型的常量,c语言的习惯是常量名称为大写,但对于普通const常量以及变量,一般为小写结合大写的方式

示例代码:

#include<stdio.h>

#define MAX 10  //定义一个宏常量,值为10
#define STRING "hello world\n" //定义了一个字符串常量 int main()
{
int i; //定义一个变量,名字为i,值可变
const int a = ; //定义了一个const常量,值为20
const char *str = "hello c";
i = ;
i = ;
//MAX = 200; //常量的值不能修改 //a = 0; //常量的值不能修改
printf(STRING);
return ;
}