• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

gcc warning problem

I just upgraded to Redhat 6.1 from an earlier slackware version, and am getting this warning (which I did not get previously) when compiling:
warning: suggest explicit braces to avoid ambiguous 'else'

Its not a huge, deal but I'd like to not see those, and man gcc isnt helping me at all... I'm using -Wall as the compile option, because all the other warnings I want to see, but this one... no... Anyone know?
0
edskee
Asked:
edskee
1 Solution
 
hansendcCommented:
Could you post a copy of the offending code so we can see if it is really a problem, or the compiler's problem?
0
 
edskeeAuthor Commented:
The code looks like this:

if(blahblah)
  if(something_else) {     // this is the line
    fa la la;
    x = 5;
    blahblah = blah;
  } else
    fa la la;

However there IS no problem with the code. It's always worked. Its good code. The problem is, this RedHat version of gcc gives that warning, while my older slackware version of gcc didnt.

I need a gcc -flag to turn off that warning :)
0
 
freshmeatCommented:
only a warning, just ignore it
or it is a small bug of gcc???
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
edskeeAuthor Commented:
Nice bit of advice there, sheesh. Its not a bug, and the REASON I posted this was because I wanted to find out how to remove this. Guess I am a freaking moron, and ignoring the warning never occured to me. Dont bother placing comments if they arent meant to help.
0
 
graham_kCommented:
well, I personally *NEVER* *EVER* allow an unbracketed single statement in my code (I mean - after IF, ELSE, FOR, WHILE, etc). There's just too much danger that you'll add an extra line & forget to add braces. The typical case is adding a quick printf() for debugging and not adding braces. Then your 'else' clause is always executed.

Some companies for which I have contracted have had this in their coding standards. Maybe you want to do it too?  Believe me, it may not be required, it may sound silly, but the first time that you make such a mistake & take 3 days to track it down ...

And, I _know_ that this isn't the answer for which you are looking, but pelase don't shout at me, I do mean well :-)
0
 
hansendcCommented:
This is definitely not a compiler problem.   Gcc probably just had a bug that didn't properly detect malformed syntax like this.  

Here is the problem:  Does your code mean this:
if(blahblah)
  if(something_else)
   {
      blahblah = blah;
   }
   else
      fa la la;

OR
if(blahblah)
  if(something_else)
   {
      blahblah = blah;
   }
else
  fa la la;

Which if does the else belong to?  This is why every beginning programmer is taught to use braces whenever possible.  This is also a common topic touched upon in compiler classes.
0
 
edskeeAuthor Commented:
How the heck is that an answer? The answer I am looking for is a GCC FLAG TO TURN OFF THAT WARNING. I've been coding this program for 4 years now, I understand the 'danger' of not using brackets, however in my personal coding style, on one line if, for's, etc, I dont like using the brackets.

If no one does know the answer, then fine, but dont propose an answer which has nothing to do with my original question. I KNOW it's a warning, I know it's a warning I am supposed to see, and I know you can turn off most other warnings. All I wanted to know is if it were possible to turn off this one, not a lecture on my coding sytle and the dangers of sloppy bracketing.

I would change it (add the brackets) but I have over 400 instances of this warning spread out over 40+ files. I dont have the three hours to scan 150,000+ lines of code add brackets to otherwise good code.

As for ths 'is it example A or B', it's example A. The else always follows the last bracket used. If it were to be example B, you'd need {}'s around the first if used.
0
 
edskeeAuthor Commented:
I'm sorry if I sound billigerent or aggressive or whatnot, but many times I have asked questions on this site, only to have comments and opinions having nothing to do with my original question offered as answers, and its frustrating.

Q: How do I make ProgramA perform this function?

Proposed answer: Why would you want ProgramA to do that? Use ProgramC, it's easier.

That isnt an answer to the question I asked... if I wanted to use ProgramC, I wouldnt have asked in the first place.

I know I could add in the brackets, or take out the -Wall option, but thats not what I asked.
0
 
hansendcCommented:
Answer:
You can't do that with this version of gcc.  I have looked through it pretty extensively and asked some people I know.  Its just a warning.  Have you ever done a kernel compile?  There are warnings all over the place.  Just ignore it.  However, there is still the problem of who the else belongs to.  I would be worried about that if I were you.
0
 
edskeeAuthor Commented:
Nothing to worry about, I wrote the code, I know how it works, I coded it that way intentionally. The compiler does not pick which if the else belongs to arbitrarily.
0
 
hansendcCommented:
So there was a reason for your ambiguity?  I'd love to hear this one.
0
 
edskeeAuthor Commented:
My ambiguity? I stated, quite clearly, what I was looking for and why.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now