gcc: warning: will never be executed

I get this gcc warning, but the function works correctly. Any ideas?

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    char A[50] = "";
    strcat(A, "Hello World!");

    int COUNT = 0;
    while(A[COUNT])
    {
        A[COUNT] = (char)(tolower(A[COUNT]));
        COUNT++;
    }
    puts(A); // output: hello world!
    return 0;
}

What version of gcc are you using and on what platform? I do not get this warning message with either gcc 3.3 or gcc 4.2.

gcc (Debian 4.3.4-6) 4.3.4

Try moving the init of COUNT to above the strcat. The compiler may be confused....

Tried, but still the same warning.

I tried with gcc-4.4 (Debian 4.4.2-3) 4.4.2 and get the same error but this time repeated:

---------- Post updated at 02:05 PM ---------- Previous update was at 01:43 PM ----------

Got it, it is the optimize cflag.

Bug?

Could be that since you're not working on a variable, but a fixed string the compiler optimizes away the loop and the tolower() call. Since you might expect to see the function called (eg. for profiling purposes) it warns you that there'll be no such call.

The warning looks correct to me.

Given char A[50] = ""; and count=0;, the compiler can put two and two together and realize that A[count] is A[0] is the first character of the empty string "", which is NULL, therefore, the code inside the loop will never execute.

[edit] I missed the strcat. And so is the compiler. Must be a bug. It is a variable, but it's initialized from a fixed string, so the optimizer may be treating it as such.

-Wunreachable-code is broken and has been removed in gcc 4.5.

Bug report:
Bug 42406 - gcc issues warning with tolower function with optimization flag