更多宏观问题 [英] More Macro questions

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

问题描述

我已经问了几个关于Macros的问题......并且想想我曾经发生的事情

缺失...所有回复的人都清楚地了解......是

这是一个预处理器动作。

希望以上是真的。

所以,有人可以查看这段代码,并帮助我理解为什么会得到

这个错误。

#include< stdio.h>


#define get_char()getc(stdin)

#define put_char(x)putc(x,stdout)

int main(int argc,const char * argv []){

int c;

while((c = get_char)!= EOF)/ *错误:''get_char''未声明

(首次使用此功能)* /

put_char(c);

返回0;

}


我认为* *会发生什么是get_char将被替换为

" getc(stdin)"


谢谢

解决方案

mdh说:


< snip>

#include< stdio.h>


#define get_char()getc(stdin)

#define put_char(x)putc(x,stdout)

int main(int argc,const char * argv []){

int c;

while((c = get_char)!= EOF)/ *错误:''get_char''未声明

(首次使用此功能)* /

put_char(c);

返回0;

}


我认为**会发生什么事情是get_char将被替换为

" getc(stdin)"。



然后你会想要#define get_char getc(stdin)


请记住,不过,这个很糟糕的使用宏。


上面的程序写得更好如下:


#include< stdio.h>


int main(无效)

{

int c;

while((c = getchar())!= EOF)

putchar(c);

返回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日


9月30日上午7:21 *,Richard Heathfield< r ... @ see.sig.invalidwrote:
< blockquote class =post_quotes>
mdh说:


< snip>


#define get_char()getc(stdin)

#define put_char(x)putc(x,stdout)


int main(int argc,const char * argv []){

* * int c;

* * while((c = get_char )!= EOF)/ *错误:''get_char''未声明

(首次使用此功能)* /

* * * * put_char(c);

* *返回0;

}


我**以为**会发生什么是get_char将被替换为

" getc(stdin)"。



然后你会想要#define get_char getc(stdin)



Richard ...你失去了我我以为这是定义声明

做了什么?


>

请承担但是,请记住,这是对宏的不良使用。



理查德......我毫不怀疑这是真的。我这么做的唯一原因就是看他们是如何工作的**。也许你可以解释为什么它会使用它并且当你倾向于使用它时很少(很少......如下所述!),

就是一个很好的例子。 />


>

以上程序编写如下:


#include< stdio.h>


int main(无效)

{

* int c;

* while((c = getchar())!= EOF)

* * putchar(c);

*返回0;
< br $>
}




>

避免使用宏,除了在这些场合,他们的优势超过了他们的优势,这是非常罕见的。


mdh写道:


9月30日上午7:21,Richard Heathfield< ; r ... @ see.sig.invalidwrote:


> mdh说:

< snip>
< blockquote class =post_quotes>
>>>


>> #define get_char()getc(stdin)
#define put_char(x)putc(x,stdout)


>> int main(int argc,const char * argv []){
int c;
while( (c = get_char)!= EOF)/ *错误:''get_char''未声明
(首次使用此功能)* /
put_char(c);
返回0;
}


>>我认为**将会发生的事情是将get_char替换为getc (标准输入)" ;.


然后你会想要#define get_char getc(stdin)




Richard ...你失去了我我以为这就是定义声明

做了什么?



RH'

#define get_char getc(stdin)


你定义一个普通的宏与你的对比

#define get_char()getc(stdin)


它定义了一个类似宏的函数。

但是在你的代码中你不能像宏那样使用它:

(c = get_char)


所以要么使用

#define get_char getc(stdin)




(c = get_char())


再见,乔乔


I have asked a few questions about Macros...and think what I have been
missing ...and which all who have replied clearly understand...is that
this is a Pre-processor action.
Hopefully the above is true.
So, could someone look at this code, and help me understand why I get
this error.
#include <stdio.h>

#define get_char() getc(stdin)
#define put_char(x) putc(x, stdout)

int main (int argc, const char * argv[]) {
int c;
while ( (c = get_char) != EOF) /* error: ''get_char'' undeclared
(first use in this function) */
put_char(c);
return 0;
}

I **thought** what would happen is that get_char would be replaced by
"getc(stdin)".

Thanks

解决方案

mdh said:

<snip>

#include <stdio.h>

#define get_char() getc(stdin)
#define put_char(x) putc(x, stdout)

int main (int argc, const char * argv[]) {
int c;
while ( (c = get_char) != EOF) /* error: ''get_char'' undeclared
(first use in this function) */
put_char(c);
return 0;
}

I **thought** what would happen is that get_char would be replaced by
"getc(stdin)".

Then you''ll want #define get_char getc(stdin)

Please bear in mind, though, that this is a poor use of a macro.

The above program would be better written as follows:

#include <stdio.h>

int main(void)
{
int c;
while((c = getchar()) != EOF)
putchar(c);
return 0;
}

Avoid macros except on occasions where their advantages outweigh their
disadvantages, which is pretty rare.

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


On Sep 30, 7:21*am, Richard Heathfield <r...@see.sig.invalidwrote:

mdh said:

<snip>

#define get_char() getc(stdin)
#define put_char(x) putc(x, stdout)

int main (int argc, const char * argv[]) {
* * int c;
* * while ( (c = get_char) != EOF) /* error: ''get_char'' undeclared
(first use in this function) */
* * * * put_char(c);
* * return 0;
}

I **thought** what would happen is that get_char would be replaced by
"getc(stdin)".


Then you''ll want #define get_char getc(stdin)


Richard...you lost me. I thought that is what the define statement
did?

>
Please bear in mind, though, that this is a poor use of a macro.


Richard...I have no doubt that this is true. The only reason I did
this was to see **how** they work. Perhaps you could explain why it is
a poor use and when you tend to use it ( rarely...as noted below!) ,
as a good example.

>
The above program would be better written as follows:

#include <stdio.h>

int main(void)
{
* int c;
* while((c = getchar()) != EOF)
* * putchar(c);
* return 0;

}



>
Avoid macros except on occasions where their advantages outweigh their
disadvantages, which is pretty rare.


mdh wrote:

On Sep 30, 7:21 am, Richard Heathfield <r...@see.sig.invalidwrote:

>mdh said:

<snip>

>>>

>>#define get_char() getc(stdin)
#define put_char(x) putc(x, stdout)

>>int main (int argc, const char * argv[]) {
int c;
while ( (c = get_char) != EOF) /* error: ''get_char'' undeclared
(first use in this function) */
put_char(c);
return 0;
}

>>I **thought** what would happen is that get_char would be replaced
by "getc(stdin)".


Then you''ll want #define get_char getc(stdin)



Richard...you lost me. I thought that is what the define statement
did?

with RH''s
#define get_char getc(stdin)

you define a plain macro vs. your
#define get_char() getc(stdin)

which defines a function like macro.
But in your code you don''t use it as a function like macro:
(c = get_char)

So either use
#define get_char getc(stdin)

or
(c = get_char())

Bye, Jojo


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

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