CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29

Thread: if(a == b == c)

  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    You might be right. I think I had a bad day yesterday. It all actually started in another thread when people were treating me like some noob programmer... I guess there are quite a few of them here.
    That's not exactly what happened. You mentioned a crash and gave no code and no context. I'm not really sure what you expected.

  2. #17
    Join Date
    Dec 2010
    Posts
    20

    Re: if(a == b == c)

    parentheses are free!

  3. #18
    Join Date
    Feb 2002
    Posts
    4,640

    Re: if(a == b == c)

    Quote Originally Posted by deyili View Post
    parentheses are free!
    How does adding parentheses help?
    Code:
    if (a == (b == c))
    Evaluates to the false, just as before.

    Viggy

  4. #19
    Join Date
    Jun 2011
    Posts
    1

    Re: if(a == b == c)

    Wouldn't if((a==b) && (b==c)) work for a true outcome?

  5. #20
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: if(a == b == c)

    Quote Originally Posted by ElectricCash View Post
    Wouldn't if((a==b) && (b==c)) work for a true outcome?
    Yes, it would. That's how it should've been coded anyway.

  6. #21
    Join Date
    Feb 2002
    Posts
    4,640

    Re: if(a == b == c)

    Quote Originally Posted by ElectricCash View Post
    Wouldn't if((a==b) && (b==c)) work for a true outcome?
    Yes, but now you're changing the condition. You're not just adding parens.

    Viggy

  7. #22
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: if(a == b == c)

    ElectricCash, you see, the reason I started this thread is because it was somewhat confusing to me when I came across that double equation comparison statement. I have an experience of well over 15 years with C/C++ and I was thrown off by it, I should admit. What made it confusing for me is a mathematical notation (that involves the use of the AND operator in its logic) which is not the case for the way that statement is evaluated in C. Again, I'm not arguing with C/C++ standards -- we cannot change them from here -- but in my book a compiler should issue a warning when such statement is encountered, since clearly, if a, b and c are all int's comparing a boolean (which is the result of the first comparison) to an int, although possible, makes little sense. Again, this is my opinion and I learned my lesson.

  8. #23
    Join Date
    Feb 2002
    Posts
    4,640

    Re: if(a == b == c)

    Hmmm, yeah. I would have expected something like this:
    Code:
    1>d:\projects\junkconsole\test.cpp(15) : warning C4805: '==' : unsafe mix of type 'bool' and type 'int' in operation
    However, this warning only trips on this 'if' stmt:
    Code:
    bool res = a == b;
    if(res == c)
    {
       d++;
    }
    Viggy

  9. #24
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: if(a == b == c)

    I suspect that for legacy C compatibility reasons, primitive comparisons actually return int rather than bool.

  10. #25
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: if(a == b == c)

    Quote Originally Posted by Lindley View Post
    I suspect that for legacy C compatibility reasons, primitive comparisons actually return int rather than bool.
    I doubt that.
    Code:
    	int a = 0;
    	int b = 1;
    	int c = sizeof(a == b);		//VS2008: c = 1
    	int d = sizeof(int);		//VS2008: d = 4

  11. #26
    Join Date
    Apr 1999
    Posts
    27,449

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    I doubt that.
    Did you compile as a 'C' file or CPP? 'C' and C++ have different language rules, so I would suspect that the same code would produce sizeof(int) for the comparison.

    Regards,

    Paul McKenzie

  12. #27
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: if(a == b == c)

    Quote Originally Posted by Paul McKenzie View Post
    Did you compile as a 'C' file or CPP? 'C' and C++ have different language rules, so I would suspect that the same code would produce sizeof(int) for the comparison.
    Maybe. I'll let someone else try it out. I stopped using C... oh boy, 10+ years ago.... maybe more. The only reference to C I get these days is when dealing with Objective-C (OSX/iOS).

  13. #28
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    I doubt that.
    I'm not sure what the standard says about this. I'm just wondering about the reason for the particular compiler not throwing the warning.

  14. #29
    Join Date
    Feb 2002
    Posts
    4,640

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    Maybe. I'll let someone else try it out. I stopped using C... oh boy, 10+ years ago.... maybe more. The only reference to C I get these days is when dealing with Objective-C (OSX/iOS).
    There's a command line switch for the Visual Studio compiler to compile a particular source file as a 'C' file. Or, if on Linux, just use 'gcc' (instead of 'g++').

    Viggy

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured