Xcode-C 编程 - While 循环 [英] Xcode- C programming - While loop
问题描述
我在 Xcode 中有一个与while"循环相关的错误/警告.有关如何修复它的任何建议?
while ( (c=getchar() != '\n') && c != EOF);
<块引用>
虽然循环体为空
IDE 中的图片:
在不知道背后是哪个编译器的情况下,我只能列出其中 2 个的已知警告标志:
- clang
-Wempty-body
;也包含在-Wextra
中; - Visual C++ C4390、包含在
/W3
中
虽然这个编译器警告对于没有副作用的情况非常有用
while (i > 0);{- 一世;}
(这会导致无限循环)
在您的具体情况下:
while ( (c=getchar() != '\n') && c != EOF);
是跳到下一行 stdin
或输入结尾的完美惯用方式.
从 XCode 打印的警告文本(来自底层编译器)来看,我很确定你的 XCode 是用 clang
配置的,所以,是的,有一种方法可以使用 clang
时关闭警告:
$ clang test.ctest.c:6:12: 警告:while 循环体为空 [-Wempty-body]而 (0);^test.c:6:12: 注意:将分号放在单独的行上以消除此警告
在你的情况下:
while ( (c=getchar() != '\n') && c != EOF);
在那种情况下(个人喜好问题)我会这样做:
while ( (c=getchar() != '\n') && c != EOF){}
因为它们严格等效
因此您可以为其他可能不情愿地输入分号的情况保留警告集,并明确告诉编译器不要担心这些情况.
I have an error/warning associated with "while" loop, in Xcode. Any suggestions on how to fix it?
while ( (c=getchar() != '\n') && c != EOF);
While loop has empty body
Picture in the IDE:
Without knowing which compiler is behind it, I can just list the know warning flags for 2 of them:
- clang
-Wempty-body
; included in-Wextra
too; - Visual C++ C4390, included in
/W3
(source: Why didn't the compiler warn me about an empty if-statement?)
While this compiler warning is very useful for conditions without side-effects like
while (i > 0);
{
--i;
}
(which would cause an infinite loop)
in your specific case:
while ( (c=getchar() != '\n') && c != EOF);
is a perfectly idiomatic way of skipping to the next line of stdin
or end of input.
From the text of the warning that XCode prints (and which comes from the underlying compiler), I'm pretty sure that your XCode is configured with clang
, so, yes, there's a way to turn the warning off when using clang
:
$ clang test.c
test.c:6:12: warning: while loop has empty body [-Wempty-body]
while (0);
^
test.c:6:12: note: put the semicolon on a separate line to silence this warning
in your case:
while ( (c=getchar() != '\n') && c != EOF)
;
in that case (matter of personal taste) I would do:
while ( (c=getchar() != '\n') && c != EOF)
{}
since they are strictly equivalent
So you can leave the warning set for other cases where a semicolon could be typed unwillingly and explicitly tell the compiler not to worry about those cases.
这篇关于Xcode-C 编程 - While 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!