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。
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: