常量,就是指在程序运行过程中不会改变的量。
在C语言里如何表示常量呢?
一共有三种方式,一种是字面量表示法:如程序中的1,-22,’a’,”This is a string”等都是字面量表示。
另 一种是名字表示法是:const 类型 常量名 = 值。如:const int age = 21。
第三种表示方式是:#define 标识符 值。这里的标识符通常会用大写字母表示,’值’后面也不能接上分号。
需注意的是编译器会将常量开辟在内存中的常量区,等值的常量在常量区有且只有一个内存空间,任何对它们进行重新赋值的行为都是非法的!
以下是常用的一些字符常量:
Escape sequence | Meaning |
---|---|
\\ | \ character |
\' | ' character |
\" | " character |
\? | ? character |
\a | Alert or bell |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\ooo | Octal number of one to three digits |
\xhh . . . | Hexadecimal number of one or more digits |