liuchuanwu的个人博客分享 http://blog.sciencenet.cn/u/liuchuanwu

博文

C语言杂笔记

已有 3691 次阅读 2014-2-28 14:53 |个人分类:C语言|系统分类:科研笔记| C语言, 杂笔记

以笔助记:

1、注意各运算符的优先级。C语言运算符有15中优先级,依次是:

   (1)初等运算符 ()  []  ->  .

   (2)单目运算符 !  ~  ++  --  -  (类型)  *  &  sizeof (自右向左结合)

   (3)算术运算符 *  /  %(先乘除后加减)

   (4)算术运算符 +  -

   (5)移位运算符 <<  >>

   (6)关系运算符 <  <=  >  >=    

   (7)关系运算符 ==  !=

   (8)按位与运算符 &

   (9)按位异或运算符 ^

   (10)按位或运算符 |

   (11)逻辑与运算符 &&

   (12)逻辑或运算符 ||

   (13)条件运算符 ? :(三目,自右向左结合)

   (14)赋值运算符 =  +=  -=  *=  /=  %=  >>=  <<=  &=  ^=  |=(自右向左结合)

   (15)逗号运算符 ,

注意&&和||运算符只有在需要时才对右侧操作数球值。条件运算符?:根据条件对第二或第三个操作数求值。逗号运算符,首先对左侧操作数求值,然后将该值被弃,再对右侧操作数求值。赋值运算符并不保证任何求值顺序(=左侧并不一定比右侧操作数先求值)。注意sizeof()是运算符而不是函数。


2、++和--,自增和自减运算符只适用于单个变量

   (1)++i    i自增1后再使用i

   (2)--i    i自减后再使用i

   (3)i++    使用i后i的值再自增1

   (4)i--    使用i后i的值在自减1

注意:++和--指针用在变量上但不能用在表达式上。如i++合法,而(i+j)++不合法。

3、用typedef声明一个新的类型名的方法:

   (1)先按定义变量的方法写出定义体。如:int a[100];

   (2)将变量名换成新的类型名。如:将a换成Num

   (3)在最前面加上typedef。如:typedef int Num[100];

   (4)然后可以用新类型名定义变量。如:Num arr;相当于int arr[100];


4、得到指定类型变量的类型转换符

   (1)声明一个指定类型变量。如 :float (*h)();h为指向返回值为浮点型的函数的指针

   (2)去掉变量名和结尾的分号。如:float (*)()

   (3)将剩余部分用括号封装起来。如(float (*)())

   (4)然后可以使用类型转换符对变量进行类型转换。如((float (*)())0)


5、函数变量名结尾加括号表示调用此变量。如果f是函数名,f();表示调用此函数,而f;只计算f的地址却不调用此函数。又如(*(float (*)()0)();结尾的分号使表达式成为一个语句。


6、main函数的默认类型为整形。main的返回值有时很重要。大多数C语言实现都通过main的返回值来告知系统函数执行是成功还是失败。返回0代表成功,返回非0代表失败。除非是void 类型,否则应该设定返回值。


7、main函数可以有参数。如int main(int argc, char *argv[]),括号内的参数是命令行参数。命令行的一般形式为cmd arg1 arg2 ... argn。命令名和各参数之间用空格隔开。argc为包括命令名在内的参数个数,argv[0]指向命令名,argv[1]指向arg1,...。


8、C语言函数参数按值传递(passed by value)。Fortran函数参数按地址传递(passed by reference)。


9、字符串常量可作为常指针使用。字符可以作为常数使用。


10、注意宏的意义和语法。宏是语句替换(不在引号内也不是别的标识符的一部分的)而不是函数或类型定义。宏的各个参数和整个表达式应该用括号括起来。


11、<limits.h> and <float.h> contain symblic constants for all the size of variables, along with other properties of the machine and compilers.


12、At least the first 31 characters of an internal name, and 6 for a external name are significant. The names begine with underscore often used by library routines.  


13、C语言进行浮点数的算术运算时,将float型数据都自动转换为double型,然后进行运算(取消否?谭书和白皮书矛盾)。


14、External and static variables are initialized to zero by default. Automatic variables for which is no explicit initializer have undefined values.


15、代数运算时,操作数转换规则: A "narrower" operand is converted to a "wider" one without losing infomation.

   (1) If either operand is long double, convert the other to long double

   (2) Otherwise, if either operand is double, convert the other to double

   (3) Otherwise, if either operand is float, convert the other t ofloat

   (4) Otherwise, convert char and short to int

   (5) Then, if either operand is long, convert the other to long

Notic that floats in a expression are not automaticallyconverted to double; this is a change from the original definition.


16、Conversion take place across assignment;The value of the right side is converted to the type of the left, which is the type of the result. Type conversion also takes place when arguments are passed to functions. Finally, explicit type conversions can be forced ("coerced") in any expression, with a unary operater called a cast.


17、由于类型转换的原因导致: -1L < 1U. -1L > 1UL。


18、if op is one of

+  -  *  /  %  <<  >>  &  ^  |

then

expr1 op= expr2

is equivalent to

expr1 = (expr1) op (expr2)

Notice the parentheses around expr2. x *= y+1 means x = x*(y+1) rather than x = x*y + 1.


19、注意switch语言中的break。如果没有break,程序会继续执行其后的case,而不管这些case是否满足条件。


20、strtol函数可以将字符串转换为长整数,stof函数将字符串转换成双精度浮点类型。


21、goto语言可用于从多重循环的最内层跳出。尽量不使用goto语句。


22、一个函数可以有一个以上的return语句,执行到哪个哪个return语言就起作用。如果函数类型和return语句中表达式的值类型不一致,则以函数类型为准。函数的return后的表达式会被转换为函数类型。


23、C语言的函数本身都是external的,C语言不允许在函数内部定义函数。


24、逆波兰式表示法。逆波兰式表示法的解释器是基于堆栈的。解释过程一般是:操作数入栈;遇到操作符时,操作数出栈、求值,将结果入栈。最后栈顶就是表达式的值。


25、自引用结构体,即结构体成员指向同样类型的结构体。


26、printf函数返回打印的字符数。A width or precision may be specified as *, in which case the value is computed by coverting the next argument(which must be an int).


27、The arguments to scanf and sscanf must be pointers.


28、cat file1 file2 ... in Linux means concatenate concatenate file1 and file2 ...


29、stderr is used for error redirection. int ferror(FILE *fp) function is used to check if an error occurred on the steam fp.


30、system(char *s) executes the command contained in the character string s, then resumes execution of the current program. the contents of s depend strongly on the local operating system.


31、C语言中stdlib.h的随机数产生器rand(),生成0到RAND_MAX间的随机整数,srand(unsigned)函数可设置随机数种子。使用rand()生成一定范围的随机数,如:

1、生成0到1间浮点数:((double)rand()/RAND_MAX + 1.0);

2、生成N1到N2间(N1<N2)整数:rand() % (N2 - N1) + N1;

math.h中也有rand()函数(谭书说生成-90到32767间随机整数,似乎有误)。


32、结构体名同结构体第一个成员。结构体可以用&取地址。


33、跳至文件fp的下一行开头 while(fgetc(fp) != 'n');。


34)The type of an assignment expression is the type of its left operand, and the value is the value after the assignment.

35. 数组大小为变量的,不能在声明的同时进行初始化。


36. 不应让一个很大的数和一个很小的数相加。


参考:

【1】 谭浩强著. C程序设计(第四版). 北京:清华大学出版社,2010

【2】 Andrew Koening著. 高巍译. C陷阱与缺陷. 北京:人民邮电出版社,2008

【3】 Brian W. Kernighan & Dennis M. Richie. The C programming language (Second Edition).

【4】 明日科技 编著. C语言必须知道的300个问题. 北京:清华大学出版社, 2012



https://wap.sciencenet.cn/blog-1005104-771787.html

上一篇:《C陷阱与缺陷》读书笔记
下一篇:C语言中变量的存储方式和生存期
收藏 IP: 155.143.49.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 06:09

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部