Skip to content

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;
}

数据类型

基本类型

类型大小范围
char1 byte-128 ~ 127
int4 bytes-2^31 ~ 2^31-1
float4 bytes~7位精度
double8 bytes~15位精度

C++ 特有类型

  • bool - 布尔类型
  • string - 字符串类
  • auto - 自动类型推断(C++11)

控制语句

cpp
// 条件
if (condition) {
    // ...
} else if (other) {
    // ...
} else {
    // ...
}

// 循环
for (int i = 0; i < 10; i++) {
    // ...
}

while (condition) {
    // ...
}

基于 MIT 许可发布