I was debugging some code yesterday and, after about fifteen minutes, found the culprit. This was in the code:

if (a = b)
{
     // code
}

instead of this:

if (a == b)
{
     // code
}

A very common mistake, but an aggravating one. My question is this. Does a compiler flag exist that checks for such potential errors (i.e. assignment operator where a comparison operator is normally expected, such as inside an if statement's parentheses) and issues a warning when that occurs? Or does one simply have to be vigilant when coding to avoid that error, and be on the lookout for that error when debugging, and you have to catch that error yourself?

Recommended Answers

All 10 Replies

Well that would depend on your compiler.

gcc with something like -Wall does.
As does say /w4 with visual studio express.

Well that would depend on your compiler.

gcc with something like -Wall does.
As does say /w4 with visual studio express.

Thanks Salem. Sorry, should have mentioned I was using Dev C++. I passed the -Wall option to the compiler for this program:

int main ()
{
    int a = 4;
    int b = 6;
    int c;
    
    if (a = b)
    {
          a = 5;
    }

    return 0;
}

It warned me that I never use the variable c, but didn't warn me on the = versus == issue. I added the /w4 option and that didn't appear to do anything in Dev C++. I changed /w4 to /W4 and that didn't change anything. I haven't tried it yet in Visual C++. When I changed the code to a .c extension, thus invoking the gcc compiler, the -Wall option gave this warning:

[Warning] suggest parentheses around assignment used as truth value

Does anyone know how to get this to display for a .cpp extension using g++ in Dev C++? -Wall gives the "unused variable" warning in g++, but apparently not this one.

IIRC, there are separate dialogs for passing options to gcc and g++

The gcc options I use when I want to be pretty strict about things are gcc -W -Wall -ansi -pedantic -O2 The two -W options turn on a lot of warnings (though not all of the ones which I would consider useful) which are practical for most common code.
The ansi/pedantic make sure it's squeaky-clean ANSI code
The O2 turns on all the data flow analysis to root out all those unused vars, use before initialisations etc.

commented: helpful. +4

well, if (a=b) { . . . } is perfectly valid, as im sure youre aware.

i used to purposely code in that manner, before i got the habit slapped out of me by a principal engineer who (god forbid!) insisted code be readable. :P i mean, not in such a trivial way, but more like the convention: if (err = myFunction()) { . . . } it's not uncommon. i'm not saying it's good style, but i see people code like that a lot.


.

IIRC, there are separate dialogs for passing options to gcc and g++

The gcc options I use when I want to be pretty strict about things are gcc -W -Wall -ansi -pedantic -O2 The two -W options turn on a lot of warnings (though not all of the ones which I would consider useful) which are practical for most common code.
The ansi/pedantic make sure it's squeaky-clean ANSI code
The O2 turns on all the data flow analysis to root out all those unused vars, use before initialisations etc.

I got it to work in Visual C++ using W4 as you suggested. Regarding Dev C++, I plugged in the compiler options you listed above and it catches the unused variable, but misses the assignment vs. comparison issue (on a C++ program using g++. I realize your example is C with gcc). Ditto with the W4 option, so either I'm doing something wrong or these flags are not equivalent between Dev C++ and Visual C++. Anyway, I'll play around with this some more and research some of the differences, but I imagine there is a way to do it. You've given me some good leads, so thank you for that.

well, if (a=b) { . . . } is perfectly valid, as im sure youre aware.

i used to purposely code in that manner, before i got the habit slapped out of me by a principal engineer who (god forbid!) insisted code be readable. :P i mean, not in such a trivial way, but more like the convention: if (err = myFunction()) { . . . } it's not uncommon. i'm not saying it's good style, but i see people code like that a lot.


.

Yes, it's valid C++ code, so I wouldn't want or expect a compiler flag to flag it as an error. In this particular case, it was an error in that the intention was not to make an assignment, but instead to make a comparison. I caught it when I went through the code line by line, but it would be nice to have a flag I could turn on and off (usually off) to do some of the work for me and point me potentially in the right direction with a warning. Such a flag exists and so far I've been 2 for 3 (Dev C++ gcc and Visual C++) in finding it and implementing it. The remaining failure is making it work for C++ program in Dev C++.

compilers check snytaxs only. It's like grammar check, thier may not be a problem with what code you wrote...but is it logical for the program?
checking it as to be done form the programar, or else the computer does what is written and not what it should do.

A reference for the options
http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Warning-Options.html#Warning-Options

Thanks for the link. I'm definitely going to bookmark it. It'll be useful.

compilers check snytaxs only. It's like grammar check, thier may not be a problem with what code you wrote...but is it logical for the program?
checking it as to be done form the programar, or else the computer does what is written and not what it should do.

Compilers, given different flags, will behave differently, or at least give different output. It "caught" that "error" when I set my Visual Studio project at the W4 level. The compiler doesn't know whether the = is an error or not ("error" being defined here as a mistake on the coder's part. Legal C++ code, but not the coder's intent), but it knows that it "might be" an error so it throws you a warning but still compiles. It's the coder's job to decide whether that warning is meaningful to his/her particular code. In the program I was debugging, it was very meaningful (but alas, at the time, I didn't know what flag to set, so I didn't get the warning and I had to find it myself. :'( Now I know how to set the flag in the future so maybe I'll spot it more quickly. :cool:

Thanks for all the replies, everyone. I just saw my post count and I'm at 999, so when I hit the "submit Reply" button......I know I get something for 1000. I'm really hoping it's a new car because the one I have now is just not impressing the ladies. Or maybe it's me... Nah, it's the car.

commented: You get a nice gold star and a green cookie :) +17

Yes. The = operator is valid so dev c++ will not flag it as an error. I personally uses dev c++ too and i tried it. I suppose that you will just have to see if your program work according to what you want. If not then look for that error. I am not sure if there is any better method to check the error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.