stdio.h
getchar() 和 putchar()
Typically, getchar() and putchar() are not true functions, but are defined using preprocessor macros.
通常,getchar() 和 putchar() 不是真正的函数,而是使用预处理器宏定义的。
getchar()
作用:get a character from stdin
原型:int getchar(void);
Required Header:<stdio.h>
Compatibility:ANSI
Return value:returns the character read. To indicate an read error or end-of-file condition, getchar returns EOF. For getchar, use ferror or feof to check for an error or for end of file.
The getchar() function takes no arguments, and it returns the next character from input.
getchar() 会读取每一个字符, 包括空白.
代码示例:
#include<stdio.h> int main(void) { char c; c = getchar(); // 读取一个字符输入,并将其赋值给 c getchar(); // 读取一个字符输入,但什么也不做,相当于丢弃了这个字符 return 0; }
下面这两行代码效果相同:
c = getchar(); scanf("%c", &c);
putchar()
作用:Writes a character to stdout.
原型:int putchar( int c );
注意 c 的类型是 int.
Parameters:c:Character to be written
Required Header:<stdio.h>
Compatibility:ANSI
Return Value:returns the character written. To indicate an error or end-of-file condition, putchar return EOF; Use ferror or feof to check for an error or end of file.
putchar() 函数打印它的参数.
下面这条语句把 c 的值作为字符打印出来:
putchar(c);
这条语句和下面这条语句效果相同:
printf("%c", c);
putchar() 不会自动换行.
即便传递给 putchar() 一个整数, 也会被转换为字符打印.
程序示例:
#include<stdio.h> int main(void) { char c = 97; putchar(c); printf("%c", 97); return 0; }
结果:
aa
Because these functions deal only with characters, they are faster and more compact than the more general scanf() and printf() functions. Also, note that they don't need format specifiers; that's because they work with characters only. Both functions are typically defined in the stdio.h file. Also, typically, they are preprocessor macros rather than true functions.
程序示例:
输出一串字符, 如果是空格就原样输出, 否则输出它在字符表中的下一个字符.
#include<stdio.h> #include<ctype.h> int main(void) { char ch; while ((ch = getchar()) != '\n') { if (isspace(ch)) putchar(ch); else putchar(ch + 1); // 再次演示了字符实际上是作为数字存储的 } printf("%c", ch); return 0; }
结果:
ok hello morning pl ifmmp npsojoh
putchar(ch + 1);
中,ch 是 char 类型,先转换为 int 类型,再加 1,根据 putchar 的函数原型,其接收的参数类型是 int。
putchar 只会根据最后一个字节确定要显示哪个字符。
程序示例:
#include<stdio.h> int main(void) { putchar('a' + 256 * 1); putchar('\n'); putchar('a' + 256 * 2); putchar('\n'); putchar('a' + 256 * 4 + 1); putchar('\n'); return 0; }
执行结果:
a a b
sprintf
原型:
int sprintf(char *str, const char *format, ...)
发送格式化输出到 str 所指向的字符串。
参数:
str -- 这是指向一个字符数组的指针,该数组存储了 C 字符串。
format -- 这是字符串,包含了要被写入到字符串 str 的文本。它可以包含嵌入的 format 标签,format 标签可被随后的附加参数中指定的值替换,并按需求进行格式化。format 标签属性是 %[flags][width][.precision][length]specifier
对比一下 printf() 的原型: int printf(const char *format, ...)
The first argument to sprintf() is the address of the target string. The remaining arguments are the same as for printf() — a conversion specification string followed by a list of items to be written.
fgets() 和 fputs()
fgets()
原型:
char *fgets(char *str, int n, FILE *stream)
参数:
str -- 这是指向一个字符数组的指针,该数组用来存储要读取的字符串。
n -- 这是要读取的最大字符数(包括最后的空字符)。通常是使用以 str 传递的数组长度。
stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了要从中读取字符的流。
作用: 从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内。当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。
如果成功,该函数返回相同的 str 参数。如果到达文件末尾或者没有读取到任何字符,str 的内容保持不变,并返回一个空指针。
如果发生错误,返回一个空指针。
如果 fgets() 读到一个换行符, 会把这个换行符储存在字符串中.
fputs()
原型:
int fputs(const char *str, FILE *stream)
参数:
str -- 这是一个数组,包含了要写入的以空字符终止的字符序列。
stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了要被写入字符串的流
返回值:
该函数返回一个非负值,如果发生错误则返回 EOF。
get_s()
get_s() 函数只从标准输入中读取数据.
如果 get_s() 函数读取到换行符, 那么就会丢弃它.
只要输入行未超过最大允许字符数, 那么 get_s() 函数和 gets() 函数完全一样, 完全可以用 get_s() 函数替换掉 gets() 函数.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术