增量实施 [英] Increment implementation

查看:56
本文介绍了增量实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是C中的错误(或未定义)实现吗?


/ * start * /

#include< stdio.h>


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


int main(无效)

{

int i,j,k;

i = 1;

j = 2;


k = min(i ++,j); / *是这个exp未定义因为我正在使用

增量运算符* /


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


返回0;

}


/ *结束* /

感谢n Regds,

Nishu

解决方案

" Nishu" < na ********** @ gmail.comwrites:


大家好,


是在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); / *是这个exp未定义因为我正在使用

增量运算符* /



是的。这就是为什么你需要ALL CAPS警告告诉你使用宏你是
,你应该小心。


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


返回0;

}


/ *结束* /


感谢n Regds,



不要使用愚蠢的缩写。它们的阻碍远远超过它们的帮助。


Nishu



-

Andrew Poelstra< http://www.wpsoftware.net/projects>

要通过电子邮件与我联系,请在上述域名中使用apoelstra。

" ;是否需要插入电缆的两端? -Anon。




Andrew Poelstra写道:


" Nishu" < na ********** @ gmail.comwrites:


/ * start * /

#include< stdio.h>


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



宏应该在所有大写中,并且他们的参数应该是完全的

括号。



为什么宏应该是全部大写?我怀疑*应该*是否由

标准指定。


谢谢,要指出,最好使用括号。

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


>


int main(void)

{

int i,j,k;

i = 1;

j = 2;


k = min(i ++,j); / *是这个exp未定义因为我正在使用

增量运算符* /



是的。这就是为什么你需要ALL CAPS警告,告诉你使用宏你是b
$ b,你应该小心。



你的意思是这是未定义的? k =(i ++< j)? (i ++):( j);请

告诉我原因。不是我应该从右到左继续

表达式增加运算符吗?


>


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


返回0;

}


/ *结束* /

感谢n Regds,



不要使用愚蠢的缩写。他们的阻力远大于他们的帮助。



好​​的。谢谢你&&此致,

Nishu


Andrew Poelstra写道:


" Nishu" < na ********** @ gmail.comwrites:


大家好,


是在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); / *是这个exp未定义因为我正在使用

增量运算符* /



是的。



不,不是。这扩展为(为了清晰起见而增加了空格):

k =(i ++< j?i ++:j);

这是完全合法的,即使后增量表达式也是如此由于条件运算符引入了所需的序列点,所以两次计算了

。结果是OP意图

是另一回事。


Robert Gamble

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

解决方案

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



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


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


这篇关于增量实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆