C语言之各种关键字

C语言32个关键字:auto, int, double, long, char, float, short, signed, unsigned, struct, union, enum, static, switch, case, default, break, register, const, volatile, typedef, extern, return, void, continue, do , while, if, else, for, goto, sizeof

*********

定义和声明的最重要区别:定义创建了对象,并为这个对象分配了内存,声明没有分配内存。

*********

register
register变量必须是一个单个的值,其长度应小于或等于整型的长度,而且register变量可能不存放在内存中,所以不能用取地址运算符'&'来获取register变量的地址。

*********

static
1. 修饰变量:静态全局和局部变量。他们都存在内存的静态区。限定了作用域,只在某文件或函数中有效。即使用extern来访问也不行。函数里定义的static变量,即使这个函数运行结束,变量也不会被销毁。函数下次使用时仍然会用到之前的值。
2. 修饰函数。限定了作用域:仅在本文件。

*********

数据类型
32位系统中,short为2字节,int为4字节,long为4字节,float为4字节,double为8字节,char为1字节。

*********

标识符命名
标识符命名规则:模块名缩写 + '_' + 作用域前缀 + 数据类型前缀 + 指针前缀 + 含义标识 + 数组或结构后缀。我现在只用到“含义标识”。真羞愧。写得什么烂代码。
含义标识命名规则: 变量命名使用名词性词组,函数命名使用动词性词组。
变量含义标识符构成:目标词 + 动词(的过去分词)+[状语]+[目的地]
函数含义标识符构成:动词(一般现在时)+目标词+[状语]+[目的地]
所有宏定义,枚举变量,只读变量都用大写字母命名,用下划线分割单词。

*********

在计算机系统中,数值一律用补码来表示(正负数)。主要原因是使用补码,可以将符号位和其他位统一处理,同时减法可以按加法来处理。正数的补码与其原码一致(最高位为0)。负数的补码,符号位为1,其余位为该数绝对值的原码按位取反,然后整个数加1。

*********

switch-case
case后面只能是整型或字符型常量或常量表达式,比如1,-1,1+2,‘A‘。字符串不行,变量或变量表达式不行,不论值是否是整型,const,static。

*********

union
union型数据所占的空间等于其最大成员所占的空间。对union型成员的存储都是相对于该联合体基地址的偏移量为0处开始,也就是联合体的访问不论对哪个变量的存取都是从union的首地址开始。

*********

big-endian and small-endian
在内存中,一个数的高位存在内存低地址中为big endian,低位存在内存低地址中为little endian。

//big-endian when 0 is returned; little-endian when 1 is return;
int  checkSystem()
{
    union check{
        int i;
        char ch;
    }c;
    c.i = 1;
    return (c.ch == 1)
}
*********

volatile
用它修饰的变量表示可以被某些编译器未知的因素更改,比如操作系统,硬件和其他线程等。遇到这个关键字声明,编译器对访问该变量的代码不再进行优化。

*********

const
const关键字也许该被替换为readonly。编译器通常不为普通const只读变量非陪存储空间,而是将他们保存在符号表中。

*********

enum
#define宏常量是在预编译阶段进行简单替换,枚举常量则是在编译的时候确定其值。
enum变量的size为4字节。

*********

typedef - The one that drives me crazy!

Types:
typedef int *A[3];  //A is such a type that it defines an array of three pointers that points to integers.

typedef int B[3];  //B is such a type that it defines an array of three integers.

typedef int (*C)[3];  //C is such a type that it defines a pointer to an array of three integers.

typedef int (*a10ptoa5i[10])[5];  //a10ptoa5i is such a type that it defines an array of ten pointers that each points to an array of five integers
/* or */
typedef int a5i[5];
typedef a5i *atenptoa5i[10];


Usages:
typedef int A[3]; A abc_1 = {1,2,3};    //abc_1 is an integer array with 10 elements.

typedef int *B[3]; B abc_2; abc_2[0] = (int*) malloc(sizeof(int)*10); abc2[0][9] = 10;    //abc_2 is an array of three integer pointers

typedef int (*C)[3]; C abc_3; int abc[3] = {4,5,6}; abc_3 = &abc; (*abc_3)[0] = 1; printf("%d\n",(*abc_3)[0]); //abc_3 is a pointer to an array of three integers. to access elements in the array, must use "(*abc_3)[i]" rather than "abc_3[i]". "abc_3 = abc" or "abc_3 = (int*)malloc(sizeof(int)*3)"would give warnings, but "abc_3 = &abc" or "abc_3 = malloc(sizeof(int)*3)" would not.


Functions:

typedef int func_A(int, int); // func_A has type "function taking two int arguments, returning int".
typedef int (*func_B)(int, int); //func_B is a pointer to type "function taking two int arguments, returning int".
func_A func_name{/*...*/} // error.
func_A* func_name{/*...*/} // correct. returns a pointer to a type 'func_A'

Note:  typedef can only be used to declare the type of return value from a function, not the overall type of the function.
Note:  If a typedef of a particular identifier is in scope, that identifer may not be used as the formal parameter of a function.


References:
  • 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、付费专栏及课程。

余额充值