gpt4 book ai didi

c - 如何像语言学一样理解C编程——C库

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:15 24 4
gpt4 key购买 nike

我正在学习 C 并且正在尝试了解库。

例如,在 The GNU C Library 中,它给出:

— Function: char * fgets (char *s, int count, FILE *stream)

The fgets function reads characters from the stream stream up to and including a newline character and stores them in the string s, adding a null character to mark the end of the string. You must supply count characters worth of space in s, but the number of characters read is at most count − 1. The extra character space is used to hold the null character at the end of the string.

If the system is already at end of file when you call fgets, then the contents of the array s are unchanged and a null pointer is returned. A null pointer is also returned if a read error occurs. Otherwise, the return value is the pointer s.

Warning: If the input data has a null character, you can't tell. So don't use fgets unless you know the data cannot contain a null. Don't use it to read files edited by the user because, if the user inserts a null character, you should either handle it properly or print a clear error message. We recommend using getline instead of fgets.

但是,我仍然看到人们使用 fgets 从控制台而不是文件收集输入,例如:

fgets(line,sizeof(line),stdin);
sscanf(line,"%d",&current); // read the input

有人可以向我解释我应该如何阅读和遵循 C 语言的语法吗?是通过模仿别人的代码来学习 C,还是通过实际引用库然后自己编写代码来学习 C?

干杯,

最佳答案

stdin 类型为FILE *的变量, 它在 <stdio.h> 中定义.用于从standard input中读取数据.

关于c - 如何像语言学一样理解C编程——C库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5602072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com