3

For my compiler tests, I need to generate this warning "Statement has no effect" in my test code. How can I do this?

Using VS cl.exe compiler

4
  • Check here: bytes.com/topic/c/answers/… Commented Jan 29, 2011 at 5:43
  • Thanks. I had used similar code. Also as mentioned my others. But I dont get the error I am looking for. Commented Jan 29, 2011 at 6:11
  • 1
    Make sure you have the warning level set sufficiently high. Commented Jan 29, 2011 at 6:11
  • Thank you all.Your comments helped. Commented Jan 29, 2011 at 6:27

5 Answers 5

6
so ross$ cat > noeff.c
void f(void) {
  1;
}
so ross$ cc -Wall -c noeff.c
noeff.c: In function ‘f’:
noeff.c:2: warning: statement with no effect
so ross$ 
Sign up to request clarification or add additional context in comments.

1 Comment

What compiler are you using? For me, cc --version returns i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
2
int main()
{
    5;   // Statement has no effect
    return 0;
}

Edit 1 Tried on VC++ 2010

#include <iostream>
#pragma warning(default:4555)

int main()
{
    5;
    getchar();
    return 0;
}

Output :

warning C4555:main.cpp(6): expression has no effect; expected expression with side-effect

NOTE: It seems VC++ 2010 has no C4705 warning on their list. MSDN Compiler Warnings

3 Comments

Tried this and built mode code with /wall. But the compiler I get is 4255 and not 4705
Does that mean I can no longer see these warnings? How can I know what other warnings were disabled in vs 10 compiler?
As every one said, it depends up on the compiler you are using. From MSDN, I see there is no way of getting the kind of warning message on VS 2010 you want, except C4555.
2
void f();
int main()
{
   f; // Statement has no effect
}

http://ideone.com/oB9kf

1 Comment

Tried this and built mode code with /wall. But the compiler I get is 4255 and not 4705
1

One more:

x == 0;

I found one of these in some code (written by others) recently - I fixed it to the intended 'x = 0;'.

GCC 4.2.1 on MacOS X 10.6.6.

cc -Wall -c x.c
x.c: In function ‘f’:
x.c:5: warning: statement with no effect

Code:

int f(int x)
{
    x *= 3;
    if (x % 2 == 0)
        x == 0;
    return x;
}

What you get with other compilers depends on the other compiler.

1 Comment

Tried this and built mode code with /wall. But the compiler I get is 4255 and not 4705
0

The following C code generates the following warnings with VS2008 at whatever the default warning level is for that compiler:

int main()
{
    int a = 0;
    1;   // this doesn't seem to generate a warning
    a + 1;
    a == 0;

    return 0;
}

C:\temp\test.c(5) : warning C4552: '+' : operator has no effect; expected operator with side-effect
C:\temp\test.c(6) : warning C4553: '==' : operator has no effect; did you intend '='?

In your comments you seem to actually be focused on getting warning C4705 ("statement has no effect"). According to MSDN, it appears that that warning is only documented for VS6. So I think that if you want that specific error code, you'll need to dig up VC++ 6.

Comments

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.