预处理指令 | 意义 | 用法 |
---|---|---|
#include | 文件包含 | #include <stdio.h> |
#define | 宏定义 | #define PCchar char* |
#undef | 撤销定义过的宏 | \#undef PCchar |
#if、#else、#elif、#endif | 条件编译,true就编译、关心宏定义,也关心宏真假 | \#if()……#elif()……#else……#endif |
#ifdef、#else、#endif | 条件编译,有定义宏就编译 | #ifdef DEBUG……#else……#endif |
#line | 语句#line来重新设定__LINE__的值,指定下一行的__LINE__的值 | \#line 99 |
#error | 生成编译错误提示消息,停止编译。 | #error "ni you yi ge bug." |
#pragma | 用于指定计算机或操作系统特定的编译器功能 |
0.#include
#include <> 从环境变量中找头文件
#include ""一般从当前目录下找头文件
not find 头文件可以用gcc -I 头文件目录 -o build 001.c 指定头文件目录来编译
#include 包含头文件
1.#define
# include <stdio.h>
# define PI 3.14159
int main(void)
{
double r, s;
printf("请输入圆的半径:");
scanf("%lf", &r); //scanf中, double只能用%lf
s = PI * r * r;
printf("s=PI*r^2 = %.6f\n", s); //PI不会被宏替换
return 0;
}
输出结果是:
请输入圆的半径:1
s=PI*r^2 = 3.141590
2.#undef
#include <stdio.h>
int main()
{
#define MAX 200
printf("MAX = %d\n", MAX);
#undef MAX
int MAX = 10;
printf("MAX = %d\n", MAX);
return 0;
}
3.#if,#elif,#else,#endif
#include <stdio.h>
int main(int argc, char *argv[])
{
#if(1==AAAA || 2==AAAA)
printf("aaaa:%d\n", AAAA);
#elif (3==AAAA || 4==AAAA)
printf("bbbb:%d\n", AAAA);
#else printf("cccc:%d\n", AAAA);
#endif
return 0;
}
4.#ifdef,#endif
gcc -D ABC 相当于 #define ABC
#include <stdio.h>
int main()
{
#ifdef DEBUG
printf("this is a bug.");
#endif
return 0;
}
输出:
####################################
#include <stdio.h>
#define DEBUG
int main()
{
#ifdef DEBUG
printf("this is a bug.");
#endif
return 0;
}
输出:this is a bug.
5.#line
#include <stdio.h>
int main()
{
#line 100
printf("%d\n",__LINE__);
}
输出:100
6.#error
#include <stdio.h>
int main()
{
#error "this is a bug."
printf("hello\n");
}
直接报错不会打印hello
7.#pragma
1)#pragma once
在头文件的最开始处加上这句话,就可以避免头文件的重复引用(include),保证每个头文件只编译一次,再加入同名的头文件也不编译,且不会报错。
2)#pragma message
输出字符
#ifdef DEBUG
#pragma message("this is okk.") //DEBUG被定义这内容将被打印
#endif
3)#pragma hdrstop
预编译头文件到此为止,后面的将不再参与预编译。
4)#pragma warning
与warning相关我操作
#pragma warning (disable:4707) //屏蔽4707警告
#pragma warning (once:4706) //只显示一次4706警告
#pragma warning (error:164) //将164号警告当作一个错误。
或者#pragma warning (disable:4707;once:4706;error:164)
5)#pragma comment
链接文件,不需要在工程中添加库了。
#pragma comment( comment-type [,“commentstring”] )
comment-type:指类型,compiler,exestr,lib,linker等……
commentstring:提供信息的字符串,也就是路径
#pragma comment (lib,"user32.lib")