为什么C的关键字以下划线开头 [英] Why does C have keywords starting with underscore

查看:305
本文介绍了为什么C的关键字以下划线开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数C语言(或与此相关的任何语言)的关键字都以字母开头.但是有些关键字以下划线开头吗?它们的关键字是:_Alignas_Alignof_Atomic_Bool_Complex_Generic_Imaginary_Noreturn_Static_assert_Thread_local.

Most keywords in C (or in any language for that matter) starts with a letter. But there are some keywords that starts with an underscore? They keywords are: _Alignas, _Alignof, _Atomic, _Bool, _Complex, _Generic, _Imaginary, _Noreturn, _Static_assert and _Thread_local.

我觉得这很奇怪.如果它是不是API真正组成部分的隐藏的全局常量或内部函数,我会理解的.但是这些是关键字.

I find it amazingly strange. If it was a hidden global constant or internal function that's not really a part of the API, I would understand it. But these are keywords.

当C实际上有一个名为boolstatic_assert的宏,并且它们的实现使用的是我刚才提到的关键字时,我感到特别奇怪.

I find it extra strange when C actually have a macros called bool and static_assert, and that their implementations is using the very keywords I just mentioned.

推荐答案

C在标准委员会制定计划之前就已经开发并非常流行.结果,有很多现有的代码.

C developed and become very popular before it was planned by a standards committee. In consequence, there was a lot of existing code.

设置C标准或更新旧标准时,一个重要的目标不是破坏"旧代码.希望与以前的编译器一起使用的代码继续与新版本的C语言一起使用.

When setting a C standard, or updating an old standard, an important goal is not to "break" old code. It is desirable that code that worked with previous compilers continue to work with new versions of the C language.

引入新的关键字(或单词的任何新定义或含义)可能会破坏旧代码,因为在编译时,该单词将具有其新关键字含义,而不是先前编译器所具有的标识符含义.该代码将必须进行编辑.除了要花钱去编辑代码之外,如果有任何错误,这也有引入错误的风险.

Introducing a new keyword (or any new definition or meaning of a word) can break old code, since, when compiling, the word will have its new keyword meaning and not the identifier meaning it had with the previous compilers. The code will have to be edited. In addition to the expense of paying people to edit the code, this also has a risk of introducing bugs if any mistakes are made.

为解决此问题,制定了一个规则,即必须保留以下划线开头的标识符.制定此规则并不会破坏很多旧软件,因为大多数编写软件的人都选择使用以字母开头而不是下划线的标识符.该规则为C标准提供了一种新功能:只要在单词中添加下划线或新的其他含义时,只要下划线遵守该规则,就可以在不破坏旧代码的情况下做到这一点.

To deal with this, a rule was made that identifiers starting with underscore were reserved. Making this rule did not break much old software, since most people writing software choose to use identifiers beginning with letters, not underscore. This rule gives the C standard a new ability: By using underscore when adding new keywords or other new meanings for words, it is able to do so without breaking old code, as long as that old code obeyed the rule.

C标准的新版本有时会为不以下划线开头的单词(例如bool)引入新的含义.但是,这些新含义通常不会在核心语言中引入.相反,它们仅在新的头文件中引入.在创建bool类型时,C标准提供了新的头文件<stdbool.h>.由于旧代码无法包含<stdbool.h>,因为编写代码时该代码不存在,因此在<stdbool.h>中定义bool不会破坏旧代码.同时,通过包含<stdbool.h>,它使编写新代码的程序员能够使用新的bool功能.

New versions of the C standard sometimes introduce new meanings for words that do not begin with an underscore, such as bool. However, these new meanings are generally not introduced in the core language. Rather, they are introduced only in new headers. In making a bool type, the C standard provided a new header, <stdbool.h>. Since old code could not be including <stdbool.h> since it did not exist when the code was written, defining bool in <stdbool.h> would not break old code. At the same time, it gives programmers writing new code the ability to use the new bool feature by including <stdbool.h>.

这篇关于为什么C的关键字以下划线开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆