C语言预处理指令总结

Lucseeker posted @ 2010年7月13日 22:50 in 未分类 with tags c , 5992 阅读

        预处理指令严格来说不属于程序语句的一部分,所有的预处理语句在程序编译之前被预处理器处理完成。每一条预处理语句总是以"#"字符开始,并且不能超过一行,一旦遇到换行符语句就被当做结束。一般来说,预处理语句的后面不能加分号。另外,唯一的一种能将预处理语句扩展到多行的办法就是在换行符之前加一个反斜杠('\')。

 

宏定义(#define,#undef)

#define指令主要用于定义常量还有就是定义带有参数的”函数“。

#define PI 3.1416

#define MAX(a,b) (a)>(b)?(a):(b)

有一个值得注意的就是括号的使用。

#undef用于取消之前定义过的宏定义。

”函数“型宏定义接受两个特殊的运算符(#,##)

使用#使得#之后的第一个参数作为一个带引号的字符串返回。例如

#define str(x) #x
cout << str(test);

被处理后相当于

cout << "test";

使用##连接##之前和之后的内容。例如

#define glue(a,b) a ## b
glue(c,out) << "test";

处理后相当于

cout << "test";

 

条件包含指令(#ifdef, #ifndef, #if, #endif, #else ,#elif) 

#ifdef micro
some codes here
#endif

如果宏被使用#define 定义,命令后边的代码被编译。

#if expression
some code here
#endif

如果表达式为真,命令后边的代码被编译。

#ifndef跟#ifdef相反。

#if 和#elif后面 的表达式用于比较操作时候只允许用整数。例如

#if VERBOSE == "on" // NOT ALLOWED
  print("trace message");
#endif
#if VERBOSE >= 2.0 // NOT ALLOWED
  print("trace message");
#endif

另外,#ifdef和#ifndef也可以分别用运算符 defined!defined取得相同的效果。例如

#if !defined TABLE_SIZE
#define TABLE_SIZE 100
#elif defined ARRAY_SIZE
#define TABLE_SIZE ARRAY_SIZE
int table[TABLE_SIZE];


行控制(#line)

语法:

#line line_number "filename"

#line命令可以简单的改变下一行__LINE____FILE__的值,文件名是可选的,__LINE____FILE__表示当前文件名和当前行的行号。

这条命令

 #line 10 "main.cpp"

…改变更改下一行行号改为10,当前文件名为”main.cpp”。


错误指令(#error)

#error message

当编译器遇到#error命令的时候会被迫停止编译,并输出错误信息和出错行号

 

文件包含(#include)

最常用,一共有两种方式

#include "file"
#include <file> 

双引号表示在当前目录搜索,然后在系统定义的目录搜索,而尖括号表示直接在系统目录搜索。

 

Pragma 指令(#pragma)

 #pragma lexems

#pragma命令赋予程序员控制编译器的能力,由于#prama的实现依赖于编译器,使用方法因编译器而异。一个选项可能完全改变程序执行顺序。

 

预定义的宏名

    __LINE__
    __FILE__
    __DATE__
    __TIME__
    __cplusplus
    __STDC__
  • __LINE____FILE__ 变量记录了编译器的当前处理行和当前处理文件。
  • __DATE__ 变量包含当前文件被编译的日期,格式为:月/日/年。
  • __TIME__ 变量包含当前文件被编译的时间,格式为:时:分:秒。
  • __cplusplus 变量只有在C++程序中才被定义;一些老编译器也可以为c_plusplus
  • __STDC__ 变量在编译C程序的时候被定义,许多编译器在编译C++代码时也会定义此变量。

Reference:

  1. www.cppreference.com/wiki/cn/preprocessor/start
  2. en.wikipedia.org/wiki/C_preprocessor
  3. www.cplusplus.com/doc/tutorial/preprocessor/

 

pingf 说:
2010年7月14日 06:58

C的宏确实很强大,尽管很多时候还是不推荐使用.
前一段用宏写了个模拟对象的框框....
sourceforge.net/projects/jcoop [文档见我的博]
不过这个要用C的编译器来编译,不要用CPP的,
因为C++中有些特性是被限制的...

KVS Question Paper 说:
2022年9月27日 19:49

KVS Sample Paper 2023 Pdf Download for Kendriya Vidyalaya Sangathan Class 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 & 12 Arts, KVS Question Paper Science & Commerce Stream Practice Paper Suggestions with Past years old exam Solved Question Bank for all Regional Students of English Medium, Hindi Medium & Urdu Medium Studying in KVS Schools across the Country. All the Kendriya Vidyalaya Sangathan Board Students can download the Sample Paper Suggestions with Model Papers along with Previous Years old Exam Solved Question Bank for all Languages & Subjects of the Course.

civaget 说:
2023年12月14日 03:07

In the vast digital landscape, 구글 상위노출 is the Holy Grail. It's the gateway to gaining maximum online visibility and attracting organic traffic, which are essential for your website's success.

civaget 说:
2023年12月20日 22:59

I really like what you have acquired here, certainly like what you are stating and the way in which you say it. 유튜브 프리미엄 무료보기

civaget 说:
2023年12月21日 21:40 해외축구중계 is the heartbeat of global soccer.
civaget 说:
2023年12月23日 20:24

Kudos to 누누티비 for offering a diverse range of content that caters to all tastes and preferences.

civaget 说:
2023年12月26日 20:32 The allure of 툰코's webtoons lies in their ability to surprise and captivate. An oasis of creativity in the webtoon world.
civaget 说:
2024年1月02日 22:32

온라인 바카라 is fantastic, and those camera angles make it even better. It's the ultimate online casino game.

pavzi.com 说:
2024年1月11日 01:05

Pavzi website is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. pavzi.com We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.

civaget 说:
2024年1月14日 16:10

Artichoke is a culinary haven, offering a menu inspired by Middle Eastern flavors. michelin guide restaurant singapore

civaget 说:
2024年1月14日 16:33

Awesome! I thank you your contribution to this matter. It has been insightful. my blog: how to seduce a girl เว็บพนันคืนยอดเสีย

civaget 说:
2024年1月15日 23:21

I just added this webpage to my feed reader, great stuff. Cannot get enough! Lahore Sky Mall


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter