您可以#在C中定义注释吗? [英] Can you #define a comment in C?

查看:70
本文介绍了您可以#在C中定义注释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试系统,但似乎无法正常工作。

I'm trying to do a debug system but it seems not to work.

我想要完成的工作是这样的:

What I wanted to accomplish is something like this:

#ifndef DEBUG
    #define printd //
#else
    #define printd printf
#endif

有没有办法做到这一点?我有很多调试消息,但我不想这么做:

Is there a way to do that? I have lots of debug messages and I won't like to do:

if (DEBUG)
    printf(...)

code

if (DEBUG)
    printf(...)

...


推荐答案

不,您不能。在对预处理指令进行任何处理之前,将从代码中删除注释。因此,您不能在宏中添加评论。

No, you can't. Comments are removed from the code before any processing of preprocessing directives begin. For this reason you can't include comment into a macro.

此外,不能保证以后通过使用任何宏欺骗手段来形成评论。编译器不需要将后期注释识别为注释。

Also, any attempts to "form" a comment later by using any macro trickery are not guaranteed to work. The compiler is not required to recognize "late" comments as comments.

实现所需内容的最佳方法是在C99中使用带有可变参数的宏(或者,也许,使用编译器扩展)。

The best way to implement what you want is to use macros with variable arguments in C99 (or, maybe, using the compiler extensions).

这篇关于您可以#在C中定义注释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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