C 作为一门兼具高效性和灵活性的编程语言自诞生以来一直是系统开发、游戏引擎、嵌入式开发等领域的核心工具。它既保留了 C 语言的过程化编程特性又引入了面向对象编程OOP、泛型编程等高级特性是编程新手进阶的重要基石。本文将从环境搭建、基础语法、数据类型、流程控制、函数、数组、指针到面向对象基础等维度结合大量可运行的代码示例全面讲解 C 入门核心知识点帮助你从零开始掌握 C 的基础框架。一、C 开发环境搭建在编写第一个 C 程序前首先需要搭建合适的开发环境。C 作为编译型语言需要编译器将源代码转换为可执行文件同时搭配编辑器 / IDE 提升开发效率。1.1 主流开发工具选择Windows 平台Dev-C轻量级、开箱即用适合新手入门Code::Blocks开源免费支持多编译器Visual Studio 2022微软官方 IDE功能强大内置 MSVC 编译器推荐新手使用社区版免费MinGWGCC 编译器的 Windows 移植版可搭配 VS Code 使用。Linux 平台自带 GCC 编译器搭配 VS Code、Vim 或 CLion 即可。macOS 平台安装 Xcode Command Line Toolsxcode-select --install获取 Clang/GCC搭配 Xcode 或 VS Code。1.2 第一个 C 程序Hello World无论学习哪门语言Hello World 都是入门的起点。以下是标准的 C Hello World 程序我们以此为例讲解基础结构bash代码解读复制代码// 包含输入输出流头文件cout/cin需要依赖此文件 #include iostream // 使用std命名空间避免每次写std::cout using namespace std; // 主函数程序的入口int表示返回值类型 int main() { // 输出字符串到控制台endl表示换行并刷新缓冲区 cout Hello, C World! endl; // 暂停程序Windows平台避免控制台一闪而过 // system(pause); // 返回0表示程序正常结束 return 0; }代码解释#include iostream预处理指令引入标准输入输出流头文件iostream是 input/output stream 的缩写提供了控制台输入输出的功能using namespace stdstd是 C 标准库的命名空间C 标准库的所有内容都封装在std中此语句让我们可以直接使用cout而非std::coutint main()main函数是程序的唯一入口int表示函数执行后返回一个整数cout Hello, C World! endlcout是标准输出流对象是流插入运算符用于将内容输出到控制台endl等价于\n换行符但会额外刷新输出缓冲区return 0主函数返回 0 表示程序正常退出非 0 值通常表示程序异常。编译运行若使用 GCC 编译器在终端执行bash代码解读复制代码g hello.cpp -o hello # 编译源代码生成可执行文件 ./hello # 运行可执行文件Linux/macOS # 或 hello.exe # Windows平台若使用 IDE如 Visual Studio直接点击 “运行” 按钮即可。二、C 基础语法2.1 注释注释是代码的说明文字编译器会忽略注释内容。C 支持两种注释方式bash代码解读复制代码// 单行注释以//开头直到行尾 /* 多行注释以/*开头以*/结尾 可跨多行 注意多行注释不能嵌套 */ #include iostream using namespace std; int main() { // 输出测试语句单行注释 cout 测试注释 endl; /* 行内多行注释 */ return 0; }2.2 标识符与关键字2.2.1 标识符标识符是变量、函数、类等自定义名称的统称命名规则由字母A-Z/a-z、数字0-9、下划线_组成不能以数字开头区分大小写如age和Age是两个不同的标识符不能与 C 关键字重名。合法标识符示例name、_score、stu_123、MAX_NUM非法标识符示例123abc以数字开头、stu-name含非法字符 -、int关键字。2.2.2 关键字关键字是 C 语言预留的特殊单词具有固定含义不能作为标识符使用。常见关键字如下类别关键字示例数据类型int、char、float、double、bool控制语句if、else、for、while、switch存储类static、const、extern面向对象class、public、private、virtual其他return、void、sizeof、typedef2.3 变量与常量2.3.1 变量变量是程序中用于存储数据的内存单元其值可以在程序运行过程中改变。定义变量的语法bash代码解读复制代码数据类型 变量名 初始值; // 推荐初始化 // 或 数据类型 变量名; // 不推荐未初始化的变量值为随机值示例bash代码解读复制代码#include iostream using namespace std; int main() { // 整型变量存储整数 int age 18; // 浮点型变量存储小数 float height 175.5f; // float需加f后缀否则默认是double double weight 65.8; // double精度更高 // 字符型变量存储单个字符用单引号包裹 char gender M; // 布尔型变量存储真true/1或假false/0 bool isStudent true; // 输出变量值 cout 年龄 age endl; cout 身高 height cm endl; cout 体重 weight kg endl; cout 性别 gender endl; cout 是否学生 boolalpha isStudent endl; // boolalpha将布尔值以true/false输出而非1/0 // 修改变量值 age 19; cout 修改后的年龄 age endl; return 0; }输出结果bash代码解读复制代码年龄18 身高175.5cm 体重65.8kg 性别M 是否学生true 修改后的年龄192.3.2 常量常量是程序运行过程中值不可改变的量C 定义常量有两种方式bash代码解读复制代码#include iostream using namespace std; // 1. 宏常量预处理阶段替换无类型检查 #define PI 3.1415926 #define MAX_SCORE 100 int main() { // 2. const常量编译阶段检查有类型限制推荐 const int MIN_AGE 0; // 使用常量 double radius 5.0; double area PI * radius * radius; cout 圆的面积 area endl; // 常量不可修改以下代码会报错 // PI 3.14; // 宏常量修改报错 // MIN_AGE 1; // const常量修改报错 return 0; }输出结果bash代码解读复制代码圆的面积78.5398对比#define是预处理指令仅做文本替换无类型安全检查const是编译期常量有类型限制占用内存更安全推荐优先使用。2.4 数据类型C 的数据类型分为基本数据类型和复合数据类型基础阶段重点掌握基本数据类型类型关键字大小字节取值范围用途布尔型bool1true/false等价于 1/0表示真假字符型char1-128 ~ 127或 0 ~ 255取决于编译器存储单个字符短整型short2-32768 ~ 32767小范围整数整型int4-2^31 ~ 2^31-1常用整数长整型long4/8取决于系统至少 4 字节较大范围整数长长整型long long8-2^63 ~ 2^63-1超大范围整数浮点型float46~7 位有效数字单精度小数双精度浮点型double815~16 位有效数字常用小数默认示例数据类型大小与取值bash代码解读复制代码#include iostream #include climits // 包含整型取值范围常量 #include cfloat // 包含浮点型取值范围常量 using namespace std; int main() { // 输出各数据类型的大小字节 cout bool大小 sizeof(bool) 字节 endl; cout char大小 sizeof(char) 字节 endl; cout short大小 sizeof(short) 字节 endl; cout int大小 sizeof(int) 字节 endl; cout long大小 sizeof(long) 字节 endl; cout long long大小 sizeof(long long) 字节 endl; cout float大小 sizeof(float) 字节 endl; cout double大小 sizeof(double) 字节 endl; // 输出整型取值范围 cout int最小值 INT_MIN endl; cout int最大值 INT_MAX endl; cout long long最小值 LLONG_MIN endl; cout long long最大值 LLONG_MAX endl; // 输出浮点型精度 cout float有效数字 FLT_DIG 位 endl; cout double有效数字 DBL_DIG 位 endl; return 0; }输出结果64 位系统bash代码解读复制代码bool大小1字节 char大小1字节 short大小2字节 int大小4字节 long大小8字节 long long大小8字节 float大小4字节 double大小8字节 int最小值-2147483648 int最大值2147483647 long long最小值-9223372036854775808 long long最大值9223372036854775807 float有效数字6位 double有效数字15位2.5 运算符运算符是用于执行运算的符号C 运算符分为算术运算符、赋值运算符、比较运算符、逻辑运算符、位运算符等以下是常用运算符的示例2.5.1 算术运算符运算符功能示例加法a b-减法a - b*乘法a * b/除法a /b整数除法取整%取模余数a % b仅整数可用自增a后置、a前置--自减a--后置、--a前置bash代码解读复制代码#include iostream using namespace std; int main() { int a 10, b 3; // 基本算术运算 cout a b a b endl; // 13 cout a - b a - b endl; // 7 cout a * b a * b endl; // 30 cout a / b a / b endl; // 3整数除法 cout a % b a % b endl; // 1余数 // 自增自减 int c 5; cout c c endl; // 5先输出后自增 cout c c endl; // 6 cout c c endl; // 7先自增后输出 int d 8; cout d-- d-- endl; // 8先输出后自减 cout d d endl; // 7 cout --d --d endl; // 6先自减后输出 return 0; }2.5.2 赋值运算符运算符功能示例等价于基本赋值a 5-加后赋值a 5a a 5-减后赋值a - 5a a - 5*乘后赋值a * 5a a * 5/除后赋值a / 5a a / 5%模后赋值a % 5a a % 5bash代码解读复制代码#include iostream using namespace std; int main() { int x 10; x 5; // x 15 cout x 5: x endl; x - 3; // x 12 cout x - 3: x endl; x * 2; // x 24 cout x * 2: x endl; x / 4; // x 6 cout x / 4: x endl; x % 4; // x 2 cout x % 4: x endl; return 0; }2.5.3 比较运算符比较运算符返回布尔值true/false常用于条件判断运算符功能示例等于a b!不等于a ! b大于a b小于a b大于等于a b小于等于a bbash代码解读复制代码#include iostream using namespace std; int main() { int m 8, n 8; cout boolalpha; // 输出true/false而非1/0 cout m n: (m n) endl; // true cout m ! n: (m ! n) endl; // false cout m n: (m n) endl; // false cout m n: (m n) endl; // false cout m n: (m n) endl; // true cout m n: (m n) endl; // true return 0; }2.5.4 逻辑运算符逻辑运算符用于组合多个条件返回布尔值运算符功能示例规则逻辑与a b全为 true 则 true否则 false逻辑或ab有一个 true 则 true否则 false!逻辑非!a取反true 变 falsefalse 变 truebash代码解读复制代码#include iostream using namespace std; int main() { bool cond1 true, cond2 false; cout boolalpha; cout cond1 cond2: (cond1 cond2) endl; // false cout cond1 || cond2: (cond1 || cond2) endl; // true cout !cond1: !cond1 endl; // false cout !cond2: !cond2 endl; // true // 实际应用判断年龄是否在18-60之间 int age 25; bool isAdult (age 18) (age 60); cout 是否为成年且未退休 isAdult endl; // true return 0; }三、流程控制语句流程控制用于改变程序的执行顺序C 提供三种基本流程结构顺序结构默认、选择结构if/switch、循环结构for/while/do-while。3.1 选择结构3.1.1 if 语句if语句根据条件执行不同的代码块分为单分支、双分支、多分支单分支 ifbash代码解读复制代码#include iostream using namespace std; int main() { // 单分支条件为true时执行 int score 90; if (score 60) { cout 成绩合格 endl; } return 0; }双分支 if-elsebash代码解读复制代码#include iostream using namespace std; int main() { // 双分支条件为true执行if块否则执行else块 int score 55; if (score 60) { cout 成绩合格 endl; } else { cout 成绩不合格 endl; } return 0; }多分支 if-else if-elsebash代码解读复制代码#include iostream using namespace std; int main() { // 多分支依次判断条件匹配则执行对应块 int score 85; if (score 90) { cout 优秀 endl; } else if (score 80) { cout 良好 endl; } else if (score 70) { cout 中等 endl; } else if (score 60) { cout 及格 endl; } else { cout 不及格 endl; } return 0; }3.1.2 switch 语句switch语句适用于多分支的等值判断比多分支 if 更简洁bash代码解读复制代码#include iostream using namespace std; int main() { // switch根据表达式的值匹配case int grade 3; switch (grade) { case 1: cout 一年级 endl; break; // 跳出switch避免穿透 case 2: cout 二年级 endl; break; case 3: cout 三年级 endl; break; default: // 所有case不匹配时执行 cout 未知年级 endl; } return 0; }注意switch的表达式只能是整型、字符型或枚举类型case后必须是常量表达式不能是变量break用于终止当前 case否则会发生 “case 穿透”执行后续 casedefault可选通常放在最后处理未匹配的情况。3.2 循环结构循环结构用于重复执行一段代码直到满足终止条件。3.2.1 for 循环for循环适用于已知循环次数的场景语法bash代码解读复制代码for (初始化表达式; 条件表达式; 更新表达式) { 循环体; }示例 1遍历 1-10 的整数并求和bash代码解读复制代码#include iostream using namespace std; int main() { int sum 0; // 初始化i1条件i10为true时执行循环体每次循环后i for (int i 1; i 10; i) { sum i; // 累加 cout 当前i i 累计和 sum endl; } cout 1-10的和 sum endl; // 55 return 0; }示例 2输出 100 以内的偶数bash代码解读复制代码#include iostream using namespace std; int main() { cout 100以内的偶数 endl; for (int i 0; i 100; i 2) { cout i ; // 每输出10个换行 if (i % 20 0 i ! 0) { cout endl; } } return 0; }3.2.2 while 循环while循环适用于未知循环次数的场景语法bash代码解读复制代码while (条件表达式) { 循环体; }示例计算 1-100 的累加和while 版bash代码解读复制代码#include iostream using namespace std; int main() { int sum 0; int i 1; // 条件i100为true时执行循环体 while (i 100) { sum i; i; // 必须手动更新循环变量否则死循环 } cout 1-100的和 sum endl; // 5050 return 0; }3.2.3 do-while 循环do-while循环先执行一次循环体再判断条件语法bash代码解读复制代码do { 循环体; } while (条件表达式);示例输入密码直到正确bash代码解读复制代码#include iostream #include string // 字符串头文件 using namespace std; int main() { string password; const string correct_pwd 123456; // 至少执行一次输入 do { cout 请输入密码; cin password; if (password ! correct_pwd) { cout 密码错误请重新输入 endl; } } while (password ! correct_pwd); cout 密码正确登录成功 endl; return 0; }3.2.4 循环控制关键字break跳出当前循环或 switch终止循环continue跳过当前循环的剩余语句直接进入下一次循环goto跳转到指定标签不推荐使用易导致代码混乱。示例break 和 continue 的使用bash代码解读复制代码#include iostream using namespace std; int main() { // break示例找到第一个大于5的数就退出循环 cout break示例 endl; for (int i 1; i 10; i) { if (i 5) { break; } cout i ; // 输出1 2 3 4 5 } cout endl; // continue示例跳过偶数只输出奇数 cout continue示例 endl; for (int i 1; i 10; i) { if (i % 2 0) { continue; // 跳过偶数 } cout i ; // 输出1 3 5 7 9 } cout endl; return 0; }