我在用C语言编写程序时使用getchar()(在课程的这一点上还不允许使用scanf.)我想知道如果它移动到下一个,我是否每次调用它; 包括在任务操作期间.例如; 我试图从控制台读取双倍; 并确定前面是否有负号.如果是的话; 我想将变量neg指定为1(这样我可以判断最终结果是否应该返回负数)然后我想移动到下一个字符来进行实际的双重计算而不是.EX)
int x = getchar();
int neg = 0;
if(x == '-') {
neg = 1;
x = getchar(); // will this make it so the next time I use the x
} // variable it will be past the negative sign and to the
//first actual digit?
Run Code Online (Sandbox Code Playgroud)
是的,每次打电话getchar()都会读取下一个字符(如果有下一个字符要阅读).
引用C11,第7.21.7.6章
该
getchar()函数返回指向的输入流中的下一个字符stdin.
如果没有任何有效的内容可供阅读,
如果流位于文件结尾,则设置流的文件结束指示符并
getchar返回EOF.如果发生读取错误,则设置流的错误指示符并getchar返回EOF.