宏是如何工作的 [英] how the macro works

查看:79
本文介绍了宏是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我无法理解以下代码如何工作并将100分配给

计数器变量?

展开 | 选择 | Wrap | 行号

解决方案

George2说:


大家好,

我无法理解以下代码如何工作并将100分配给

计数器变量?


void Foo(int *输入)

{

*输入= 100;


返回;

}


#define GE TFOO(Foo(& counter),柜台)


int counter;


int main()

{

GETFOO;

返回0;

}



宏替换后已经发生了,你的程序变成了:


void Foo(int *输入)

{

*输入= 100;


返回;

}

int counter;


int main()

{

(Foo(& counter),柜台);

返回0;

}


既然你看到了这样的话,它会让你觉得有点怪异吗?

-

Richard Heathfield< http: //www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


George2< ge ************* @ yahoo.comwrote in news:c0f8e56c-9fb3-40d6- a504-
72 ********** @ s12g2000prg.googlegroups.com


我无法理解以下代码如何工作并将100分配给

计数器变量?



我的猜测是你不知道逗号操作符是如何工作的。


需要两个操作数,一个左边一个和一个。


它计算左操作数,然后计算右操作数,并且

结果表达式等于右操作数。 (我不记得是否

你可以用它作为L值,但我的猜测是不行的。)


如果你有:


y = Func(),5;

然后它就像你写的那样:


Func ();


y = 5;


即:Func()得到评估,然后5。获得评估,

结果表达式为5。

-
$ b $bTomásóhéilidhe


Tomásóhéilidhe写道:


(这些名字中有趣的人物。)


George2< ge ************* @ yahoo.com在新闻中写道:c0f8e56c-9fb3-40d6-a504-
72 ********** @ s12g2000prg.googlegroups.com


>我无法理解以下代码如何工作并将100分配给计数器变量?



我的猜测是你不知道逗号操作符是如何工作的。


需要两个操作数,一个左边一个和一个。


它计算左操作数,然后计算右操作数,并且

结果表达式等于右操作数。 (我不记得

你可以把它当作L值,但我的猜测是没有的)。



到目前为止,这么好,但是:


如果你有:


y = Func(),5;


那么就像你写的那样:


Func() ;


y = 5;



否;就像你写的那样


y = Func(); 5;


逗号是绑定最少的操作符(当它是一个操作符时)。


示例代码:


#include< stdio.h>


int func(void)

{return 17; }


int main(无效)

{

int y;

y = func() ,5;

fprintf(stderr," y ==%d \ n",y);

返回0;

}


输出:

$ b $ = = 17


-

Chris未指定优先级的运算符 Dollin


Hewlett-Packard Limited Cain Road,Bracknell,注册号:

注册办事处:Berks RG12 1HN 690597 England


Hello everyone,
I can not understand how the following code works and assign 100 to
counter variable?

Expand|Select|Wrap|Line Numbers

解决方案

George2 said:

Hello everyone,
I can not understand how the following code works and assign 100 to
counter variable?

void Foo (int* input)
{
*input = 100;

return;
}

#define GETFOO (Foo( &counter ), counter)

int counter;

int main()
{
GETFOO;
return 0;
}

After macro replacements have occurred, your program becomes:

void Foo (int* input)
{
*input = 100;

return;
}
int counter;

int main()
{
(Foo( &counter ), counter);
return 0;
}

Now that you see it like that, does it strike you as being a bit weird?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


George2 <ge*************@yahoo.comwrote in news:c0f8e56c-9fb3-40d6-a504-
72**********@s12g2000prg.googlegroups.com:

I can not understand how the following code works and assign 100 to
counter variable?


My guess is you don''t know how the comma operator works.

It takes two operands, a left one and a right one.

It evaluates the left operand, then evaluates the right operand, and the
resulting expression is equal to the right operand. (I can''t remember if
you can use it as an L-value, but my guess would be no).

If you have:

y = Func(), 5;
then it''s as if you wrote:

Func();

y = 5;

That is: "Func()" gets evaluated, then "5" gets evaluated, and the
resultant expression is "5".
--
Tomás ó héilidhe


Tomás ó héilidhe wrote:

(Those are funny characters in the name.)

George2 <ge*************@yahoo.comwrote in news:c0f8e56c-9fb3-40d6-a504-
72**********@s12g2000prg.googlegroups.com:

>I can not understand how the following code works and assign 100 to
counter variable?


My guess is you don''t know how the comma operator works.

It takes two operands, a left one and a right one.

It evaluates the left operand, then evaluates the right operand, and the
resulting expression is equal to the right operand. (I can''t remember if
you can use it as an L-value, but my guess would be no).

So far, so good, but:

If you have:

y = Func(), 5;

then it''s as if you wrote:

Func();

y = 5;

No; it''s as though you''d written

y = Func(); 5;

Comma is the least binding operator (when it''s an operator at all).

Example code:

#include <stdio.h>

int func(void)
{ return 17; }

int main(void)
{
int y;
y = func(), 5;
fprintf( stderr, "y == %d\n", y );
return 0;
}

Output:

y == 17

--
Chris "an operator of unspecified precedence" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England


这篇关于宏是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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