常量:
- const <类型名> <常量名> = <表达式>
- #define <宏名> <常量>
例:
const float pi = 3.1415926;
#define pi 3.1415926
L2-1 计算圆的面积
#include "iostream"
using namespace std;
int main(void)
{
const double pi = 3.1415926; //常量pi=3.1415926
double radius(0);
cout << "Please input a radius of a circle" << endl;
while (cin >> radius) //输入半径
{
if (radius <= 0)break;
cout << "Area of the circle is " << pi*radius*radius << endl; //输出面积
}
system("pause");
return 0;
}
变量:
[<存储类>] <类型名> <变量名表>
存储类:
- auto:一次性存储,其存储空间可被若干变量多次覆盖
- register:存放在通用寄存器中
- extern:在所有函数中都可引用
- static:固定存储位置,程序的整个运行期间都有效
例:
int a;
char ch;
unsigned int u32;
float fi=2.31;
变量命名规则--------前缀符号
前缀 | 数据类型 |
c | 字符 |
by | 字节 |
n | 短整数和整数 |
i | 整数 |
b | 布尔型 |
w | 无符号字 |
l | 长整数 |
dw | 无符号长整数 |
fn | 函数指针 |
s | 串 |
sz | 以0为结束的字符串 |
lp | 32位长整数指针 |
h | 句柄 |
msg | 消息 |