标签: c-preprocessor

C预处理器是否首先删除注释或扩展宏?

考虑这个(可怕的,可怕的,没有好的,非常糟糕的)代码结构:

#define foo(x) // commented out debugging code

// Misformatted to not obscure the point
if (a)
foo(a);
bar(a);
Run Code Online (Sandbox Code Playgroud)

我已经看到两个编译器的预处理器在这段代码上生成不同的结果:

if (a)
bar(a);
Run Code Online (Sandbox Code Playgroud)

if (a)
;
bar(a);
Run Code Online (Sandbox Code Playgroud)

显然,对于可移植的代码库来说这是一件坏事.

我的问题:预处理器应该用这个做什么?首先是Elide评论,还是先扩展宏?

c comments c99 c-preprocessor

54
推荐指数
3
解决办法
2万
查看次数

如何在C预处理器中生成错误或警告?

我有一个程序必须只在DEBUG模式下编译.(测试目的)

如何让预处理器阻止在RELEASE模式下进行编译?

c-preprocessor

54
推荐指数
4
解决办法
5万
查看次数

#warning预处理器指令的可移植性

我知道#warning指令不是标准的C/C++,但有几个编译器支持它,包括gcc/g ++.但对于那些不支持它的人,他们会默默地忽略它还是会导致编译失败?换句话说,我可以安全地在我的项目中使用它而不会破坏不支持它的编译器的构建吗?

c++ compiler-construction portability warnings c-preprocessor

53
推荐指数
2
解决办法
4万
查看次数

为什么这个 .c 文件 #include 本身?

为什么这个.c文件#include本身?

vsimple.c

#define USIZE 8
#include "vsimple.c"
#undef USIZE

#define USIZE 16
#include "vsimple.c"
#undef USIZE

#define USIZE 32
#include "vsimple.c"
#undef USIZE

#define USIZE 64
#include "vsimple.c"
#undef USIZE
Run Code Online (Sandbox Code Playgroud)

c c-preprocessor

53
推荐指数
2
解决办法
8042
查看次数

为什么要使用#define而不是变量

什么是点#define在C++?我只看到了用它代替"幻数"的例子,但我没有看到只是将该值赋予变量的重点.

c++ c-preprocessor

52
推荐指数
5
解决办法
9万
查看次数

C中的命名空间

有没有办法(ab)使用C预处理器来模拟C中的命名空间?

我正在考虑以下几点:

#define NAMESPACE name_of_ns
some_function() {
    some_other_function();
}
Run Code Online (Sandbox Code Playgroud)

这将被翻译为:

name_of_ns_some_function() {
    name_of_ns_some_other_function();
}
Run Code Online (Sandbox Code Playgroud)

c c++ namespaces c-preprocessor

51
推荐指数
6
解决办法
5万
查看次数

解决方案范围内的#define

有没有办法全局声明#define?

就像我想拥有一个文件,例如,

#define MONO
Run Code Online (Sandbox Code Playgroud)

我希望所有源代码文件都知道这个预处理器指令已经定义.我怎么做到这一点?

c# c-preprocessor

51
推荐指数
4
解决办法
3万
查看次数

错误:类型'const char [35]'和'const char [2]'到二进制'operator +'的操作数无效

在我的文件的顶部,我有

#define AGE "42"
Run Code Online (Sandbox Code Playgroud)

稍后在文件中我多次使用ID,包括一些看起来像的行

1 std::string name = "Obama";
2 std::string str = "Hello " + name + " you are " + AGE + " years old!";
3 str += "Do you feel " + AGE + " years old?";
Run Code Online (Sandbox Code Playgroud)

我收到错误:

"错误:类型'const char [35]'和'const char [2]'到二进制'运算符+''的操作数无效"

第3行.我做了一些研究,发现这是因为C++如何处理不同的字符串,并且能够通过将"AGE"更改为"string(AGE)"来修复它.但是,直到今天我才意外地错过了其中一个实例,并且想知道为什么编译器没有抱怨,即使我还有一个只是"AGE"的实例.

通过一些反复试验,我发现我只需要string(AGE)在不连接函数体中创建的另一个字符串的行上.

我的问题是"在后台发生的事情是C++不喜欢将字符串与预处理器放置的字符串连接起来,除非你还连接了你在函数中定义的字符串."

c++ string stdstring c-preprocessor

51
推荐指数
4
解决办法
10万
查看次数

为什么有人会使用#define来定义常量?

这是一个简单的问题,但为什么有人会使用#define定义常量?

有什么区别

#define sum 1const int sum = 1;

c++ const c-preprocessor

50
推荐指数
3
解决办法
2万
查看次数

我们可以有递归宏吗?

我想知道我们是否可以在C/C++中使用递归宏?如果是,请提供示例.

第二件事:为什么我无法执行以下代码?我在做什么错?是因为递归宏吗?

# define pr(n) ((n==1)? 1 : pr(n-1))
void main ()
{
    int a=5;
    cout<<"result: "<< pr(5) <<endl;
    getch();
}
Run Code Online (Sandbox Code Playgroud)

c c++ macros c-preprocessor

50
推荐指数
4
解决办法
4万
查看次数