《C语言深度剖析》学习笔记1

来源:互联网 发布:完美主义 知乎 编辑:程序博客网 时间:2024/05/22 15:55


《C语言深度剖析》

作者:陈正冲,石虎

http://blog.csdn.net/dissection_c/article/details/6046410

看看书,做做笔记~

第一章:关键字

>C语言一共有32个关键字:http://tigcc.ticalc.org/doc/keywords.html
        auto, static, register, const, int, float, char, double, short, long, signed, unsigned,  struct, union, enum,         for, while, do, if, else, switch, case, default, break, continue,  typedef,         return, extern, void, goto,         volatile, sizeof
>声明和定义的区别:     
        A) int i;        B) extern int i;
>定义:编译器创建一个对象,为这个对象分配一块内存并给它取一个名字,这个名字即变量名/对象名。这个名字和这块内存匹配后就不能再将它们分开。这块内存的位置也不               能被改变。 一个变量在一定的区域内(函数内,全局等等)只能被定义一次。

>声明:声明有两重含义, 1:告诉编译器,这个名字已经匹配到一块内存了,下面代码用到的变量是在别的地方定义的。
                                             2:告诉编译器,这个名字我预定量,别的地方不能再用它来命名了。

>声明可以出现多次, 而定义不能重复。

1.2 

register变量必须是一个单个的值,其长度不能大于整形的长度, 不能用取址运算符来获取register变量的地址。

1.3 

static修饰变量时,变量被分配在内存的静态区。

静态全局变量的作用域仅限于变量被定义的文件中,其他文件无法使用。

静态局部变量只能在函数里用,当这个函数运行结束时,这个静态变量的值不销毁。  

static修饰函数时,函数的作用域局限于本文件。

1.5 sizeof不是函数!

32位系统下

#include <stdio.h>void fun(int b[100]){printf("sizeof(b) = %d\n", sizeof(b));}int main(){int *p = NULL;int a[100];int b[100];printf("sizeof(p) = %d, sizeof(*p) = %d\n", sizeof(p), sizeof(*p));printf("sizeof(a) = %d, sizeof(a[100]) = %d, sizeof(&a) = %d, sizeof(&a[0]) = %d\n", sizeof(a), sizeof(a[100]), sizeof(&a), sizeof(&a[0]));fun(b);return 0;}
输出:
sizeof(p) = 4, sizeof(*p) = 4sizeof(a) = 400, sizeof(a[100]) = 4, sizeof(&a) = 4, sizeof(&a[0]) = 4sizeof(b) = 4
 
>sizeof(a[100])是啥意思?
>函数求值是在运行的时候,sizeof求值是在编译的时候。虽然没有a[100]这个元素,但sizeof(a[100])并不是去访问a[100],而是去识别a这个数组的数组元素的类型.

In the programming languages C and C++, the unary operator ‘sizeof’ is used to calculate the sizes of datatypes, in number of bytes. 

>sizeof是计算数据类型大小的,它认为a[100]的类型为int。

说起sizeof,又想起来sizeof(expression ? expression :expression)。

>条件表达式的返回值的类型

Conditional expressions have right-to-left associativity. The first operand must be of integral or pointer type. The following rules apply to the second and third expressions:

  • If both expressions are of the same type, the result is of that type.

  • If both expressions are of arithmetic or enumeration types, the usual arithmetic conversions (covered inArithmetic Conversions) are performed to convert them to a common type.

  • If both expressions are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type.

  • If both expressions are of reference types, reference conversions are performed to convert them to a common type.

  • If both expressions are of type void, the common type is type void.

  • If both expressions are of a given class type, the common type is that class type.

Any combinations of second and third operands not in the preceding list are illegal. The type of the result is the common type, and it is an l-value if both the second and third operands are of the same type and both are l-values.

1.4 signed和unsigned: unsigned 都是不小于0的

1.6 if语句中的比较

       > 1.bool变量与零值比较:if(bool) ; if(!bool);

       > 2.float变量与零值比较:if(float >= -EPSINON) && (float <= EPSINON);

              float变量1与float变量2比较:if(float1 >= float2 - EPSINON) && (float1 <= float2 + EPSINON);

        >3.指针变量与零值比较:if(NULL == p); if(NULL != p);

使用if else语句时, 应先处理正常情况,再处理异常情况。

1.7 case后的值只能是整形或字符型

1.8 switch case 中,不能用continue。

        多重循环中,应当将最短的循环放到最外层,最长的放到最内层,减少CPU跨切循环层的次数。


1.10 void的作用在于:1.对函数返回的限定;2.对函数参数的限定;

        >void *p; 空类型指针可以指向任何类型的数据。

        >在C语言中,可以给无参数的函数传送任意类型的参数,但在C++中不行。

         >void a; 这样做,编译器不知道该给a分配多大的空间,不能通过编译。

void *p;p++;//ANSI不行,GNU可以

1.10 return 语句不能返回指向栈内存的指针

1.11 const 和 define 的区别://这个不懂啊。。

#define M 3 //宏常量const int N=5; //此时并未将N 放入内存中......int i=N; //此时为N 分配内存,以后不再分配!int I=M; //预编译期间进行宏替换,分配内存int j=N; //没有内存分配int J=M; //再进行宏替换,又一次分配内存!

 1.14 struct 

> 0 <= 空结构体的大小 <=最小的结构体的大小 ,  空结构体的大小是1.

> struct 和 class 的区别:。struct 的成员默认情况下属性是public 的,而class 成员却是private 的。

1.15 union

>大端模式:字数据的高字节放到低地址中;小端模式:字数据的高字节放到高地址中。

#include <stdio.h>int main(){  int a[5] = {1, 2, 3, 4, 5};  int *p1 = (int *)(&a + 1);  int *p2 = (int *)((int)a + 1);  printf("%x, %x", p1[-2], *p2);//output;5, 2000000    return 0;}

1.16 enum

enum 的枚举值必须是整型.

sizeof(enum_type)  = sizeof(int);

enum 和 define 的区别是什么?


1.17 typedef

typedef和const结合

typedef 就像 auto,extern,mutable,static,和 register 一样,是一个存储类关键字

typedef 和 define的区别是什么?