您的位置:首页 > 编程语言 > C语言/C++

C语言学习11:typedef和define区别,头文件的使用以及 <>和“”区别,宏定义中#和##的用法,条件编译,gcc和c99中宏定义使用printf和sprintf,条件编译避免头文件包含

2013-10-07 11:45 971 查看
1,typedef和define区别

#include <stdio.h>
#define INT32 int
#define COUNT 10

//typedef 是编译器关键字,和#define是完全不同的
//typede 是会令编译器做类型推导,
//#define 是预处理关键字,预处理后即处理完毕
typedef int int32;
//带参数宏(按照参数进行宏替换)
#define max(a,b) (((a)>(b))?(a):(b))

int main(void)
{
int i;
INT32 a=3;
int32  b=5;
int m;

for(i=0;i<COUNT;i++)
printf("%d ",i);
putchar('\n');

m=max(a+3,b-1);
printf("m=%d\n ",m);

return 0;
}


结果:

will@will-laptop:~/ex/10$ ./a.out
0 1 2 3 4 5 6 7 8 9
m=6


2,头文件的使用以及<>和“”的区别

C文件:
//#include<>搜索系统默认目录(/usr/include  /usr/local/include  gcc指定目录)
//#include“”搜索源文件当前目录,然后搜索系统默认目录
//<>或者“”内是头文件路径(相对路径 /绝对路径)
#include </usr/include/stdio.h>
#include "headerfile/123.h"

int main(void)
{
printf("hello!\n");
printf("max=%d\n",max(6,7));
return 0;
}
H文件:123.h
#define max(a,b) ( (a>b)?(a):(b))


结果:

will@will-laptop:~/ex/10$ ./a.out
hello!
max=7


3,宏定义中#和##的用法

#include<stdio.h>
//#将传入参数名转换成加上“”的参数名字符串
#define tostring(s)  #s
//##连接字符串
#define stringcat(s1,s2)  s1##s2
#define stringcat1(s1,s2)  s1+s2

int main(void)
{
printf("%s\n",tostring(12345));
printf("%d\n",stringcat(123,456));
printf("%d\n",stringcat1(123,456));

return 0;
}


结果:

will@will-laptop:~/ex/10$ ./a.out
12345
123456
579


4,条件编译

#include <stdio.h>

//#define  DEBUG
//定义与未定义产生不同的结果,三个语句就是一套
//就是#ifdef  #else  #endif

int main(void)
{
#ifdef DEBUG
printf("this is a debug version!\n");
#else
printf("this is a release version!\n");
#endif
#if 1
printf("macro condition is true!\n");
#else
printf("macro condition is false\n");
#endif
return 0;
}


结果:

will@will-laptop:~/ex/10$ ./a.out
this is a release version!
macro condition is true!


5,gcc和c99宏定义中使用printf和sprintf,

#include <stdio.h>
//"调用"宏时,变参部分参数不能为空
//GCC扩展功能
#define debug(msg,args...)        \
printf(msg,args)
//C99 的用法
#define debug1(msg,...)            \
printf(msg,__VA_ARGS__)
//"调用"宏时,变参部分参数可以为空
//GCC扩展功能
#define info(msg,args...)          \
fprintf(stderr,msg,##args)

//C99 的用法
#define info1(msg,...)             \
fprintf(stderr,msg,##__VA_ARGS__)

int main(void)
{
int a=3,b=5;

debug("a=%d,b=%d\n",a,b);
debug1("a=%d,b=%d\n",a,b);

printf("----------------------------\n");
info("12312312312312312\n");
info1("a=%d,b=%d\n",a,b);

return 0;
}


结果:

will@will-laptop:~/ex/10$ ./a.out
a=3,b=5
a=3,b=5
----------------------------
12312312312312312
a=3,b=5


6,条件编译避免头文件包含

条件编译避免头文件包含
-->

//条件编译的应用
//防止头文件包含
//如果出现两个头文件都包含同样的一个头文件就需要用到这个东西
#ifdef  _HEADER_PROTECT_H_
#define _HEADER_PROTECT_H_
//中间可以放你的文件

#endif /*_HEADER_PROTECT_H_*/


7,宏定义实现函数功能

#include <stdio.h>

//宏定义的高级应用

#define swap(a,b) do {               \
typeof(a) __t;                    \   //typeof可以跟变量和表达式,取变量和表达式值的类型
__t=a;                             \
a=b;                                 \
b=__t;                          \
}while(0)
int main(void)
{
int a=3,b=5;

//typedef:gcc
typeof(a) flag=1;
printf("before swap:a=%d,b=%d\n",a,b);
if(flag)
swap(a,b);
else
printf("no swap\n");
printf("=====================\n");

printf("after swap: a=%d,b=%d\n",a,b);
return 0;
}


结果:

will@will-laptop:~/ex/10$ ./a.out
before swap:a=3,b=5
=====================
after swap: a=5,b=3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐