Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Odd C code

  • 11-02-2006 12:24AM
    #1
    Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭


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


Comments

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


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


  • Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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