Thread: Need Some explanation for this program output

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Need Some explanation for this program output

    Hi,

    I am returning to C programs after a long time and i have confusion why 3rd output is 31! Remembering concepts of precedence and association i think i am either overlooking something or it may be undefined behavior. But i am 70% sure that it may not be come under criteria of undefined behavior and this is why posting it here to understand it.

    Code:
    If p = 10 then
    (++p) + p + p = 33
    p + (++p) + p = 33
    p + p + (++p) = 31
    p + p * (++p) = 132
    S_ccess is waiting for u. Go Ahead, put u there.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm pretty sure all that nonsense results in undefined behaviour.
    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.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I'm pretty sure I agree with Salem

    Edit: gcc even warns you about it :/

    Code:
    #include <stdio.h>
    
    int main(void)
    {    
        int p = 10;
        int a, b, c, d;
        
        a = (++p) + p + p;
        b = p + (++p) + p;
        c = p + p + (++p);
        d = p + p * (++p);
        
        return 0;
    }
    Code:
    dumb.c: In function ‘main’:
    dumb.c:8:10: warning: operation on ‘p’ may be undefined [-Wsequence-point]
        8 |     a = (++p) + p + p;
          |         ~^~~~
    dumb.c:8:10: warning: operation on ‘p’ may be undefined [-Wsequence-point]
    dumb.c:9:14: warning: operation on ‘p’ may be undefined [-Wsequence-point]
        9 |     b = p + (++p) + p;
          |             ~^~~~
    dumb.c:9:14: warning: operation on ‘p’ may be undefined [-Wsequence-point]
    dumb.c:10:18: warning: operation on ‘p’ may be undefined [-Wsequence-point]
       10 |     c = p + p + (++p);
          |                 ~^~~~
    dumb.c:11:18: warning: operation on ‘p’ may be undefined [-Wsequence-point]
       11 |     d = p + p * (++p);
          |                 ~^~~~
    dumb.c:11:18: warning: operation on ‘p’ may be undefined [-Wsequence-point]
    Last edited by Hodor; 10-11-2019 at 11:23 PM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Ok. Understood. Thanks a lot guys for clearing out my confusion.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer output explanation
    By freakZ in forum C Programming
    Replies: 1
    Last Post: 06-04-2016, 11:08 AM
  2. Explanation for the Output
    By soton1986 in forum C Programming
    Replies: 4
    Last Post: 04-09-2015, 12:29 AM
  3. Program explanation.
    By nullifyed in forum C Programming
    Replies: 3
    Last Post: 06-18-2010, 02:29 AM
  4. Fork and wait. need output explanation
    By ollie88r in forum C Programming
    Replies: 18
    Last Post: 11-03-2009, 09:26 AM
  5. Output Explanation
    By anirban in forum C Programming
    Replies: 8
    Last Post: 01-20-2009, 07:10 AM

Tags for this Thread