#include
using namespace std;
// main函数必须有且只有一个
int main() {
// 不换行
//cout << "hello world";
//换行
cout << "hello world" << endl;
system("pause");
return 0;
}
//描述信息:单行注释;
/*描述信息*/:多行注释;
数据类型 变量名 变量初始值
#include
using namespace std;
int main() {
int a = 10;
cout << "a=" << a << endl;
system("pause");
return 0;
}
变量作用是给内存空间起名,方便操作这段内存
作用:用于记录程序中不可修改的数据
#define 常量名 常量值
const 数据类型 常量名 = 常量值
#include
using namespace std;
#define day 7 // 宏常量
int main() {
//day = 8 //一旦修改常量会报错
const int a = 10; // const修饰的变量=常量
//a = 11 // 一旦修改该常量也会报错
cout << "day=" << day << endl;
cout << "a=" << a << endl;
system("pause");
return 0;
}
写变量名称的时候不要和关键词重复!!!
C++规定给标识符(变量、常量)命名时,有一套自己的规则:
为什么会有数据类型这种东西?为了分配一个合理的内存空间
#include
using namespace std;
int main() {
// 短整型(可以表示-2^15到2^15-1)
short num1 = 10;
//2^15-1=32767,假如num1=32768就会输出负数,因为它表示不了那么大
//short num1 =32768 ;
// 整型(可以表示-2^31到2^31-1)
int num2 = 10;
// 长整型(可以表示-2^31到2^31-1)
long num3 = 10;
// 长长整型(可以表示-2^63到2^63-1)
long long num4 = 10;
cout << num1 << endl;
cout << num2 << endl;
cout << num3 << endl;
cout << num4 << endl;
system("pause");
return 0;
}
利用sizeof可以可以统计数据类型所占用的内存的大小,sizeof(数据类型/变量)
#include
using namespace std;
int main() {
short num1 = 10;
int num2 = 10;
long num3 = 10;
long long num4 = 10;
// sizeof(变量)
cout << sizeof(num1) << endl;
cout << sizeof(num2) << endl;
cout << sizeof(num3) << endl;
cout << sizeof(num4) << endl;
//sizeof(数据类型)
cout << sizeof(short) << endl;
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(long long) << endl;
system("pause");
return 0;
}
用于表示小鼠
| 数据类型 | 占用空间 | 有效数字范围 |
|---|---|---|
| float | 4字节 | 7位有效数字 |
| double | 8字节 | 15-16位有效数字 |
在C中,3.14的有效数字位数为3,而不是小数点后的2个数
#include
using namespace std;
int main() {
float f1 = 3.14f; //加f指定是单精度,如果不说明,则认为是双精度数字
double d1 = 3.14;
cout << f1 << endl;
cout << d1 << endl;
cout << sizeof(float) << endl;
cout << sizeof(double) << endl;
// 科学计数法
float f2 = 3e2; // 3*10^2
float f3 = 3e-2; // 3*0.1^2
cout << f2 << endl;
cout << f3 << endl;
system("pause");
return 0;
}
字符型变量用于显示单个字符,char ch = 'a';
#include
using namespace std;
int main() {
char ch = 'a';
//错误1:char ch = "a"; // 不可以使用双引号
//错误2:char ch = 'abcdf'; //不可以有多个字符
cout << ch << endl;
cout << sizeof(ch) << endl;
//查看字符的ASCII的编码
cout << int(ch) << endl;
system("pause");
return 0;
}
用于表示一些不能显示出来的ASCII字符
常用的有\n 、\\、 \t
#include
using namespace std;
int main() {
//换行符 \n
cout << "Helo World\n"; // 等同于cout << "Helo World" << endl;
cout << "Helo \n World";
// 反斜杠\ \
cout << "\\" << endl;
// 水平制表 \t
cout << "aaaa\tbcdef" << endl;
system("pause");
return 0;
}
用于表示一串字符
char 变量名[]=""#include
using namespace std;
int main() {
char str[] = "hello";
cout << str<< endl;
system("pause");
return 0;
}
string 变量名 = ""#include
using namespace std;
#include // 注意这里引入了string的头文件
int main() {
string str = "hello";
cout << str << endl;
system("pause");
return 0;
}
布尔数据类型用于表示真或者假的值。
bool只用两个值:
#include
using namespace std;
int main() {
bool flag1 = true;
bool flag2 = false;
cout << flag1 << endl;
cout << flag2 << endl;
cout << sizeof(bool) << endl;
system("pause");
return 0;
}
从键盘获取值,cin >> 变量
#include
using namespace std;
#include
int main() {
// 整型
int a = 0;
cout << "请给a赋值:" << endl;
cin >> a;
cout << a;
// 浮点型
float b = 0.1;
cout << "请给b赋值:" << endl;
cin >> b;
cout << b;
// 字符型
char c = 'a';
cout << "请给c赋值:" << endl;
cin >> c;
cout << c;
// 字符串型
string d = "ab";
cout << "请给d赋值:" << endl;
cin >> d;
cout << d;
// bool型
bool e = true;
cout << "请给e赋值(0或1):" << endl;
cin >> e;
cout << e;
system("pause");
return 0;
}