Like Tree1Likes
  • 1 Post By iMalc

No Compile Error for if(x=1)

This is a discussion on No Compile Error for if(x=1) within the C++ Programming forums, part of the General Programming Boards category; I discovered that VC++ Express 2008 complies: if(x=1)doTask(); *Note: (x=1) not (x==1) Is there a way to get VC++ to ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    93

    No Compile Error for if(x=1)

    I discovered that VC++ Express 2008 complies:
    if(x=1)doTask();

    *Note: (x=1) not (x==1)

    Is there a way to get VC++ to give me a warning for this?
    And, why is this is allowed in the first place?
    Thanks

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by motocross1 View Post
    And, why is this is allowed in the first place?
    Because it is valid code...and can be useful if you want to put the result of a condition into a variable and test it for true/false at the same time.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    I'm not sure I understand, but do you mean...
    I can do math inside an IF and the IF is true when the math result is not 0 and false when the result is 0?

    And if this is what you mean, then...
    Shouldn't I test: if((x=y+z)!==FALSE) or if((x=y+z)===FALSE)
    Also, is this okay too: if(x++) and if(x+=3)

    I'm just curious to know.
    Not as if I will start using such code.
    I think it is ugly.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Make sure that your project is set to warning level 4. I believe that should pick it up.
    motocross1 likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by motocross1 View Post
    And, why is this is allowed in the first place?
    Thanks
    A typical use is:
    if ( (result = SomeFunction()) == SOME_VALUE )
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by motocross1 View Post
    I'm not sure I understand, but do you mean...
    I can do math inside an IF and the IF is true when the math result is not 0 and false when the result is 0?

    And if this is what you mean, then...
    Shouldn't I test: if((x=y+z)!==FALSE) or if((x=y+z)===FALSE)
    Also, is this okay too: if(x++) and if(x+=3)

    I'm just curious to know.
    Not as if I will start using such code.
    I think it is ugly.
    I meant... code like
    Code:
    if(someflag = test_condition() != some_value)
    ...
    is often used.
    It doesn't look too ugly to me .

    Edit: Just noticed Post #5 ...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    @manasij, you've forgotten a set of parens:
    Code:
    if((someflag = test_condition()) != some_value)
    But this isn't really the same as what the OP posted.
    The case of this:
    Code:
    if (x = a)
      ...
    is really just a case of this
    Code:
    if (b)
      ...
    In such a case, where no logical test is explicitly performed, it is interpreted as if it were written:
    Code:
    if (b != 0)
      ...
    
    if ((x = a) != 0)
      ...

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by oogabooga View Post
    @manasij, you've forgotten a set of parens:
    I thought they weren't necessary... as assignment has a lower precedence than the comparisons.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Quote Originally Posted by motocross1 View Post
    And, why is this is allowed in the first place?
    Because assignment operations yield a value.

    Apart from particular options with your compiler (iMalc mentioned warning level 4) you can also trigger warnings from such things by swapping left and right operands.

    This will not compile with any compiler
    Code:
    if (1 = x)
    but this will
    Code:
    if (1 == x)
    The weakness of this technique is that it only works if one of the operands is an rvalue (i.e. it cannot be on the left hand side of an assignment). So it won't catch
    Code:
    if (some_variable = another_variable) ....
    because the assignment is still valid.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by manasij7479 View Post
    I thought they weren't necessary... as assignment has a lower precedence than the comparisons.
    Sorry manasij, I thought you meant it the other way around (like Elysia's post).

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    iMalc,
    Thank you.

    Level 4 did it.
    And I actually found a semi-copycat post when Googling "VC++ warning level":
    VC++ General: How to avoid making an assignment when the intention is a comparison? - CodeGuru Forums

    Of course, WOW, the list of warnings is HUGE.
    I guess I'm about to really learn C++ now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. compile time error or runtime error?
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-07-2008, 08:08 AM
  3. 0 compile error but 2 build error. need help please!
    By jibbles in forum C Programming
    Replies: 5
    Last Post: 08-30-2004, 05:30 PM
  4. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 06:00 AM
  5. error C2061 - Compile Error
    By GaGi in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2002, 11:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21