c语言中相关标准

来源:互联网 发布:银联数据待遇 编辑:程序博客网 时间:2024/04/30 15:02

保留标识符(Reserved identifiers)

+All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.(留给库的实现者使用)
+All identifier that begin with an underscore are always reserved for use as identifier with file scope in both the ordinary identifier and tag name space.(以下划线开头且具有外部连接属性的函数和数据对象的名字)
+Each macro name listed in any of the following sub-clauses (including the future library directions) is reserved for any use if any of its associated headers is included.
+All identifiers with external linkage in any of the following sub-clauses (including the future library directions) are always reserved for use as identifiers with external linkage.
+Each identifier with file scope listed in any of the following sub-clauses (including the future library directions) is reserved for use as an identifier with file scope in the same space if any of its associated headers is included.

库的使用

头文件的使用

+幂等性。多次包含相同的标准头文件与包含一次的效果等同。
+相互独立。任何标准头文件的正常工作都不需要以包含其他标准头文件为前提。
+和文件级别的声明等同。必须包含头文件,然后才能使用。

预定义

头文件包含

<>类型的包含,一般用于标准头文件。
“”类型的包含,一般用于用户定义的头文件。

宏定义

不带参数

宏定义又称为宏代换、宏替换,简称“宏”。
格式 为 #define 标识符 字符串

(1)宏名一般用大写

(2)使用宏可提高程序的通用性和易读性,减少不一致性,减少输入错误和便于修改。例如:数组大小常用宏定义

(3)预处理是在编译之前的处理,而编译工作的任务之一就是语法检查,预处理不做语法检查。

(4)宏定义末尾不加分号;

(5)宏定义写在函数的花括号外边,作用域为其后的程序,通常在文件的最开头。

(6)可以用#undef命令终止宏定义的作用域

(7)宏定义允许嵌套

(8)字符串(” “)中永远不包含宏

(9)宏定义不分配内存,变量定义分配内存。

(10)宏定义不存在类型问题,它的参数也是无类型的

带参数

除了一般的字符串替换,还要做参数代换
格式:#define 宏名(参数列表) 字符串

(1)实参如果是表达式容易出问题

% #define S(r) r*r

area=S(a+b);第一步换为area=r*r;,第二步被换为area=a+b*a+b;

正确的宏定义是#define S(r) ((r)*(r))

(2)宏名和参数的括号间不能有空格

(3)宏替换只作替换,不做计算,不做表达式求解

(4)函数调用在编译后程序运行时进行,并且分配内存。宏替换在编译前进行,不分配内存

(5)宏的哑实结合不存在类型,也没有类型转换。

(6)宏展开使源程序变长,函数调用不会

(7)宏展开不占运行时间,只占编译时间,函数调用占运行时间(分配内存、保留现场、值传递、返回值)

有参宏定义中#的用法

例如 #define STR(str) #str
其中#号表示对str加双引号。
例如#define STR(str) L##str
则在第一个替换的基础上给前面加L。结果为L”…”。

注意事项

% #define 第一位置,第二位置

宏定义不替换程序中字符串里的东西
宏定义中 被替换的必须是合法的标识符,可以是关键字。
宏定义中第二个位置如果有字符串,必须在两个引号中
宏替换要求只能替换完全匹配的的标识符。例如NAMELIST 就不能被NAME的宏定义替换。
带参数的宏定义中,“”内的字符不被当成形参。如#define FUN(a) “a”. FUN(234)会被替换为“a”.

条件编译

形式

#if 表达式//语句#else//语句#endif
#if//语句#elif//语句#else//语句#endif
#ifdef 宏名//语句#endif
#ifndef 宏名//语句#endif
  1. #error 指令将使编译器显示一条错误信息,然后停止编译
  2. #line指令可以改变编译器用来指出警告和错误信息的文件号和行号
  3. #pragma指令没有正式的定义。编译器可以自定义其用途。
0 0