标准C中预定义的宏.docx_第1页
标准C中预定义的宏.docx_第2页
标准C中预定义的宏.docx_第3页
标准C中预定义的宏.docx_第4页
标准C中预定义的宏.docx_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

转载c/c+标准预定义宏2009-08-22 14:40/sevencat/archive/2004/06/10/14872.html一、标准预定义宏The standard predefined macros are specified by the relevant language standards, so they are available with all compilers that implement those standards. Older compilers may not provide all of them. Their names all start with double underscores._FILE_This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in #include or as the input file name argument. For example, /usr/local/include/myheader.h is a possible expansion of this macro._LINE_This macro expands to the current input line number, in the form of a decimal integer constant. While we call it a predefined macro, its a pretty strange macro, since its definition changes with each new line of source code._FILE_ and _LINE_ are useful in generating an error message to report an inconsistency detected by the program; the message can state the source line at which the inconsistency was detected. For example, fprintf (stderr, Internal error: negative string length %d at %s, line %d., length, _FILE_, _LINE_);An #include directive changes the expansions of _FILE_ and _LINE_ to correspond to the included file. At the end of that file, when processing resumes on the input file that contained the #include directive, the expansions of _FILE_ and _LINE_ revert to the values they had before the #include (but _LINE_ is then incremented by one as processing moves to the line after the #include).A #line directive changes _LINE_, and may change _FILE_ as well. See Line Control.C99 introduces _func_, and GCC has provided _FUNCTION_ for a long time. Both of these are strings containing the name of the current function (there are slight semantic differences; see the GCC manual). Neither of them is a macro; the preprocessor does not know the name of the current function. They tend to be useful in conjunction with _FILE_ and _LINE_, though._DATE_This macro expands to a string constant that describes the date on which the preprocessor is being run. The string constant contains eleven characters and looks like Feb 12 1996. If the day of the month is less than 10, it is padded with a space on the left.If GCC cannot determine the current date, it will emit a warning message (once per compilation) and _DATE_ will expand to ? ? ?._TIME_This macro expands to a string constant that describes the time at which the preprocessor is being run. The string constant contains eight characters and looks like 23:59:01.If GCC cannot determine the current time, it will emit a warning message (once per compilation) and _TIME_ will expand to ?:?:?._STDC_In normal operation, this macro expands to the constant 1, to signify that this compiler conforms to ISO Standard C. If GNU CPP is used with a compiler other than GCC, this is not necessarily true; however, the preprocessor always conforms to the standard unless the -traditional-cpp option is used.This macro is not defined if the -traditional-cpp option is used.On some hosts, the system compiler uses a different convention, where _STDC_ is normally 0, but is 1 if the user specifies strict conformance to the C Standard. CPP follows the host convention when processing system header files, but when processing user files _STDC_ is always 1. This has been reported to cause problems; for instance, some versions of Solaris provide X Windows headers that expect _STDC_ to be either undefined or 1. See Invocation._STDC_VERSION_This macro expands to the C Standards version number, a long integer constant of the form yyyymmL where yyyy and mm are the year and month of the Standard version. This signifies which version of the C Standard the compiler conforms to. Like _STDC_, this is not necessarily accurate for the entire implementation, unless GNU CPP is being used with GCC.The value 199409L signifies the 1989 C standard as amended in 1994, which is the current default; the value 199901L signifies the 1999 revision of the C standard. Support for the 1999 revision is not yet complete.This macro is not defined if the -traditional-cpp option is used, nor when compiling C+ or Objective-C._STDC_HOSTED_This macro is defined, with value 1, if the compilers target is a hosted environment. A hosted environment has the complete facilities of the standard C library available._cplusplusThis macro is defined when the C+ compiler is in use. You can use _cplusplus to test whether a header is compiled by a C compiler or a C+ compiler. This macro is similar to _STDC_VERSION_, in that it expands to a version number. A fully conforming implementation of the 1998 C+ standard will define this macro to 199711L. The GNU C+ compiler is not yet fully conforming, so it uses 1 instead. We hope to complete our implementation in the near future._OBJC_This macro is defined, with value 1, when the Objective-C compiler is in use. You can use _OBJC_ to test whether a header is compiled by a C compiler or a Objective-C compiler._ASSEMBLER_This macro is defined with value 1 when preprocessing assembly language.C标准中的一些预定义宏昨天写代码时需要在代码获取当前编译时间,从而可动态地作为版本信息,因此用到了C标准中的一些预定义的宏。在此将C标准中定义的几个宏一并总结一下:_DATE_ 进行预处理的日期(“Mmm dd yyyy”形式的字符串文字,如May 27 2006)_FILE_ 代表当前源代码文件名的字符串文字 ,包含了详细路径,如G:/program/study/c+/test1.c_LINE_ 代表当前源代码中的行号的整数常量_TIME_ 源文件编译时间,格式微“hh:mm:ss”,如:09:11:10;_func_ 当前所在函数名,在编译器的较高版本中支持_FUNCTION_ 当前所在函数名对于_FILE_,_LINE_,_func_,_FUNCTION_ 这样的宏,在调试程序时是很有用的,因为你可以很容易的知道程序运行到了哪个文件的那一行,是哪个函数。而对于_DATE_,_TIME_则可以获取编译时间,如如下代码通过宏获取编译时间,并通过sscanf()从中获取具体的年月日时分秒数据,可在代码中做相应使用。我的代码中是根据此数据作为版本标识,并依此判断哪个版本新些及是否需要升级。char * creationDate = _DATE_ , _TIME_;sscanf(creationDate, %s %d %d, %d:%d:%d, month, &day, &year, &hour, &min, &sec);预处理命令#pragma和预定义宏-转载一、C预定义宏C标准指定了一些预定义宏,编程中常常用到。_DATE_ 进行预处理的日期_FILE_ 代表当前源代码文件名的字符串_LINE_ 代表当前源代码文件中行号的整数常量_STDC_ 设置为1时,表示该实现遵循C标准_STDC_HOSTED_ 为本机环境设置为,否则设为0_STDC_VERSION_ 为C99时设置为199901L_TIME_ 源文件的编译时间_func_ C99提供的,为所在函数名的字符串对于_FILE_,_LINE_,_func_这样的宏,在调试程序时是很有用的,因为你可以很容易的知道程序运行到了哪个文件的那一行,是哪个函数.例如:#include#includevoid why_me();int main() printf( The file is %sn, _FILE_ ); printf( The date is %sn, _DATE_ ); printf( The time is %sn, _TIME_ ); printf(The version is %sn,_STDC_VERSION_); printf( This is line %dn, _LINE_ ); printf( This function is %sn , _func_ ); why_me(); return 0;void why_me() printf( This function is %sn, _func_ ); printf( This is line %dn, _LINE_ );二、#line和#error#line用于重置由_LINE_和_FILE_宏指定的行号和文件名。用法如下:#line number filename例如:#line 1000 /将当前行号设置为1000#line 1000 lukas.c /行号设置为1000,文件名设置为lukas.c#error指令使预处理器发出一条错误消息,该消息包含指令中的文本.这条指令的目的就是在程序崩溃之前能够给出一定的信息。三、#pragma在所有的预处理指令中,#Pragma 指令可能是最复杂的了。#pragma的作用是设定编译器的状态或者是指示编译器完成一些特定的动作。#pragma指令对每个编译器给出了一个方法,在保持与C和C+语言完全兼容的情况下,给出主机或操作系统专有的特征。依据定义,编译指示是机器或操作系统专有的,且对于每个编译器都是不同的。其格式一般为: #Pragma Para其中Para 为参数,下面来看一些常用的参数。(1)message 参数。 Message 参数是我最喜欢的一个参数,它能够在编译信息输出窗口中输出相应的信息,这对于源代码信息的控制是非常重要的。其使用方法为:#Pragma message(“消息文本”)当编译器遇到这条指令时就在编译输出窗口中将消息文本打印出来。当我们在程序中定义了许多宏来控制源代码版本的时候,我们自己有可能都会忘记有没有正确的设置这些宏,此时我们可以用这条指令在编译的时候就进行检查。假设我们希望判断自己有没有在源代码的什么地方定义了_X86这个宏可以用下面的方法#ifdef _X86#Pragma message(“_X86 macro activated!”)#endif当我们定义了_X86这个宏以后,应用程序在编译时就会在编译输出窗口里显示“_X86 macro activated!”。我们就不会因为不记得自己定义的一些特定的宏而抓耳挠腮了。(2)另一个使用得比较多的pragma参数是code_seg。格式如:#pragma code_seg( section-name,section-class )它能够设置程序中函数代码存放的代码段,当我们开发驱动程序的时候就会使用到它。(3)#pragma once(比较常用)只要在头文件的最开始加入这条指令就能够保证头文件被编译一次。这条指令实际上在VC6中就已经有了,但是考虑到兼容性并没有太多的使用它。(4)#pragma hdrstop表示预编译头文件到此为止,后面的头文件不进行预编译。BCB可以预编译头文件以加快链接的速度,但如果所有头文件都进行预编译又可能占太多磁盘空间,所以使用这个选项排除一些头文件。有时单元之间有依赖关系,比如单元A依赖单元B,所以单元B要先于单元A编译。你可以用#pragma startup指定编译优先级,如果使用了#pragma package(smart_init) ,BCB就会根据优先级的大小先后编译。(5)#pragma resource *.dfm表示把*.dfm文件中的资源加入工程。*.dfm中包括窗体外观的定义。(6)#pragma warning( disable : 4507 34; once : 4385; error : 164 )等价于:#pragma warning(disable:4507 34) /* 不显示4507和34号警告信息。如果编译时总是出现4507号警告和34号警告,而认为肯定不会有错误,可以使用这条指令。*/#pragma warning(once:4385) / 4385号警告信息仅报告一次#pragma warning(error:164) / 把164号警告信息作为一个错误。同时这个pragma warning 也支持如下格式:#pragma warning( push ,n )#pragma warning( pop )这里n代表一个警告等级(1-4)。#pragma warning( push )保存所有警告信息的现有的警告状态。#pragma warning( push, n)保存所有警告信息的现有的警告状态,并且把全局警告等级设定为n。#pragma warning( pop )向栈中弹出最后一个警告信息,在入栈和出栈之间所作的一切改动取消。例如:#pragma warning( push )#pragma warning( disable : 4705 )#pragma warning( disable : 4706 )#pragma warning( disable : 4707 )/.#pragma warning( pop )在这段代码的最后,重新保存所有的警告信息(包括4705,4706和4707)。(7)pragma comment(.)该指令将一个注释记录放入一个对象文件或可执行文件中。常用的lib关键字,可以帮我们连入一个库文件。(8)progma pack(n)指定结构体对齐方式!#pragma pack(n)来设定变量以n字节对齐方式。n 字节对齐就是说变量存放的起始地址的偏移量有两种情况:第一、如果n大于等于该变量所占用的字节数,那么偏移量必须满足默认的对齐方式,第二、如果n小于该变量的类型所占用的字节数,那么偏移量为n的倍数,不用满足默认的对齐方式。结构的总大小也有

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论