By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,157 Members | 891 Online
Bytes IT Community
+ Ask a Question
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<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

Sep 15 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Nishu" <na**********@gmail.comwrites:
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.
Nishu
--
Andrew Poelstra <http://www.wpsoftware.net/projects>
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" <na**********@gmail.comwrites:
/* 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.
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))
>
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" <na**********@gmail.comwrites:
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
That's a matter of style.
and their arguments should be fully
parenthesized.
Agreed.
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.
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

Sep 15 '06 #4

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<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.

Sep 15 '06 #5

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<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] = 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

Sep 15 '06 #7

P: n/a
Nishu said:

<snip>
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" <na**********@gmail.comwrites:
Andrew Poelstra wrote:
>"Nishu" <na**********@gmail.comwrites:
/* 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.

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))
>>
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?
My mistake. I didn't know that ? was a sequence point. The behavior
is well-defined (although non-expected!).

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
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.