C/C++ 基础
基本语法
C 语言 Hello World
c
#include <stdio.h>
int main() {
printf("Hello, C!\n");
return 0;
}C++ Hello World
cpp
#include <iostream>
int main() {
std::cout << "Hello, C++!" << std::endl;
return 0;
}数据类型
基本类型
| 类型 | 大小 | 范围 |
|---|---|---|
| char | 1 byte | -128 ~ 127 |
| int | 4 bytes | -2^31 ~ 2^31-1 |
| float | 4 bytes | ~7位精度 |
| double | 8 bytes | ~15位精度 |
C++ 特有类型
bool- 布尔类型string- 字符串类auto- 自动类型推断(C++11)
控制语句
cpp
// 条件
if (condition) {
// ...
} else if (other) {
// ...
} else {
// ...
}
// 循环
for (int i = 0; i < 10; i++) {
// ...
}
while (condition) {
// ...
}