Thread: Assignment operator in C

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    Lightbulb Assignment operator in C

    In which version of C was the problem of = and == solved?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You don't need to spam up our board with pointless and naïve questions. Are you trying to take a test or something?

    And what are you even talking about? There was never a problem with = and ==.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    What is your question specifically?

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    Sorry I misread the criticism section of C in wikipedia so the misconception.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    Look, I won't criticize you. Just please rephrase your question as it doesn't make any sense now.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by SrikanthSuresh View Post
    In which version of C was the problem of = and == solved?
    It is not version of C - it your writing style
    If you always write the condition in form
    Code:
    if(0 == i)
    then compiler will give you error when you by mistake miss the = char
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vart
    If you always write the condition in form
    Except that you cannot always write the condition in that way since you may be comparing two non-const lvalues... unless you're really suggesting always ensuring that the left hand side is an rvalue or non-const lvalue, but if you can always remember that, then you can always remember not to use = when you mean ==.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or you can remember to use compiler which actually warns about using assigning operator inside if
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It becomes a problem when I actually want to do an assignment and a boolean check at the same time. I wouldn't want future code maintainers to panic when those warnings pop up... possibly causing them to think I didn't know what I was doing.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    (1): I submit that if your "maintainers" don't understand the definition of "false positive" they shouldn't be maintaining anything.

    (2): If your company standard has such extremes as "no warnings allowed even false positives" you shouldn't be combining assignment with the check in the first place.

    Soma

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Heh, I was waiting for someone to tell me I was just coding wrong. Something like
    Code:
    if (fp = fopen(...)) {
        /* do good stuff since file open succeeded */
        }
    Compiler would whine telling me to dumb down my code. Oh well.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by nonoob View Post
    Heh, I was waiting for someone to tell me I was just coding wrong. Something like
    Code:
    if (fp = fopen(...)) {
        /* do good stuff since file open succeeded */
        }
    Compiler would whine telling me to dumb down my code. Oh well.
    The warning can usually be eliminated there by doing:
    Code:
    if ((fp = fopen(...)) != NULL) {
    Note that the extra brackets there are actually crucial to preventing the warning.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assignment operator
    By acosgaya in forum C++ Programming
    Replies: 9
    Last Post: 02-20-2011, 09:47 AM
  2. assignment operator
    By dudeomanodude in forum C++ Programming
    Replies: 25
    Last Post: 03-06-2008, 01:14 PM
  3. assignment operator... i think
    By beene in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2007, 04:10 PM
  4. Assignment operator help.
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2002, 10:15 PM
  5. Assignment operator
    By Claire in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2002, 04:08 AM