预处理指令 [英] Preprocessor directive

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

问题描述

在函数中取消定义后,是否可以重新定义具有全局范围的宏?如果有,有人可以解释

如何?


/如果/我上面的问题不是很清楚你可以参考

以下示例。


例如cosider 2文件sample.c和sample.h


sample.h

#define BLAH 10

sample.c

/ *这应打印10 * /

printf(" \\ \\ n%d",BLAH);


func1();


printf(" \ n%d", BLAH);


func1()

{

#undef BLAH

#define BLAH 15

/ *这应该打印15,但是BLAH有一个本地范围* /

printf(" \ n%d",BLAH);


}


谢谢,

解决方案

你想要完成什么? ?在编译代码时,预处理器只会扩展您的

宏。所以,如果你要编译一个包含''sample.h'的单独的.c

文件,它只会在

sample.h中看到BLAH的定义。 (这是10)。 BLAH没有本地范围。就

编译器而言,最后一个printf()语句的参数是

常量15.解释你要做什么,我可以提供一些

援助。


" Trying_Harder" < FR *********** @ yahoo.com>在消息中写道

news:b0 ************************** @ posting.google.c om ...

在函数中取消定义后,是否可以重新定义具有全局范围的宏?如果有,有人可以解释一下吗?

/如果/我上面的问题不是很清楚你可以参考下面的例子。

sample.h
#define BLAH 10

sample.c
/ *这个应打印10 * /
printf(" \ n%d",BLAH);

func1();

printf(" \ n%d,BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/ *这应该打印15 ,但BLAH有一个本地范围* /
printf(" \ n%d",BLAH);



Trying_Harder写道:

在函数中取消定义后,是否可以重新定义具有全局范围的宏?如果是,有人可以解释
如何?

宏不是范围。预处理*之前*范围

建立任何类型。

/如果/我上面的问题不是很清楚你可以参考
以下示例。

例如cosider 2文件sample.c和sample.h

sample.h
#define BLAH 10

示例.c
/ *这应打印10 * /
printf(" \ n%d",BLAH);

func1();

printf(" \ n%d",BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/ *这应该打印15,但是BLAH有一个本地范围* /
printf(" \ n%d",BLAH);

}




对于翻译单位的其余部分,BLAH将替换为15。


HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀

哦,好老常规垃圾垃圾日。


Trying_Harder写道:


在函数中取消定义后,是否可以重新定义具有全局范围的宏?如果是的话,有人可以解释一下怎么样?


宏定义的范围从#define

持续到#undef,或持续到翻译单元的末尾。宏

范围没有嵌套,所以没有办法推出来。更换

定义然后pop原来。

/如果/我上面的问题不是很清楚你可以参考下面的例子。

例如cosider 2 files sample.c和sample.h

sample.h
#define BLAH 10

sample.c
/ *这应该打印10 * /
printf(" \ n%d",BLAH);

func1();

printf(" \ n%d",BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/ *这应打印15,但BLAH有本地范围* /
printf(" \ n%d",BLAH);

}




数字输出为10 ,15和10,按此顺序。

BLAH没有本地范围。在func1(); BLAH保持

定义为15直到编译结束。


-
Er ********* @ sun.com


Is it possible to redefine a macro with global scope after
undefining it in a function? If yes, could someone explain
how?

/If/ my question above isn''t very clear you can refer to
the following example.

eg cosider 2 files sample.c and sample.h

sample.h
#define BLAH 10

sample.c
/* This should print 10 */
printf("\n %d ", BLAH);

func1();

printf("\n %d ", BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);

}

Thanks,

解决方案

What are you trying to accomplish ? The pre-processor simply expands your
macro when you compile your code. So, if you were to compile a separate .c
file that included ''sample.h'', it would only see the definition of BLAH in
sample.h (which is 10). BLAH does not have local scope. As far as the
compiler is concerned, the argument to the last printf() statement is the
constant 15. Explain what you are trying to do and I can provide some
assistance.

"Trying_Harder" <fr***********@yahoo.com> wrote in message
news:b0**************************@posting.google.c om...

Is it possible to redefine a macro with global scope after
undefining it in a function? If yes, could someone explain
how?

/If/ my question above isn''t very clear you can refer to
the following example.

eg cosider 2 files sample.c and sample.h

sample.h
#define BLAH 10

sample.c
/* This should print 10 */
printf("\n %d ", BLAH);

func1();

printf("\n %d ", BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);



Trying_Harder wrote:

Is it possible to redefine a macro with global scope after
undefining it in a function? If yes, could someone explain
how?
Macros are not `scoped''. Preprocessing takes place *before* scope of
any kind is established.
/If/ my question above isn''t very clear you can refer to
the following example.

eg cosider 2 files sample.c and sample.h

sample.h
#define BLAH 10

sample.c
/* This should print 10 */
printf("\n %d ", BLAH);

func1();

printf("\n %d ", BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);

}



`BLAH'' will be replaced by `15'' for the rest of the translation unit.

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.


Trying_Harder wrote:


Is it possible to redefine a macro with global scope after
undefining it in a function? If yes, could someone explain
how?
The scope of a macro definition lasts from the #define
to the #undef, or to the end of the translation unit. Macro
scopes do not nest, so there''s no way to "push" a replacement
definition and then "pop" the original.
/If/ my question above isn''t very clear you can refer to
the following example.

eg cosider 2 files sample.c and sample.h

sample.h
#define BLAH 10

sample.c
/* This should print 10 */
printf("\n %d ", BLAH);

func1();

printf("\n %d ", BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);

}



The numbers output would be 10, 15, and 10, in that order.
BLAH does not have "a local scope" in func1(); BLAH remains
defined as 15 all the way to the end of the compilation.

--
Er*********@sun.com


这篇关于预处理指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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