435,157 Members | 891 Online
Need help? Post your question and get tips & solutions from a community of 435,157 IT Pros & Developers. It's quick & easy.

# Increment implementation

 P: n/a Hi All, Is this wrong(or undefined) implementation in C? /* start */ #include #define min(a,b) (a
10 Replies

 P: n/a "Nishu" #define min(a,b) (a To reach me by email, use `apoelstra' at the above domain. "Do BOTH ends of the cable need to be plugged in?" -Anon. Sep 15 '06 #2

 P: n/a Andrew Poelstra wrote: "Nishu" #define min(a,b) (a int main(void) { int i,j,k; i = 1; j = 2; k = min(i++, j); /* is this exp undefined because I'm using increment operator */ Yes. That's why you need the ALL CAPS warning to tell you that you're using a macro, and you should be careful. you mean that this is undefined? k = (i++ < j)? (i++) : (j) ; Please tell me the reason too. Isnt that I should proceed from right to left of the expression in case of increment operators? > printf("i = %d j = %d k = %d\n", i,j,k); return 0; } /* end */ Thank n Regds, Don't use silly abbreviations. They hinder far more than they help. Ok. Thank you && Regards, Nishu Sep 15 '06 #3

 P: n/a Andrew Poelstra wrote: "Nishu" #define min(a,b) (a

 P: n/a Nishu wrote: you mean that this is undefined? k = (i++ < j)? (i++) : (j) ; Please tell me the reason too. No , it is not undefined because as Robert Gamble said there is a sequence point at "?". Isnt that I should proceed from right to left of the expression in case of increment operators? I'm not sure what you mean here. The sequence of events will be the following: 1) i

 P: n/a Um why write min() as a real function and use the 'inline' attribute ? Why Macros should be ALL CAPS? I doubt if *should* is specified by the standard. I'd hate to work with someone that wrote such confusing code. -Samuel Sep 15 '06 #6

 P: n/a Spiros Bousbouras wrote: 1) i

 P: n/a Nishu said: One doubt(and infact a self test too). It is wrong to use a[i] = i++; but can I safely assume that it is OK to use a[i++] = i; No, it's still undefined, and for the same reason. Let's say, for the sake of argument, that it /were/ legal, and see whether we always get the right result. We will assume that the preceding line is: i = 6; Implementation A thinks like this: a[i++] = i; hmm, okay... MOV tmp1, i ; tmp1 = 6 MOV tmp2, i ; tmp2 = 6 MOV a[tmp1], tmp2 ; a[6] = 6 INC i ; i = 7 Implementation B thinks like this: a[i++] = i; hmm, okay... MOV tmp1, i ; tmp1 = 6 INC i ; i = 7 MOV tmp2, i ; tmp2 = 7 MOV a[tmp1], tmp2 ; a[6] = 7 Implementation C thinks like this: a[i++] = i; hmm, okay... MOV tmp1, i ; tmp1 = 6 INC i ; i = 7 MOV tmp2, i ; tmp2 = 7 MOV a[tmp2], tmp1 ; a[7] = 6 All of the above interpretations follow all the rules of C. But as you can see, we have three different results. We deduce that the expression a[i++] = i; is not well-defined. The above doesn't rule out "implementation-defined" and "unspecified", but a reading of the Standard does: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." (Violation of a 'shall' outside a constraint gives undefined behaviour.) -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at above domain (but drop the www, obviously) Sep 15 '06 #8

 P: n/a "Nishu" "Nishu" #define min(a,b) (a> int main(void) { int i,j,k; i = 1; j = 2; k = min(i++, j); /* is this exp undefined because I'm using increment operator */ Yes. That's why you need the ALL CAPS warning to tell you that you'reusing a macro, and you should be careful. you mean that this is undefined? k = (i++ < j)? (i++) : (j) ; Please tell me the reason too. Isnt that I should proceed from right to left of the expression in case of increment operators? My mistake. I didn't know that ? was a sequence point. The behavior is well-defined (although non-expected!). -- Andrew Poelstra To reach me by email, use `apoelstra' at the above domain. "Do BOTH ends of the cable need to be plugged in?" -Anon. Sep 15 '06 #9

 P: n/a Robert Gamble posted: >Macros should be in ALL CAPS That's a matter of style. Of course, alternatively, we could simply perpend: #define ThisIsAMacroAndSoYouShouldTakeDueCareInInvokingItE speciallyIfYouDontWantTheAr gumentToBeEvaluatedMoreThanOnce Yes, it's a matter of style, but I think the ALL CAPS are more convenient. -- Frederick Gotham Sep 15 '06 #10

 P: n/a Frederick Gotham wrote: Robert Gamble posted: Macros should be in ALL CAPS That's a matter of style. Of course, alternatively, we could simply perpend: #define ThisIsAMacroAndSoYouShouldTakeDueCareInInvokingItE speciallyIfYouDontWantTheAr gumentToBeEvaluatedMoreThanOnce Yes, it's a matter of style, but I think the ALL CAPS are more convenient. I didn't say that I disagree, I didn't provide an opinion one way or the other. The point was to clarify that that it isn't syntactically incorrect as the wording used by Andrew (who you failed to properly attribute) could be interpreted by some as an assertion that is was (as the OP did for instance). Robert Gamble Sep 15 '06 #11

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more C / C++ Questions on Bytes