以下 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”已经给出一样?
也许更好的解决方案是:首先读取一个 char/string in ans
,默认值:"return"
in char/string='s'/"s" 询问下一个问题以读取 integer nfds
,默认值:"return"