473,379 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Increment implementation

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
10 1621
"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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Robert Swan | last post by:
I'd like to know why the following program outputs 1, and not 0. #include <iostream> class a { int v; public: a():v(0){} a& operator++(int) { v++;
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.