Increment implementation

N

Nishu

Hi All,

Is this wrong(or undefined) implementation in C?

/* start */
#include<stdio.h>

#define min(a,b) (a<b? a:b)

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 */

printf("i = %d j = %d k = %d\n", i,j,k);

return 0;
}

/* end */


Thank n Regds,
Nishu
 
A

Andrew Poelstra

Nishu said:
Hi All,

Is this wrong(or undefined) implementation in C?

/* start */
#include<stdio.h>

#define min(a,b) (a<b? a:b)

Macros should be in ALL CAPS, and their arguments should be fully
parenthesized.
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.
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.
 
N

Nishu

Andrew said:
Macros should be in ALL CAPS, and their arguments should be fully
parenthesized.

Why Macros should be ALL CAPS? I doubt if *should* is specified by the
standard.

Thanks, to point out, it is better to use parentheses.
#define min((a) < (b)) ( ((a) < (b))? (a) : (b))
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?
Don't use silly abbreviations. They hinder far more than they help.

Ok. Thank you && Regards,
Nishu
 
R

Robert Gamble

Andrew said:
Macros should be in ALL CAPS

That's a matter of style.
and their arguments should be fully
parenthesized.
Agreed.


Yes.

No, it isn't. This expands to (spaces added for clarity):
k = ( i++ < j ? i++ : j );
which is completely legal, even if the post-increment expression is
evaluated twice since the required sequence points are introduced by
the conditional operator. Whether the result is what the OP intended
is a different matter.

Robert Gamble
 
S

Spiros Bousbouras

Nishu said:
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<j will be evaluated.
2) The value of i will be increased by 1 and the new
value will be stored back in i.

If the result of step 1) was true then you also have the
following 2 steps:
3a) i will be evaluated and its value will be the value of
the expression.
4a) The value of i will be increased by 1 and the new
value will be stored back in i.

Instead , if the result of step 1) was false then you have the
following step:
3b) j will be evaluated and its value will be the value of
the expression.
 
S

Samuel Stearley

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
 
N

Nishu

Spiros said:
1) i<j will be evaluated.
2) The value of i will be increased by 1 and the new
value will be stored back in i.

If the result of step 1) was true then you also have the
following 2 steps:
3a) i will be evaluated and its value will be the value of
the expression.
4a) The value of i will be increased by 1 and the new
value will be stored back in i.

Instead , if the result of step 1) was false then you have the
following step:
3b) j will be evaluated and its value will be the value of
the expression.

Thanks.
I read http://c-faq.com/expr/seqpoints.html

One doubt(and infact a self test too). It is wrong to use a = i++;
but can I safely assume that it is OK to use a[i++] = i; since here i++
defines the final value of i?

Regards,
Nishu
 
R

Richard Heathfield

Nishu said:

One doubt(and infact a self test too). It is wrong to use a = 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.)
 
A

Andrew Poelstra

Nishu said:
Why Macros should be ALL CAPS? I doubt if *should* is specified by the
standard.

It isn't. The problem is that you typed i++ once and it was
incremented twice. You need some indication that you're using
a macro, because lowercase implies a function, where such
things can't happen.
Thanks, to point out, it is better to use parentheses.
#define min((a) < (b)) ( ((a) < (b))? (a) : (b))


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!).
 
F

Frederick Gotham

Robert Gamble posted:
That's a matter of style.

Of course, alternatively, we could simply perpend:

#define
ThisIsAMacroAndSoYouShouldTakeDueCareInInvokingItEspeciallyIfYouDontWantTheAr
gumentToBeEvaluatedMoreThanOnce

Yes, it's a matter of style, but I think the ALL CAPS are more convenient.
 
R

Robert Gamble

Frederick said:
Robert Gamble posted:


Of course, alternatively, we could simply perpend:

#define
ThisIsAMacroAndSoYouShouldTakeDueCareInInvokingItEspeciallyIfYouDontWantTheAr
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top