2

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:

C programming using Xcode

15
  • Please don't post images of code, post code. Commented Sep 29, 2017 at 20:01
  • 1
    this warning is stupid. The loop has a side effect. Commented Sep 29, 2017 at 20:02
  • 1
    I'm at work, without access to Xcode. Somebody should check whether 'Empty loop bodies' is still one of the warnings one can enable or disable in the project build settings. If so, that's the answer. Commented Sep 29, 2017 at 20:05
  • 3
    Lol. The warning isn’t “stupid” and the side effect may well be intended (to skip to the end of the line). The intent of the warning is that, in C, where braces are optional, it’s easy to accidentally include the semicolon when it wasn’t intended. The solution is to make the intent explicit, e.g. while (...) { } (with nothing in the braces) instead of while (...);. Commented Sep 29, 2017 at 20:07
  • 1
    @Jean-FrançoisFabre yeah, my first step in starting a new Xcode-or-anything project is to turn every available warning on. Unrelated comment: having edited the image out of the post, it's no longer clear that the author is actually referring to a warning, not an error. Do you think it's justified also to tweak the text? Commented Sep 29, 2017 at 20:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Semicolon on the next line did silence the warning!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.