常量的值是固定的,不能变的(fixed)
字面值 literal constant,如 int a = 101中的101
零开头的数字0113是八进制,0x4b,0x,零x开头的是16进制。a==11, b==12, c==13, d==14, e==15, f==16
75 // decimal
0113 // octal
0x4b // hexadecimal
75 // int
75u // unsigned int
75l // long
75ul // unsigned long
3.14159 // 3.14159
6.02e23 // 6.02 x 10^23
1.6e-19 // 1.6 x 10^-19
3.0 // 3.0
3.14159L // long double
6.02e23f // float
字符和字符串
'z'
'p'
"Hello world"
"How do you do?"
x
'x'
转义字符
\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\' single quote (')
\" double quote (")
\? question mark (?)
\\ backslash (\)
'\n'
'\t'
"Left \t Right"
"one\ntwo\nthree"
C字符串可以断行(要求\后面没有别的字符,只有断行)
"string expressed in \
two lines"
C字符串拼接
"this forms" "a single" "string" "of characters"
UNICODE字符串 wchar_t
L"This is a wide character string"
布尔字面值true, false
Boolean literals
宏常量Defined constants (#define)
#define PI 3.14159
#define NEWLINE '\n'
// defined constants: calculate circumference
#include <iostream>
using namespace std;
#define PI 3.14159
#define NEWLINE '\n'
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0;
}
#define是预处理语句,非C++执行语句。
声明常量
const int pathwidth = 100;
const char tabulator = '\t';