解释型语言和编译型语言
变量和常量
变量
- 局部变量和全局变量
#include <iostream>
using namespace std;
int num = 1; // 全局变量
int main() {
cout << "hello world" << endl;
cout << "请输入你的名字" << endl;
string name;
cin >> name;
int num = 2; // 局部变量
cout << num << endl;
cout << "hello" << name << "你好" << endl;
printf("你好");
cin.get();
return 0;
}
常量
保存的数据不能被修改
- 符号常量 符号常量不能定义数据类型。
#define PI 3.14
- const限定符
const double PI = 3.14;
数据类型
整型
#include <cstddef> // 添加这行以定义 ptrdiff_t
#include <iostream>
using namespace std;
int main() {
short num1 = 123;
cout << sizeof(num1) << endl;
int num2 = 12345;
cout << sizeof num2 << endl;
long num3 = 1234;
cout << sizeof num3 << endl;
long long num4 = 1234;
cout << sizeof num4 << endl;
return 0;
}
// 2
// 4
// 8
// 8
浮点型
- 单精度 float
- 双精度 double
#include <cstddef> // 添加这行以定义 ptrdiff_t
#include <iostream>
using namespace std;
int main() {
short num1 = 123;
cout << sizeof(num1) << endl;
int num2 = 12345;
cout << sizeof num2 << endl;
long num3 = 1234;
cout << sizeof num3 << endl;
long long num4 = 1234;
cout << sizeof num4 << endl;
// 浮点型
float num5 = 1.234;
printf("%f\n", num5);
printf("%d\n", sizeof num5);
double num6 = 3.14;
printf("%d\n", sizeof num6);
return 0;
}
布尔型
- true
- false