Did you know?

  • Thread starter Vijay Kumar R Zanvar
  • Start date
V

Vijay Kumar R Zanvar

Did you know that the following if statement is semantically
right, but could logically be wrong?

if ( i = 2 )
{
/* do something */
}

Tip:

The compiler may only produce a waring: "Possibly incorrect
assignment", but we may ignore it. To avoid such a mistake,
just reverse the two identifiers.

if ( 2 == i )
{
/* do something */
}

If '==' is replaced by an '=', the compiler will give an error
saying "Lvalue required".
 
J

Joona I Palaste

Vijay Kumar R Zanvar said:
Did you know that the following if statement is semantically
right, but could logically be wrong?
if ( i = 2 )
{
/* do something */
}

The compiler may only produce a waring: "Possibly incorrect
assignment", but we may ignore it. To avoid such a mistake,
just reverse the two identifiers.
if ( 2 == i )
{
/* do something */
}
If '==' is replaced by an '=', the compiler will give an error
saying "Lvalue required".

Yes, we did know all this. But thanks for letting us know anyway.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
 
V

Vijay Kumar R Zanvar

Joona I Palaste said:
Vijay Kumar R Zanvar <[email protected]> scribbled the following:

^^^^^^^^

Did I scribble?

Did you know that the following if statement is semantically
right, but could logically be wrong?
[...]
If '==' is replaced by an '=', the compiler will give an error
saying "Lvalue required".

Yes, we did know all this. But thanks for letting us know anyway.

I suppose it would be fun working with you, Mr. Palaste. You must be a
comedian!!
 
N

Noah Roberts

Vijay said:
if ( 2 == i )
{
/* do something */
}

What if 2 != i but i == 2, ever thought of that?

BTW, did you know that the above code doesn't do anything even if 2 ==
i? I wonder what happens if i == 2 though...

NR
 
G

Goran Larsson

Vijay Kumar R Zanvar said:
Did you know that the following if statement is semantically
right, but could logically be wrong?

if ( i = 2 )
{
/* do something */
}

Did you know that the following if statement is semantically
right, but could logically be wrong?

if ( i = k )
{
/* do something */
}
 
R

Robert Stankowic

Noah Roberts said:
What if 2 != i but i == 2, ever thought of that?

BTW, did you know that the above code doesn't do anything even if 2 ==
i? I wonder what happens if i == 2 though...

What, if you replace "==" with "is equal to" as well as "!=" with "is
unequal to" and "i" with "the value contained in a variable named i" ?
Robert
 
R

Ravi

Did you know that the following if statement is semantically
right, but could logically be wrong?

if ( i = k )
{
/* do something */
}

On second thoughts it could be correct. If you want the loop
to execute with i assigned to the value of k only when k is
not zero you might as well use that form.

Sorter than:
if (k!=0)
{
i=k;
/* do everything ;)*/
}
 
A

Al Bowers

Vijay said:
Vijay Kumar R Zanvar <[email protected]> scribbled the following:


^^^^^^^^

Did I scribble?


Did you know that the following if statement is semantically
right, but could logically be wrong?

[...]

If '==' is replaced by an '=', the compiler will give an error
saying "Lvalue required".

Yes, we did know all this. But thanks for letting us know anyway.


I suppose it would be fun working with you, Mr. Palaste. You must be a
comedian!!

Perhaps dry humor given the fact that everyone should know
this if they read the FAG.
See question 17.4
http://www.eskimo.com/~scs/C-faq/q17.4.html
 
R

Richard Bos

Vijay Kumar R Zanvar said:
Tip:

The compiler may only produce a waring: "Possibly incorrect
assignment", but we may ignore it. To avoid such a mistake,
just reverse the two identifiers.

if ( 2 == i )
{
/* do something */
}

Did you know that this is a stupid trick to rely on, because it makes
the code less clear, doesn't help you in all too many situations, and
therefore _will_ bite you sooner or later? At least the real solution,
which is being aware of the problem, will help you debug your code when
you _do_ make this mistake.

Richard
 
M

Mike Wahler

Vijay Kumar R Zanvar said:
Did you know

Of course we know.
that the following if statement is semantically
right, but could logically be wrong?

if ( i = 2 )
{
/* do something */
}

Tip:

The compiler may only produce a waring: "Possibly incorrect
assignment", but we may ignore it. To avoid such a mistake,
just reverse the two identifiers.

if ( 2 == i )
{
/* do something */
}

If '==' is replaced by an '=', the compiler will give an error
saying "Lvalue required".

This issue has been discussed many, many, many times
here, if you'd taken the time to check old posts.
Some folks use the 2 == i form, others don't.

Also note that sometimes the = (assignment) is indeed
what the coder wants as in e.g.

char *p;
if(p = malloc(100))
/* malloc succeeded */


-Mike
 
T

Tristan Miller

Greetings.

Vijay Kumar R said:
The compiler may only produce a waring: "Possibly incorrect
assignment", but we may ignore it. To avoid such a mistake,
just reverse the two identifiers.

Wouldn't a better way of avoiding such a mistake be not to ignore compiler
warnings in the first place?
 
A

Ashish

Vijay Kumar R Zanvar said:
Did you know that the following if statement is semantically
right, but could logically be wrong?

if ( i = 2 )
{
/* do something */
}

Tip:

The compiler may only produce a waring: "Possibly incorrect
assignment", but we may ignore it. To avoid such a mistake,
just reverse the two identifiers.

if ( 2 == i )
{
/* do something */
}

If '==' is replaced by an '=', the compiler will give an error
saying "Lvalue required".

Are you still living in the 19th century? This has been discussed millions
of times.
 
R

Richard Heathfield

Richard said:
Did you know that this is a stupid trick to rely on,

It's stupid to rely on it, but not stupid to use it.
because it makes
the code less clear,

The expression isn't unclear /at all/, and only an expert could have the
slightest doubt about it. :)
doesn't help you in all too many situations,

Not casting malloc doesn't help you in all too many situations, either (such
as in code where malloc is not used, for example). That doesn't mean I'm
about to start casting malloc, though.
and
therefore _will_ bite you sooner or later?

Nope. It doesn't bite, as long as you don't rely on it. It's just another
tool in the box.
At least the real solution,
which is being aware of the problem, will help you debug your code when
you _do_ make this mistake.

It is important to be aware of the problem. That doesn't mean you shouldn't
use the compiler to help you avoid it on occasion.
 
N

Neil Cerutti

Vijay said:
Vijay Kumar R Zanvar <[email protected]> scribbled the following:
^^^^^^^^

Did I scribble?
Did you know that the following if statement is semantically
right, but could logically be wrong?
[...]
Yes, we did know all this. But thanks for letting us know anyway.

--
I suppose it would be fun working with you, Mr. Palaste. You
must be a comedian!!

Perhaps dry humor given the fact that everyone should know
this if they read the FAG.

The what!?

Oh.

I find it's useful to write QAF backwards to how it's usually
written (Questions Asked Frequently) to avoid this type of
error. That way, if you miswrite G instead of Q, it will be
meaningless instead of sort of amusing. ;-)
 
M

Mike Wahler

Neil Cerutti said:
Vijay said:
Vijay Kumar R Zanvar <[email protected]> scribbled the following:
^^^^^^^^

Did I scribble?
Did you know that the following if statement is semantically
right, but could logically be wrong?

[...]
Yes, we did know all this. But thanks for letting us know anyway.

Perhaps dry humor given the fact that everyone should know
this if they read the FAG.

The what!?

Oh.

I find it's useful to write QAF backwards to how it's usually
written (Questions Asked Frequently) to avoid this type of
error. That way, if you miswrite G instead of Q, it will be
meaningless instead of sort of amusing. ;-)

It would be a gaffe (GAF) :)

-Mike
 
C

CBFalconer

Richard said:
Did you know that this is a stupid trick to rely on, because it
makes the code less clear, doesn't help you in all too many
situations, and therefore _will_ bite you sooner or later? At
least the real solution, which is being aware of the problem,
will help you debug your code when you _do_ make this mistake.

We have been around this before. Clarity is in the eye of the
beholder. I consider anything that catches silly errors
beneficial. This is an error that can escape code review very
easily.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top