what print?void main() { int i=0; i=i-- --2; print("i=",i); }

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ashish8
    New Member
    • Feb 2014
    • 1

    what print?void main() { int i=0; i=i-- --2; print("i=",i); }

    Code:
    void main()
    {
    int i=0;
    i=i-- --2;
    print("i=",i);
    }
    Last edited by Banfa; Feb 5 '14, 03:35 PM. Reason: Added code tags round your code
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Main returns int not void, print is not a standard library function and you have not included and headers.

    However assuming that you fixed all that, including changing the printf format string to "i=%d" then line 4 wont compile because the operator-- must be applied to an lvalue but 2 is not an lvalue.

    Assuming you change line 4 to i=i-- - -2; i.e. take away minus 2 then you have attempted to modify the value of the variable i twice between sequence points so the expression invokes undefined behaviour and anything could be printed, or nothing, or whatever you think the right answer is.

    Comment

    Working...