C语言中的register关键字

A register variable declaration advises the compiler that the variable in question will be heavily used.

register的声明的变量,告诉编译器,这个变量将会被频繁使用。

The idea is that register variables are to be placed in machine registers, which may result in samller and faster program.

这就要求将register变量存放在机器寄存器中,这可以带来更小,更快的程序。

But programs are free to ignore the advice.

但是,程序有权忽略这个建议。

The register variable declaration can only be appied to automatic variables, and to the formal parameters of a functions.

这个寄存器变量声明不仅仅可以用在自动变量上,也能用在函数的参数上。

In practice, there are restrictions on register variables, reflecting the realitied of underlying hardware.

实际上,对register的使用是有限制条件的,这个体现在具体硬件上。

Only a few variables in each function may be ketp in registers, and only certain types are allowed.

每个函数中只有很少的变量能够存放在寄存器中,而且,只是确定的类型可以存放在寄存器中。

Excess register declarations are harmless, however, since the word register is ignored for excess or disallowed declarations.

过多的使用寄存器变量并没有坏处,然而,由于“register”会被忽略的。

And it is not possible to take address of a register variable, regardless of whether the variable is actually placed in register.

特别注意:不能用&取地址符号,去获取一个register变量的地址,无论这个变量是否被实际得存放在寄存器里了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要遍历C语言关键字,可以使用C语言标准库的<string.h>头文件和<ctype.h>头文件的函数。下面是一种实现方式: 1. 定义一个字符数组来存储C语言的所有关键字,例如: ``` char *keywords[] = {"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"}; ``` 2. 对于输入的每个单词,使用C语言标准库的<ctype.h>头文件的函数检查它是否为关键字。例如,使用strcmp函数比较输入的单词是否与数组的任何一个关键字相等: ``` int is_keyword(char *word) { int i, n; n = sizeof(keywords) / sizeof(char *); for (i = 0; i < n; i++) { if (strcmp(word, keywords[i]) == 0) { return 1; } } return 0; } ``` 3. 对于输入的每个单词,还需要使用C语言标准库的<ctype.h>头文件的函数将其转换为小写字母,以便与关键字数组的字符串进行比较。例如,使用tolower函数将单词转换为小写字母: ``` void to_lower(char *word) { while (*word) { *word = tolower(*word); word++; } } ``` 4. 最后,可以将输入的文本分解成单词,并调用is_keyword函数和to_lower函数来检查它们是否为关键字。 以下是一个完整的示例程序,演示如何遍历C语言关键字: ``` #include <stdio.h> #include <string.h> #include <ctype.h> char *keywords[] = {"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"}; int is_keyword(char *word) { int i, n; n = sizeof(keywords) / sizeof(char *); for (i = 0; i < n; i++) { if (strcmp(word, keywords[i]) == 0) { return 1; } } return 0; } void to_lower(char *word) { while (*word) { *word = tolower(*word); word++; } } int main() { char text[] = "for (int i = 0; i < n; i++) { printf(\"%d\\n\", i); }"; char *word; word = strtok(text, " "); ### 回答2: 在C语言关键字是预定义的标识符,用于表示语言的特殊功能和结构。要遍历C语言关键字,可以按照以下步骤进行: 1. 创建包含C语言所有关键字的数组。根据C语言的标准,可以将关键字存储在一个字符串数组。 2. 使用循环结构,遍历数组的每个关键字。可以使用for循环或while循环来实现遍历。 3. 在循环,将每个关键字打印出来或进行其他操作。可以使用printf函数将关键字输出到控制台上,也可以将其存储在新的数组或其他数据结构。 4. 执行完循环后,即可遍历所有的C语言关键字。 下面是一个简单的示例代码,用于遍历C语言关键字: ```c #include<stdio.h> #include<string.h> int main() { char* keywords[] = {"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"}; int numKeywords = sizeof(keywords) / sizeof(keywords[0]); for(int i = 0; i < numKeywords; i++) { printf("%s\n", keywords[i]); } return 0; } ``` 以上代码通过使用循环遍历并打印出C语言的所有关键字。运行代码后,会依次打印出每个关键字,完成关键字的遍历。 ### 回答3: 要遍历C语言关键字,首先需要了解C语言关键字有哪些。C语言关键字是由编译器预先定义的,用于表示特定功能或命令的保留字。 C语言关键字共有32个,包括基本数据类型(如int、float、char等)、流程控制语句(如if、while、for等)、函数定义关键字(如void、return等)等。 遍历C语言关键字可以采用以下步骤: 1. 定义一个字符串数组,用于存储C语言关键字。 2. 创建一个循环,循环变量从0到31(因为C语言一共有32个关键字)。 3. 在循环,依次将每个关键字赋值给字符串数组的元素。 4. 遍历完所有关键字后,输出或处理字符串数组关键字。 以下是示例代码: ``` #include <stdio.h> #include <string.h> int main() { char keywords[32][10] = {"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"}; int i; for (i = 0; i < 32; i++) { printf("%s\n", keywords[i]); } return 0; } ``` 以上代码会将C语言关键字遍历并逐个输出。可以根据实际需求,将关键字存储到其他数据结构,或进行其他处理操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值