街角_祝福

导航

预处理

1.宏定义

  求求两个数中小的那一个?

#define MIN(a, b) (a)>(b)? (a):(b)
   求一年有多少秒?(不是闰年)

#define YEAR_SECOND (365*60*60*24)UL
2.const用途

   1)定义常量 2)修饰函数参数,返回值

3. const和#define比较。

     const和#define都可用来定义常量,但是const更好,因为它可以指定数据类型。

4.sizeof的使用


#include <stdio.h>
#include <time.h> 
#include <string.h>
struct A/*结构体内的所有元素都小于处理器的位数,那么就以其中的最长的为对齐单位*/ 
{
	short a; //2 BYTE
	short b; //2 BYTE
	char d;  //2 BYTE
};
struct B/*有大于处理器处理位数的,以处理器位数对齐*/
{
	long a;  //4 BYTE
	short b; // 4BYTE
};
int main(int argc, char *argv[])
{
	char *str = "01234"; //指针,指针大小 
	char str1[] = "01234"; //数组指针 ,数组大小 
	char str2[100] = "01234";//数组大小 
	char *str3 = (char *)malloc( 100 );//指针大小 
	printf( "%d\t%d\t%d\t%d\t%d\t%d\n", sizeof(str), sizeof(str1), sizeof(str2), 
			sizeof(str3), sizeof( struct A), sizeof( struct B) );
	printf( "strlen=%d,sizeof=%d\n", strlen("01234567"), sizeof( "01234567" ) ); //9,8
	return 0;
}



a=6;
sizeof( a=8 );

这样的语句,执行完之后,a的值是6,而不是8,因为a=8不会执行。

5.内联函数和宏定义

    内联函数是代码嵌入,会对参数做合法性检查,而宏定义只会做简单的替换,不会参数检查。


posted on 2012-06-19 21:13  街角_祝福  阅读(115)  评论(0编辑  收藏  举报