Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By grumpy
  • 1 Post By laserlight

Operator Assignment ??

This is a discussion on Operator Assignment ?? within the C Programming forums, part of the General Programming Boards category; Can anyone please explain the following code: Code: #include<stdio.h> void main () { int m = m <= m > ...

  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
    21,758
    Where did you get this from? It is unnecessarily convoluted and results in undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,758
    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.
    rags_to_riches likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    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 01:59 AM.
    rags_to_riches likes this.
    Right 98% of the time, and don't care about the other 3%.

  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
    21,758
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,758
    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.
    nowshek likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,498
    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,311
    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 01:53 PM.
    Right 98% of the time, and don't care about the other 3%.

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    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 !



  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    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%.

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, 08:47 AM
  2. assignment operator
    By dudeomanodude in forum C++ Programming
    Replies: 25
    Last Post: 03-06-2008, 12: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, 09:15 PM
  5. Assignment operator
    By Claire in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2002, 03:08 AM

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