Cprogramming 简明教程

C - Keywords

关键字是那些在编译器中具有特殊含义的预定义单词,它们不可用于任何其他目的。根据 C99 标准,C 语言有 32 个关键字。关键字不能用作 identifiers

Keywords are those predefined words that have special meaning in the compiler and they cannot be used for any other purpose. As per the C99 standard, C language has 32 keywords. Keywords cannot be used as identifiers.

下表列出了 C 语言中所有可用关键字(保留字):

The following table has the list of all keywords (reserved words) available in the C language:

C 语言中的所有关键字均为小写字母,但 C 语言中新添加的关键字包含大写字母。C 是一种区分大小写的语言。因此,int 是关键字,但 INT 或 Int 不被识别为关键字。C99 及更高版本引入的新关键字以下划线字符开头。编译器检查源代码中所有关键字的语法是否正确,然后将其转换为机器代码。

All the keywords in C have lowercase alphabets, although the keywords that have been newly added in C, do have uppercase alphabets in them. C is a case-sensitive language. Hence, int is a keyword but INT, or Int are not recognized as a keyword. The new keywords introduced from C99 onwards start with an underscore character. The compiler checks the source code for the correctness of the syntax of all the keywords and then translates it into the machine code.

Example of C Keywords

在以下程序中,我们将关键字用作标识符,即用作用户定义函数的名称,这将导致编译错误。

In the following program, we are using a keyword as an identifier i.e., as the name of the user-defined function, that will cause a compilation error.

#include <stdio.h>

void register(int, int);
int main () {

   /* variable definition: */
   int a=5, b=7;
   register(a,b);

   return 0;
}
void register(int a, int b)
{
   printf("%d", a+b);
}

Errors

main.c:3:15: error: expected identifier or '(' before 'int'
    3 | void register(int, int);
      |               ^~~
main.c: In function 'main':
main.c:8:14: error: expected ')' before ',' token
    8 |    register(a,b);
      |              ^
      |              )
main.c: At top level:
main.c:12:15: error: expected identifier or '(' before 'int'
   12 | void register(int a, int b)
      |               ^

发生错误的原因是,我们使用关键字 register 作为用户定义函数的名称,这是不允许的。

The reason for the errors is that we are using a keyword register as the name of a user-defined function, which is not allowed.

ANSI C 版本有 32 个关键字。这些关键字是程序逻辑的基本要素。这些关键字可大致分为以下类型 −

The ANSI C version has 32 keywords. These keywords are the basic element of the program logic. These keywords can be broadly classified in following types −

  1. Primary Data types

  2. User defined types

  3. Storage types

  4. Conditionals

  5. Loops and loop controls

  6. Others

我们来看一下每一类中的关键字。

Let us discuss the keywords in each category.

Primary Types C Keywords

这些关键字用于 variable 声明。C 是一种静态类型语言,要使用的变量必须声明。C 中的变量使用以下关键字声明:

These keywords are used for variable declaration. C is a statically type language, the variable to be used must be declared. Variables in C are declared with the following keywords:

int

Declares an integer variable

long

Declares a long integer variable

short

Declares a short integer variable

signed

Declares a signed variable

double

Declares a double-precision variable

char

Declares a character variable

float

Declares a floating-point variable

unsigned

Declares an unsigned variable

void

Specifies a void return type

User-defined Types C Keywords

C 语言允许您根据需要定义新的 data types 。用户定义类型包含一个或多个基本类型元素。

C language allows you to define new data types as per requirement. The user defined type has one or more elements of primary type.

针对用户定义数据类型提供了以下关键字 −

The following keywords are provided for user defined data types −

struct

Declares a structure type

typedef

Creates a new data type

union

Declares a union type

enum

Declares an enumeration type

Storage Types C Keywords

下列关键字称为 storage specifiers 。它们指示变量在内存中的存储位置。变量的默认存储类型是自动的,尽管您可以要求编译器使用特定的存储属性来形成变量。

The following set of keywords are called storage specifiers. They indicate the location where in the memory the variables stored. Default storage type of a variable is auto, although you can ask the compiler to form a variable with specific storage properties.

auto

Specifies automatic storage class

extern

Declares a variable or function

static

Specifies static storage class

register

Specifies register storage class

Conditionals C Keywords

下列关键字可帮助您在程序中放入条件逻辑。由 if 和 else 关键字表示的条件逻辑为一个条件提供了两个替换操作。对于多路分支,请使用 switch – case 构造。在 C 中,汇编器中的跳转操作由 goto keyword 实现。

The following set of keywords help you to put conditional logic in the program. The conditional logic expressed with if and else keywords provides two alternative actions for a condition. For multi-way branching, use switch – case construct. In C, the jump operation in an assembler is implemented by the goto keyword.

goto

Jumps to a labeled statement

if

Starts an if statement

else

Executes when the if condition is false

case

Labels a statement within a switch

switch

Starts a switch statement

default

Specifies default statement in switch

Loops and Loop Control C Keywords

重复或迭代是算法的一个基本方面。C 为形成循环提供了不同的选择,以及用于控制循环行为的关键字。每个关键字都允许您形成不同特征和用法循环。

Repetition or iteration is an essential aspect of the algorithm. C provides different alternatives for forming a loop, and keywords for controlling the behaviour of the loop. Each of the keywords let you form a loop of different characteristics and usage.

For

Starts a for-loop

do

Starts a do-while loop

while

starts a while loop

continue

Skips an iteration of a loop

break

Terminates a loop or switch statement

Other C Keywords

下列其他关键字也极其重要:

The following miscellaneous keywords are also extremely important:

const

Specifies a constant value

Sizeof

Determines the size of a data type

Volatile

compiler that the value of the variable may change at any time

在 C99 版本中,增加了五个关键字 −

In C99 version, five more keywords were added −

  1. _Bool

  2. _Complex

  3. _Imaginary

  4. inline

在 C11 中,增加了七个关键字

In C11, seven more keywords have been added

  1. _Alignas

  2. _Alignof

  3. _Atomic

  4. _Generic

  5. _Noreturn

  6. _Static_assert

当发布 C23 标准时,它将引入 14 个关键字 −

When the C23 standard will be released it will introduce 14 more keywords −

  1. alignas

  2. alignof

  3. bool

  4. constexpr

  5. false

  6. nullptr

  7. static_assert

  8. thread_local

  9. true

  10. typeof

  11. typeof_unqual

  12. _Decimal128

最近保留的大多数单词以下划线开头,后跟大写字母,因为现有的程序源代码不应使用这些标识符。

Most of the recently reserved words begin with an underscore followed by a capital letter, Since existing program source code should not have been using these identifiers.

使用关键字时必须记住以下几点:

Following points must be kept in mind when using the keywords:

  1. Keywords are reserved by the programming language and have predefined meaning. They cannot be used as name of a variable or function.

  2. Each keyword has to be used as per the syntax stipulated for its use. If the syntax is violated, the compiler reports compilation errors.

  3. C is one of the smallest computer languages with only 32 keywords in its ANSI C version, although a few more keywords have been added afterwards.