gpt4 book ai didi

c - c中#define的奇怪错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:01:57 25 4
gpt4 key购买 nike

我知道 #define 在编译为实际值之前被替换了。那么,为什么这里的第一个代码编译没有错误,而第二个代码却没有?

第一个;

#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("bc");
return 0;
}

第二个(不工作);

#include <stdio.h>
#include <stdlib.h>
#define Str "bc";
int main()
{
printf(Str);
return 0;
}

error: expected ')' before ';' token

谢谢你的回答,抱歉我的英语不好......

最佳答案

因为 Str 宏的计算结果为 "bc"; — 包括了分号。所以你的宏扩展为:

printf("bc";);

您不需要在#define 后跟一个分号。它们以换行符结束,而不是像 C 语句那样以分号结束。这很困惑,我知道; C 预处理器是一种奇怪的野兽,它是在人们了解之前就发明的。

关于c - c中#define的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6704241/

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