作者:陈正冲,石虎
http://blog.csdn.net/dissection_c/article/details/6046410
看看书,做做笔记~
第一章:关键字
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;
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) = 4
sizeof(a) = 400, sizeof(a[100]) = 4, sizeof(&a) = 4, sizeof(&a[0]) = 4
sizeof(b) = 4
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的区别是什么?