none
Basic Programming Problem RRS feed

  • Question

  • Hi, Can anyone please explain me the result of

    #include<stdio.h>

    int a=2;

    a=a++ + ~++a;

    printf("%d",a);

    return 0;

    Friday, November 1, 2013 6:57 AM

Answers

  • Well, I've stated that I've used Java instead of C. And after compiling the program in C, I've spotted many discrepancies between C & Java related to post-incremental operator!

    Code compiled w/ GCC 4.7.3:

    #include<stdio.h>

    int main() {

      int a, b;

      //*******************************************//
      a = 2;
      a = a++ + ~++a;
      printf("%d\n", a);  // C -> 0;   Java -> -3
      //*******************************************//
      a = 2;
      b = a++ + ~++a;
      printf("%d\n", b);  // C -> -1;  Java -> -3
      //*******************************************//
      a = 2;
      b = a++;
      printf("%d\n", b);  // C -> 2;   Java -> 2
      //*******************************************//
      a = 2;

      a = a++;
      printf("%d\n", a);  // C -> 3;   Java -> 2

      a += ~++a;
      printf("%d\n", a);  // C -> -1;  Java -> -2
      //*******************************************//

      return 0;
    }

    Even for something as simple as a = a++; C gives out different result from that of Java's!

    Seems like unary post-incremental operator delays itself much further. Even after left-side a is assigned the result from the expression to its right!

    And if a different variable receives the right-side total result like b = a++; we got ourselves the expected value in b.

    And this time, it is the same value we got in Java! Although in Java, it doesn't matter if we use the same or a different variable as the receiver though! It is consistent in all cases!!!

    For the full expression -> a = a++ + ~++a; we got another oddity! In C, seems like the unary operator ~ happens before the unary pre-increment operator ++!!!

    Or maybe the pre-incremental from the right term happened before the post-incremental from the left term!!!  @_@

    I feel safe about using them in Java. But if you use those unary operators in C/C++/D, at least use another variable to store the total result. Even so, be careful!  :P

    And perhaps some1 should test that out in C# and JavaScript to see how they pan out as well!  :D

    P.S.: JavaScript matches Java alright!  \^-^/


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Sunday, November 3, 2013 12:24 AM
    Answerer

All replies

  • This sub-forum is for the Small Basic programming language, not for C and its derivatives!  ;-)

    Anyways, since that is relatively easy to respond, I'll try my best!  :D

    The expression -> a++ + ~++a; is mostly made outta unary operators. Take a look at this link -> Unary_operation.

    Term a++ uses the post-increment operator, while the other uses the pre-increment 1. Article link -> Increment & decrement operators.

    Pre-increment is the easiest to understand. It merely increases by 1 the value of a variable as we expect so.

    Now the post-increment is almost the same, but the assignment is delayered. Lemme explain it better:

    It also increases a variable by 1, but spits out its previous value instead!  @_@

    So, if a = 2, a++ still evaluates as 2! But soon after that, a = 3, as we expect it. It's pretty exotic indeed!  *_*

    After that, we gotta deal w/ the so-called one's compliment unary operator ~ -> ~ operator.

    Internally, it inverts all the bits of a value. Externally for us, its effect is the inversion of a (value+1).

    Let's say that a = 4 now, so its 1 complement is -> -(4 + 1) -> -5.

    Well, I guess you're ready to understand how all of that pans out now. Check my step-by-step comments below:

    Warning: I've used Java to test that out, not C!

    #include<stdio.h>
    
    int a = 2;
    
    a = a++ + ~++a;
    // 2 + ~4
    // 2 + -(4 + 1)
    // 2 + -(5)
    // -3
    
    printf("%d", a);
    
    return 0;


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Friday, November 1, 2013 8:33 AM
    Answerer
  • Thank You so much for your response. By the by, i will definitely post my concerns related to C programming in the apposite forum from next time onwards. But i would like to note you in this regard that the answer to this problem as provided was -1, which was in no par with my calculations. If possible, please elucidate why!

    Regards,

    Sumana Sil



    • Edited by Sumana Sil Saturday, November 2, 2013 6:02 AM
    Saturday, November 2, 2013 6:02 AM
  • Well, I've stated that I've used Java instead of C. And after compiling the program in C, I've spotted many discrepancies between C & Java related to post-incremental operator!

    Code compiled w/ GCC 4.7.3:

    #include<stdio.h>

    int main() {

      int a, b;

      //*******************************************//
      a = 2;
      a = a++ + ~++a;
      printf("%d\n", a);  // C -> 0;   Java -> -3
      //*******************************************//
      a = 2;
      b = a++ + ~++a;
      printf("%d\n", b);  // C -> -1;  Java -> -3
      //*******************************************//
      a = 2;
      b = a++;
      printf("%d\n", b);  // C -> 2;   Java -> 2
      //*******************************************//
      a = 2;

      a = a++;
      printf("%d\n", a);  // C -> 3;   Java -> 2

      a += ~++a;
      printf("%d\n", a);  // C -> -1;  Java -> -2
      //*******************************************//

      return 0;
    }

    Even for something as simple as a = a++; C gives out different result from that of Java's!

    Seems like unary post-incremental operator delays itself much further. Even after left-side a is assigned the result from the expression to its right!

    And if a different variable receives the right-side total result like b = a++; we got ourselves the expected value in b.

    And this time, it is the same value we got in Java! Although in Java, it doesn't matter if we use the same or a different variable as the receiver though! It is consistent in all cases!!!

    For the full expression -> a = a++ + ~++a; we got another oddity! In C, seems like the unary operator ~ happens before the unary pre-increment operator ++!!!

    Or maybe the pre-incremental from the right term happened before the post-incremental from the left term!!!  @_@

    I feel safe about using them in Java. But if you use those unary operators in C/C++/D, at least use another variable to store the total result. Even so, be careful!  :P

    And perhaps some1 should test that out in C# and JavaScript to see how they pan out as well!  :D

    P.S.: JavaScript matches Java alright!  \^-^/


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Sunday, November 3, 2013 12:24 AM
    Answerer
  • Thanks for your response! :)
    Sunday, November 3, 2013 9:09 AM