在 C 编程中,为什么在使用 readline() 函数时将 NULL 作为参数传递? 除了使用 const char 之外,还有其他存储名称的方法吗?
#import <readline/readline.h>
#import <stdio.h>
int main(int argc, const char * argv[])
{
printf("Who is cool? ");
const char *name = readline(NULL);
printf("%s is cool!\n\n",name);
return 0;
}
编辑:当使用这段代码(在 Xcode 中)时,我注意到我输入的字符被回显了。 所以我从控制台看到的实际上是这样的:
who is cool? MMiikkeeyy
Mikey is cool!
是否有解释为什么会发生这种情况?
最佳答案
readline
函数是 a GNU thing ,允许用户使用各种 shell 中的所有编辑功能进行输入。
参数只是在要求输入之前发出提示,因此您的代码等效于(在 C 代码中,因为我的 Mac 不方便):
#include <stdio.h>
#include <readline/readline.h>
int main (int argc, const char * argv[]) {
const char *name = readline ("Who is cool? "); // Don't need separate printf.
printf ("%s is cool!\n\n", name);
return 0;
}
可以找到有关如何使用它的更多详细信息 here ,第一个句子中的链接引用的页面之一。
关于c - 使用 readline() 函数时,为什么必须将 NULL 作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27614772/