gpt4 book ai didi

c - 在 C 中#ifdef 中定义常量

转载 作者:行者123 更新时间:2023-11-30 14:21:22 25 4
gpt4 key购买 nike

我想根据所使用的操作系统定义一个常量。
因此:

#include <stdio.h>

#ifdef _Win32 //Used for system("cls") command
#include <process.h>
#define CLEAR "system(\"cls\")"
#endif

#ifdef __APPLE__
#define CLEAR "system(\"clear\")"
#endif

int main()
{
CLEAR;
}

Xcode 给我一个错误,指出表达式结果未使用于
#define CLEAR "system(\"clear\") 和 main 函数内部。

我使用的是 Mac。

最佳答案

用途:

#define CLEAR system("clear")

不是

#define CLEAR "system(\"clear\")"

您收到错误是因为您的宏调用被替换为:

"system(\"clear\")";

这是一个无用的表达式语句(这里的表达式是字符串),例如:

0;   // valid but pointless

关于c - 在 C 中#ifdef 中定义常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14777200/

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