一、知识点代码
c
// ============ 骨架:任何程序都从这开始 ============
#include <stdio.h> // 包含头文件:把标准输入输出(printf等)拿进来
int main() // 主函数:程序唯一入口,从这里开始执行
{
return 0; // 返回 0:表示程序正常结束
}c
// ============ 1. 输出文字 printf ============
#include <stdio.h>
int main()
{
printf("Hello, World!\n"); // 打印:引号内是文字,原样输出;\n 是换行
return 0;
}c
// ============ 2. 换行 \n 的作用 ============
#include <stdio.h>
int main()
{
printf("Hello"); // 末尾没写 \n,打印后不换行
printf("World\n"); // 会紧接在 Hello 后面输出成 HelloWorld,最后才换行
return 0;
}c
// ============ 3. 输出数字 %d(占位符)============
#include <stdio.h>
int main()
{
printf("我今年 %d 岁\n", 20); // %d 是占位符,被逗号后面的 20 替换;d=decimal 整数
return 0;
}c
// ============ 4. 多个占位符一一对应 ============
#include <stdio.h>
int main()
{
printf("%d + %d = %d\n", 1, 2, 3); // 3 个 %d 依次对应 1、2、3,输出 1 + 2 = 3
return 0;
}c
// ============ 5. 一条语句以分号 ; 结尾 ============
#include <stdio.h>
int main()
{
printf("Hello"); // 每条语句末尾都要有分号,漏写会编译报错
printf("World\n");
return 0;
}二、课后题 · 代码题
c
// ===== 课后题3:输出 Hello world! =====
#include <stdio.h>
int main()
{
printf("Hello world!\n"); // 引号里是要打印的文字,末尾 \n 换行
return 0;
}c
// ===== 课后题4:分两行输出姓名和学号 =====
#include <stdio.h>
int main()
{
printf("姓名:张三\n"); // 第一行,末尾 \n 换行
printf("学号:20260001\n"); // 第二行:因为上一行换行了,这行在新的一行
return 0;
}三、我出的题 · 随课练习
🔮 猜输出
c
// ===== 猜输出①:先自己猜,再上机验证 =====
#include <stdio.h>
int main()
{
printf("Hello"); // 没写 \n,打印后不换行
printf("World\n"); // 会接在 Hello 后面
return 0;
}c
// ===== 猜输出②:先自己猜,再上机验证 =====
#include <stdio.h>
int main()
{
printf("%d + %d = %d\n", 1, 2, 3); // 3 个 %d 依次填 1、2、3
return 0;
}🛠 变式题
c
// ===== 变式题1:输出 Good Morning! =====
#include <stdio.h>
int main()
{
printf("Good Morning!\n"); // 把引号里的文字换成要输出的内容即可
return 0;
}c
// ===== 变式题2:输出「我今年20岁,身高175厘米」(用两个 %d)=====
#include <stdio.h>
int main()
{
printf("我今年%d岁,身高%d厘米\n", 20, 175); // 两个 %d,逗号后依次跟 20、175
return 0;
}c
// ===== 变式题3:分两行输出「第一行」「第二行」=====
#include <stdio.h>
int main()
{
printf("第一行\n"); // 第一行末尾加 \n 换行
printf("第二行\n"); // 第二行
return 0;
}🐛 改错题
c
// ===== 改错题1:下面这行错在哪?=====
#include <stdio.h>
int main()
{
printf("Hello, World!\n") // ← 错:这行末尾漏了分号 ;
return 0;
}c
// ===== 改错题1 · 正确版 =====
#include <stdio.h>
int main()
{
printf("Hello, World!\n"); // 补上分号 ;
return 0;
}c
// ===== 改错题2:下面这行错在哪?=====
#include <stdio.h>
int main()
{
printf("我今年 %d 岁\n"); // ← 错:写了 %d,但逗号后没给对应的数
return 0;
}c
// ===== 改错题2 · 正确版 =====
#include <stdio.h>
int main()
{
printf("我今年 %d 岁\n", 20); // %d 后面补上数字 20
return 0;
}四、加练题
c
// (暂无。你喊"再出几道 XX 题",我出的代码会追加在这里)