gpt4 book ai didi

c - #include 比它出现在程序中更早

转载 作者:行者123 更新时间:2023-12-02 06:53:38 25 4
gpt4 key购买 nike

我有一个 header ,其中一个函数声明是否定义了 DEBUG

我还定义了这个函数,其工作原理相同。但是我会知道 DEBUG 是否仅在参数检查后在 main() 中定义。

我已将 #include 放在 main() 中:

标题:

#ifdef DEBUG
void printStack(Stack* st);
#endif

函数定义所在的文件

#ifdef DEBUG
void printStack(Stack* st)
{
int i;
for (i = 0; i < st->size; i++)
printf(ValType_IOSPECIF " ",st->data[i]);
printf("\n");
}
#endif

ma​​in()所在的文件

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "calculating.h"

#define EXPR_LENGTH 1000

int main(int argc, char* argv[])
{
int argflag = 0;
if (argc >= 2)
argflag = !strcmp(argv[1], "-debug");

#if (argflag)
#define DEBUG
printf("! DEBUG !\n\n\n")
#endif

#include "stack.h" // there is a that function here
...
}

是吗?

最佳答案

你混淆了 C 和预处理器宏:

在编译源代码之前,以#开头的所有内容都被预处理器替换,并且只有在运行时才会执行main

所以,不,你没有做对。

您不能在运行时#define#if;这是编译前的事情。只需使用 C 的普通变量和 if 控制机制。

你的错误强烈表明你没有通过连贯的教程或 C 书来学习这门语言。也许您想学习新文学;我能想到的任何 C 教程都会教您如何快速完成此操作。

关于c - #include 比它出现在程序中更早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36460705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com