Menu
CoddyTech

打印与注释

Coddy 面向 Python 开发者的 C 课程的第 2 课,共 48 课。

Python 会为你格式化对象。C 的 printf 使用格式字符串,其中的转换说明符必须与参数类型匹配;%d 会打印 int。注释使用 // 或 /* ... */。换行符需要在 C 格式字符串中显式写出。

Python

print("value=7")

C17

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>

int main(void) {
printf("value=%d\n",7);
return 0;
}

迁移规则

让 printf 转换说明符与参数类型匹配。

challenge icon

挑战

简单

实现 announce(value),打印 value=,后面跟着整数和一个换行符。输入是一个整数。

编辑未锁定的源文件和头文件。确保声明的公共 API 与可见的锁定文件 main.c 兼容,因为它会直接调用该函数。不要再添加另一个 main。除非任务明确检查范围,否则所有数字任务测试数据都足够小,不会溢出。只打印指定的输出;显示输入解析和输出时,由驱动程序负责提供。

亲手试试

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){int n=0;if(scanf("%d",&n)!=1)return 1;announce(n);return 0;}
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

面向 Python 开发者的 C中的所有课

自己练习: C 在线编译器