Thread: Please explain me this concept!!

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Please explain me this concept!!

    Here is a code

    int i=7;
    printf("%d\n", i++*i++);

    this gives the output 56.
    I have learned that increment or decrement is performed immediately after giving up the previous value and before any other part of the expression is evaluated.And i know that post increment means assign the value first and then increment.
    But how exactly is the output calculated??? What is sequence point and what does it has to do with this code???
    Please explain.
    Last edited by dala; 09-26-2012 at 08:50 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Implementation defined

    EDIT: Ups, not the information I wanted it to be...

    EDIT^2 : Maybe this one?
    Last edited by GReaper; 09-26-2012 at 08:50 AM.
    Devoted my life to programming...

  3. #3

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Thank u so much for the link , it was very helpful.

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Short answer: Never do this in real-life code.

    It's nothing more than an intellectual exercise in understanding undefined behaviour.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Given the frequency with which we see this question being asked, I'm not so sure it really *is* about understanding undefined behavior. I think they're actually teaching this as valid code, expecting the answer to be whatever Turbo Crap gives them.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rags_to_riches View Post
    Given the frequency with which we see this question being asked, I'm not so sure it really *is* about understanding undefined behavior. I think they're actually teaching this as valid code, expecting the answer to be whatever Turbo Crap gives them.
    I disagree. I've lost count of the number of people I've seen asking a variation of this question, and have seen no correlation between those people and their choice of compiler.

    Sometimes it a case learning to use expressions like "z = a++ + ++b;" (i.e. multiple variables incremented in one expression) safely, so they assume they can get away with incrementing a variable twice, such as "z = a++ + ++a;". And they find they don't get the result they expect.

    I have also seen plenty of cases, like the OP in this case, giving some relevant words ("sequence point", "undefined", etc) which implies that someone has already tried to explain (or teach) what is going on.


    I think this comes up a lot because people are people. Some make incorrect leaps of judgement (eg if I can increment two variables in an expression, I can increment one variable twice. Oh, bugger, why isn't it doing what I expect?). Some don't quite grasp what their teacher or textbook is telling them (teacher says this is "undefined" but it produces the value 56 rather than 58 - huh??) - which can be due to poor teaching, to inattention by the student, or simply due to a human misunderstanding.

    The thing is, once you know this type of thing, it's pretty easy to understand (unless you try to write obscure one-liners for obfuscated C contests). But, when you first learn, there are a lot of subtleties. So even the best students can misunderstand at first.
    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.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If your "tutor" claims to know "the answer", then feel free to show them this experiment with several compilers and ask them to explain all the results.

    And by explain, I mean to the extent that you can then compile the code with the "code-masher 4.5 C compiler" and be able to predict the output BEFORE running the code.

    You see, with well-formed code which has no undefined behaviour, you can describe what will happen without having to run the code.

    With the code you posted - you're screwed before you begin.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain this concept to me
    By sigur47 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2012, 07:35 AM
  2. concept of buffering in c
    By deepaklearner in forum C Programming
    Replies: 2
    Last Post: 06-08-2011, 12:21 PM
  3. Concept
    By kusal in forum C Programming
    Replies: 12
    Last Post: 01-06-2007, 10:32 PM
  4. Concept help
    By Mithoric in forum Windows Programming
    Replies: 13
    Last Post: 04-18-2004, 03:05 PM
  5. File I/O - Concept
    By MethodMan in forum C Programming
    Replies: 0
    Last Post: 03-02-2003, 12:07 PM