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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Addy
  • 49
  • 4
  • Please don't post images of code, post code. – unwind Sep 29 '17 at 20:01
  • do you know copy/paste _as text_ ? – Jean-François Fabre Sep 29 '17 at 20:01
  • 1
    this warning is stupid. The loop has a side effect. – Jean-François Fabre Sep 29 '17 at 20:02
  • here: I manually OCR'ed that for you – Jean-François Fabre Sep 29 '17 at 20:03
  • you could add a body: move `c!=EOF` in it. – Jean-François Fabre Sep 29 '17 at 20:04
  • I try to put the semicolon for empty code blocks on a separate line to make it obvious that it's intentional. Probably won't do anything for the warning message though. – itsme86 Sep 29 '17 at 20:04
  • 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. – Tommy Sep 29 '17 at 20:05
  • @Tommy, however this feature is useful for things like `while (i < 0);` – Jean-François Fabre Sep 29 '17 at 20:06
  • 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 (...);`. – Rob Sep 29 '17 at 20:07
  • @Rob there's a way to turn it off with clang for that particular occurrence, see my answer. Yes, looks very much like your comment, now I notice. – Jean-François Fabre Sep 29 '17 at 20:32
  • 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? – Tommy Sep 29 '17 at 20:42
  • @Jean-FrançoisFabre - Yes, I know how to turn off warnings. By the way, you can do that either as a clang command line switch or with a `#pragma clang diagnostic ignored "-W..."` as [shown here](https://stackoverflow.com/a/23859952/1271826). Or there's actually an "empty loop bodies" setting in your target build options (you have to show all settings to see it). But, like you, I think it best to just write code to make this explicit (using `{ }` or, at least with `;` on its own line) and not suppress the warning at all. It's a useful warning for Objective-C programming. – Rob Sep 30 '17 at 00:45
  • not saying you don't know. and turning them off is indeed a bad idea. Better to change locally so the warning doesn't appear. – Jean-François Fabre Sep 30 '17 at 07:48
  • @Rob that said, I downloaded clang for windows and I can't get this empty body warning even with `-Wempty-body`. Windows version outdated? – Jean-François Fabre Sep 30 '17 at 12:38
  • I downloaded Windows clang (`clang --version` reports 5.0.0) and `clang test.c` reported empty body (as did `clang -Wempty-body test.c`). Only when I did `clang -Wno-empty-body test.c` did I not see empty body warning. – Rob Sep 30 '17 at 16:14

1 Answers1

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219