英文:
I can't wrap my head around this sentence from the GNU C PREPROCESSOR documentation
问题
以下是翻译好的部分:
在“预处理器概述”部分的第9行中,它位于“标记化”部分下面。
如下所示:
唯一可以被视为预处理关键字的标识符是
defined
。
我尝试了
#define DEFINED
但它完全正常工作。
我还向ChatGPT提出了问题,并在网上搜索,但没有什么有用的信息。
这在实践中告诉我们什么?
这给我们带来了什么限制?
英文:
It is in the 9th row under the "tokenization" section in the "Preprocessor overview"
It's as follows:
>The only identifier which can be considered a preprocessing keyword is defined
.
I tried
#define DEFINED
but it works just fine.
I also asked chatGPT and searched the web for it but nothing helped.
What does this tell us in practice?
What limitations does this cause us?
答案1
得分: 2
我尝试过
#define DEFINED
但它工作得很好。
C 是区分大小写的。defined
不同于 DEFINED
。
在实践中,这告诉我们什么?这会给我们带来什么限制?
defined
关键字只能在 #if
和 #elif
预处理指令的表达式中使用。
理论上,你可以创建模棱两可的代码,如:
#define defined
#if defined defined
// 这行代码是否包含?
#endif
但更可能会导致编译器错误。
在实际世界中,这不是一个问题。只需不定义预处理符号 defined
即可。这类似于不能创建名为 if
或 while
的变量。
英文:
> I tried
>
> #define DEFINED
>
> but it works just fine.
C is case-sensitive. defined
is not same as DEFINED
.
> What does this tell us in practice? What limitations does this cause us?
defined
keyword is usable only in expressions of #if
and #elif
preprocessing directives.
In theory you could create ambiguous code like:
#define defined
#if defined defined
// Is this line included or not?
#endif
Thought more likely you get a compiler error.
In practical world this is not an issue. Just don't define defined
preprocessing symbol. This comparable to not being able to create variables named if
or while
.
答案2
得分: 1
首先请注意,C语言是区分大小写的,所以DEFINED
和defined
意味着不同的事情。文本所指的是特殊标识符defined
,只在预处理宏内有效。使用方式如下:
#if defined(__STDC__) && (__STDC__VERSION == 201710L)
puts("This C compiler is up to date.");
#endif
这等同于:
#ifdef __STDC__
#if (__STDC__VERSION == 201710L)
puts("This C compiler is up to date.");
#endif
#endif
正如我们所看到的,使用define
的好处在于它允许更简单的预处理检查,而无需多重嵌套的#if
/#ifdef
。假设我们要检查编译器是否遵循标准C并且支持VLA(可变长度数组):
#if defined(__STDC__) && !defined(__STDC_NO_VLA__)
puts("This compiler is useful");
#else
puts("This compiler is useless");
#endif
这是一种更方便的代码,没有代码重复,与下面的混乱代码相比:
#ifdef __STDC__
#ifndef __STDC_NO_VLA__
puts("This compiler is useful");
#else
puts("This compiler is useless");
#endif
#else
puts("This compiler is useless");
#endif
英文:
First note that C is a case-sensitive language, so DEFINED
and defined
mean different things. What the text refers to is the special identifier defined
only valid inside pre-processor macros. Usage is like this:
#if defined(__STDC__) && (__STDC__VERSION == 201710L)
puts("This C compiler is up to date.");
#endif
This is equivalent to:
#ifdef __STDC__
#if (__STDC__VERSION == 201710L)
puts("This C compiler is up to date.");
#endif
#endif
As we can see, the benefit of define
is that it allows simpler pre-processor checks without resorting to multiple nested #if
/#ifdef
. Suppose we want to check of a compiler is following standard C and also supports VLA:
#if defined(__STDC__) && !defined(__STDC_NO_VLA__)
puts("This compiler is useful");
#else
puts("This compiler is useless");
#endif
That's a whole lot more convenient code with no code repetition, compared to this mess:
#ifdef __STDC__
#ifndef __STDC_NO_VLA__
puts("This compiler is useful");
#else
puts("This compiler is useless");
#endif
#else
puts("This compiler is useless");
#endif
答案3
得分: 0
因为你可以写成以下两种方式之一:
#ifdef MACRO
或者:
#if defined MACRO
所以你不能将一个宏命名为"defined",因为它是一个关键字。
英文:
It's because you can write either:
#ifdef MACRO
Or:
#if defined MACRO
So you can't name a macro defined because it's a keyword.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论