Thread: Operator Assignment ??

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Question Operator Assignment ??

    Can anyone please explain the following code:
    Code:
    #include<stdio.h>
    void main ()
    {
    int m = m <= m > 0, n = --m;
    n = n++ && m-12;
    printf("%d %d %d", m,n, ++m - --n);
    }
    The output is : 1 -1 2
    I want to understand how the above code works.
    Can anyone explain how the operator assignment takes place?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where did you get this from? It is unnecessarily convoluted and results in undefined behaviour.
    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

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Where did you get this from? It is unnecessarily convoluted and results in undefined behaviour.
    It's a problem from the book on testing C aptitude....
    Can u plz tell how it works.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nowshek
    It's a problem from the book on testing C aptitude....
    Can u plz tell how it works.
    It doesn't work. At least, if I were interviewing a person and decided to see if the person had some aptitude in C by showing the candidate this code, anything less than a shriek of disgust from the candidate would make me doubt that the person knows C.

    The first line of main is a total mess that may result in undefined behaviour, but I don't know for sure. The second line is okay because of the sequence point introduced by &&, but it is still dumb. Anyway, I am certain that this line results in undefined behaviour:
    Code:
    printf("%d %d %d", m,n, ++m - --n);
    m and n are accessed, but at the same time they are modified by side effects.
    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

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The behaviour, as laserlight said, is undefined.

    The word "undefined" in the C standard essentially means that "how it works" can be anything, from reformatting your hard drive, displaying a cartoon of little green mice chasing spacemen on your screen, or printing the value 42 over and over.

    Any code that modifies a variable twice between sequence points - which is what your code does - has undefined behaviour.

    And main() returns int, not void.

    If you want to demonstrate C aptitude, learn the meaning of "undefined" as it is defined in the C standard, and learn that main() returns int.
    Last edited by grumpy; 11-18-2011 at 02:59 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Quote Originally Posted by grumpy View Post
    The behaviour, as laserlight said, is undefined.

    The word "undefined" in the C standard essentially means that "how it works" can be anything, from reformatting your hard drive, displaying a cartoon of little green mice chasing spacemen on your screen, or printing the value 42 over and over.

    Any code that modifies a variable twice between sequence points - which is what your code does - has undefined behaviour.

    And main() returns int, not void.

    If you want to demonstrate C aptitude, learn the meaning of "undefined" as it is defined in the C standard, and learn that main() returns int.
    but the following code compiles and runs without error............

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nowshek
    but the following code compiles and runs without error............
    That is one possible outcome of undefined behaviour.

    Suppose you murder your neighbour in cold blood. You are never caught. Does that mean that it was legal for you to murder your neighbour?
    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
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Quote Originally Posted by laserlight View Post
    That is one possible outcome of undefined behaviour.

    Suppose you murder your neighbour in cold blood. You are never caught. Does that mean that it was legal for you to murder your neighbour?
    if it is undefined behaviour then the output of the code must change...but it give 1 -1 2.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nowshek
    if it is undefined behaviour then the output of the code must change...
    That is not true. It is entirely possible that given a piece of code that results in undefined behaviour, every current compiler with every possible configuration produces the same behaviour... until one of the compilers has a new release, or a new player enters the market. Or, the compiler that you use always compiles your program to produce the behaviour that you have come to expect, except when you compile your program on Friday the 13th.
    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

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nowshek View Post
    if it is undefined behaviour then the output of the code must change...but it give 1 -1 2.
    No, that would be erratic behaviour.

    Undefined behaviour means that it may not do what you expect... but in a given compiler/OS it usually surprises you in the same way each time.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    produce the behaviour that you have come to expect, except when you compile your program on Friday the 13th.
    Or when you're attempting to demonstrate it to your Boss...

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    For some extra information on undefined behavior (and unspecified and implementation-defined), read this: Question 11.33.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    No, that would be erratic behaviour.

    Undefined behaviour means that it may not do what you expect... but in a given compiler/OS it usually surprises you in the same way each time.
    One potential manifestation of undefined behaviour is erratic behaviour. Ever tried to track down a bug in a function that increments a static pointer twice in a statement, and only yields an abnormal termination - in other code that dereferences that pointer - only after the third time it is called?

    To quote the 1999 C standard Undefined behavior is "behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements". (the American origin of the standard is obvious in the spelling it uses).

    The standard, immediately goes on in the next clause with this: "NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message)."
    Last edited by grumpy; 11-18-2011 at 02:53 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by grumpy View Post
    .... displaying a cartoon of little green mice chasing spacemen on your screen, or printing the value 42 over and over.
    If I ever write a compiler for educational purposes or otherwise.... I'll make a point by doing all of those...and some more creatively defined behaviours.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Just as well I used the word "chasing" in my previous post rather than the first word that came to mind .......
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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