我无法理解GNU C预处理器文档中的这个句子。

huangapple go评论73阅读模式
英文:

I can't wrap my head around this sentence from the GNU C PREPROCESSOR documentation

问题

以下是翻译好的部分:

链接到页面

在“预处理器概述”部分的第9行中,它位于“标记化”部分下面。
如下所示:

唯一可以被视为预处理关键字的标识符是 defined

我尝试了

#define DEFINED

但它完全正常工作。

我还向ChatGPT提出了问题,并在网上搜索,但没有什么有用的信息。

这在实践中告诉我们什么?
这给我们带来了什么限制?

英文:

Link to the page

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 即可。这类似于不能创建名为 ifwhile 的变量。

英文:

> 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语言是区分大小写的,所以DEFINEDdefined意味着不同的事情。文本所指的是特殊标识符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.

huangapple
  • 本文由 发表于 2023年6月22日 15:39:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定