Gilby check your post messages from me. I have a couple questions for you.
PM != Post Messages
PM = Private Messages
(:
Okay private messages,
Syntax or logic error
Since you’re using C style operators
!= is a logical operation
= is an assignment operation
To keep everything logical you need
PM == Private Messages
would that actually be a syntax error? (too lazy to bother writing code and compiling it right now) while it doesn’t make any sense, there is nothing wrong with asigning something there. The following is legal syntax:
if (x = 5) do_something();
it essentially means, if 5 can be assigned to x, then do_something(), while it is not usefull, and you would never want to do that, it is legal syntax.
if this were c programming, the problem would be missing semicolons, not using the wrong operator. see, your not the only C programming nerd on the forums
not meaning to offend you by calling you a nerd
It all depends on what flavor of a C language you’re using.
Regular C and C++ will allow you to put an assignment operation where the logical expression should go in an if statement. C and C++ won’t complain. No syntax error. However, doing so is almost always a logic error (a bug).
Some other C-like languages don’t allow you to put an assignment operation where a logical expression is expected. To do so would cause a syntax error. C# and Java won’t allow that sort of mistake and will give you a syntax error.
That’s why I stated that you’d either end up with a syntax error or a logic error.
In C and C++ the result of an assignment operation is the value that gets assigned. So in the case of (x=5) the result is 5. For the case of (x=0) the result is 0.
In C and C++ a value of 0 is false while anything else is true.
So the following statement would evaluate to true and the function do_something() would be called
if (x=5) do_something();
The following statement would evaluate to false and do_something() would not be called
if (x=0) do_something();
C and C++ are evil that way. Errors like that can be hard to catch and the compiler won’t flag them as syntax errors.
For that reason some C and C++ programmers get in the habit of reversing their comparison operations so that if they mistype a = instead of == they will get a syntax error. For example the following will generate a syntax error:
if (5=x) do_something();
The following is the safe way to do the comparison:
if (5==x) do_something();
Writing (5==x) looks ass-backwards but is the safe way to write a logical expression in C or C++. That way if you mistype a = instead of == or != or <= or >= you will get a syntax error instead of an accidental assignment and a logic error (bug) in the program.
Wow, I feel so much smarter now! (OK, no I don’t cause I don’t have the slightest clue what you’re talking about!)
Okay okay, give me a break, I have only been programming in C of any kind for about a week, and have only tried tradition C (or pretty close to it). I thought when you said logic error, you meant the compiler would say “logic error, invalid operator in line 5” or something like that, when you really only meant it was a bug. And yeah, your right about how it works, I guess I forgot about that :(. I think at least some compilers will flag it as a possible problem, but not an actual error. And maybe in some strange case, you would want to do that
but it would be a very bad idea
pong
I gave you the nerd explanation. No offense.
If you bump up the compiler warning levels to maximum you should get a warning from the compiler that you’re doing something bad on that line.
If I bump up the compiler warnings for the MS Visual C++ compiler I get the following warning for that line:
warning C4706: assignment within conditional expression
With the default compiler settings I get no warning for that line.
Pay attention to compiler warnings. They can alert you to something that you are doing wrong. Bump up the compiler warnings to maximum and see if your program still compiles cleanly. If you get warnings make sure you know why each warning was made and whether it’s an issue or not.
Try compiling a little program with that line at various warning levels and see what warnings your compiler gives you.
(Personal bugbear) …or they can just annoy you no end when you’ve got a raft full of singleton classes and it’s whinging about private constructors. Yes, I know, dammit, that’s the idea!
On the other hand, I like the sound of a compiler that warns you about logic errors. I could do with one of those… how much time would that save? Yeah…
Phil
Shhhh, don’t tell him that warnings are annoying. He’s learning C and needs the warnings.