Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Odd C code

Options
  • 11-02-2006 1:24am
    #1
    Registered Users Posts: 1,275 ✭✭✭


    Anyone any idea what this syntax does?
    int k = i+++j;
    


Comments

  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    If it worked, I'd imagine it would make k equal to i plus 1 plus j.


  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    Of course it works: just curious to see how many folk know or can deduce what it does.


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    i don't believe it works.


  • Registered Users Posts: 4,188 ✭✭✭pH


    In all fairness it's not as odd as :
    #include <stdio.h>
    main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
    main(-86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
    main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
    "@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#\
    ;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
    q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
    ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
    iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
    ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
    }'+}##(!!/")
    :t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
      :0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
    "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
    


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    and what does that do?
    re-establish the codes proxy?


  • Advertisement
  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    Sandals wrote:
    and what does that do?
    re-establish the codes proxy?

    Nah - looks familiar. Is it the Christmas one?


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Right, I'll be able to tell you once I look up C's operator precedence. Specifically if the evaluation pre/post-increment comes before or after addition.

    Ok post increment is evaluated first over pre-increment.

    So the token +++ would be split into two tokens, ++ and + (which is why I wanted to know the precedence - see references)

    So that will evaluate to:
    k= (i++) + j;
    

    Which basically should be equivalent to the following statments
    k = i + j;
    i++;
    

    Check it out. assign some values to i and j, and then check out what the statment does. If I'm right (I'm too tired/not-arsed to compile it myself just now)
    if you do
    int i = 1;
    int j = 1;
    int k = i+++j;
    
    if((k == 2) && (i == 2) && (j==1)){
      printf("I got it right. It's i post-increment plus j\n");
    } else if ((k == 3) && (i==1) && (j==2)){
      // This shouldn't happen, but covers the possibility that it pre-increments j, so k would be i + j + 1, and j would be j+1. But pre-increment has a lower precedence than post-increment, so it really shouldn't happen if your C implementation is good.
      printf("I got it wrong - it's i plus preincrement j\n");
    } else {
      printf("Ok, that totally messed up.\n\nThe real values are: i= %d, j=%d, k=%d\n", i, j, k);
    }
    

    I'd say that snippet of code would give you a thorough explanation of what it does.

    If I remember to, I'll try it out in the morning. By the looks of things, you should get i=2, j=1 and k=2.

    References:
    I got the precedence chart by googling "operator precedence c". I got http://www.swansontec.com/sopc.htm

    Take care,
    Aoife


  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    You're right, but for the wrong reasons. The selection of (i++) + j over i + (++j) isn't a question of precedence, but rather about tokenising the line: in this case (as is usual in compilers) the compiler takes the longest string possible, when scanning from left to right, and this is "i++".

    The issue of precedence only arises once you have actually identified the operators.


Advertisement