|
-
June 12th, 2011, 12:18 PM
#16
Re: if(a == b == c)
 Originally Posted by ahmd
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.
-
June 15th, 2011, 10:19 PM
#17
-
June 16th, 2011, 09:47 AM
#18
Re: if(a == b == c)
 Originally Posted by deyili
parentheses are free!
How does adding parentheses help?
Evaluates to the false, just as before.
Viggy
-
June 18th, 2011, 09:05 PM
#19
Re: if(a == b == c)
Wouldn't if((a==b) && (b==c)) work for a true outcome?
-
June 18th, 2011, 10:26 PM
#20
Re: if(a == b == c)
 Originally Posted by ElectricCash
Wouldn't if((a==b) && (b==c)) work for a true outcome?
Yes, it would. That's how it should've been coded anyway.
-
June 20th, 2011, 11:26 AM
#21
Re: if(a == b == c)
 Originally Posted by ElectricCash
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
-
June 20th, 2011, 12:36 PM
#22
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.
-
June 20th, 2011, 02:21 PM
#23
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
-
June 21st, 2011, 10:23 AM
#24
Re: if(a == b == c)
I suspect that for legacy C compatibility reasons, primitive comparisons actually return int rather than bool.
-
June 21st, 2011, 11:57 AM
#25
Re: if(a == b == c)
 Originally Posted by Lindley
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
-
June 21st, 2011, 12:11 PM
#26
Re: if(a == b == c)
 Originally Posted by ahmd
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
-
June 21st, 2011, 01:08 PM
#27
Re: if(a == b == c)
 Originally Posted by Paul McKenzie
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).
-
June 21st, 2011, 01:21 PM
#28
Re: if(a == b == c)
 Originally Posted by ahmd
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.
-
June 21st, 2011, 01:52 PM
#29
Re: if(a == b == c)
 Originally Posted by ahmd
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|