【发布时间】:2026-02-11 17:15:02
【问题描述】:
以下 C 代码有什么问题? (更新)
int nfds = 0;
char c[2] = " ";
char ans[2] = " ";
printf("Test p or s [p,s]: p? ");
ans[0] = (char)getchar();
if (ans[0] != '\n')
{
ans[1] = '\0';
printf("ans = %s\n", ans);
}
else
ans[0] = '\0';
/* FIXME: answering 's' -> nfds is never read */
printf ("Choose [0, 1, 2, 3]: 0? ");
c[0] = (char)getchar();
if (c[0] != '\n')
{
c[1] = '\0';
nfds = strtol(c, NULL, 10);
}
else
c[0] = '\0'
printf( "nfds=%d\n", nfds);
回答“return”有效,但回答“s+return”不会在 print("Choose...") 处停止,而是继续执行,就好像“return”已经给出一样?
也许更好的解决方案是:首先在ans中读取一个字符/字符串,默认:"return" in char/string='s'/"s" 询问下一个问题以读取整数nfds,默认:"return"
【问题讨论】:
-
回答 s^D 有效,但不回答 s^J(\n=return)