Стандарт языка Си С99 TC (1113411), страница 34
Текст из файла (страница 34)
It has the disadvantages of evaluating one or the other of itsarguments a second time (including side effects) and generating more code than a function if invokedseveral times. It also cannot have its address taken, as it has none.#define max(a, b) ((a) > (b) ? (a) : (b))The parentheses ensure that the arguments and the resulting expression are bound properly.§6.10.3.5Language155ISO/IEC 9899:TC35EXAMPLE 3Committee Draft — Septermber 7, 2007WG14/N1256To illustrate the rules for redefinition and reexamination, the sequence#define#define#undef#define#define#define#define#define#define#define#define#define#define#definexf(a)xxgzhm(a)wt(a)p()q(x)r(x,y)str(x)3f(x * (a))2fz[0]g(~a(w)0,1aintxx ## y# xf(y+1) + f(f(z)) % t(t(g)(0) + t)(1);g(x+(3,4)-w) | h 5) & m(f)^m(m);p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) };char c[2][6] = { str(hello), str() };results inf(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);int i[] = { 1, 23, 4, 5, };char c[2][6] = { "hello", "" };6EXAMPLE 4sequenceTo illustrate the rules for creating character string literals and concatenating tokens, the#define str(s)# s#define xstr(s)str(s)#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \x ## s, x ## t)#define INCFILE(n) vers ## n#define glue(a, b) a ## b#define xglue(a, b) glue(a, b)#define HIGHLOW"hello"#define LOWLOW ", world"debug(1, 2);fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away== 0) str(: @\n), s);#include xstr(INCFILE(2).h)glue(HIGH, LOW);xglue(HIGH, LOW)results in156Language§6.10.3.5WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3printf("x" "1" "= %d, x" "2" "= %s", x1, x2);fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n",s);#include "vers2.h"(after macro replacement, before file access)"hello";"hello" ", world"or, after concatenation of the character string literals,printf("x1= %d, x2= %s", x1, x2);fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: @\n",s);#include "vers2.h"(after macro replacement, before file access)"hello";"hello, world"Space around the # and ## tokens in the macro definition is optional.7EXAMPLE 5To illustrate the rules for placemarker preprocessing tokens, the sequence#define t(x,y,z) x ## y ## zint j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,),t(10,,), t(,11,), t(,,12), t(,,) };results inint j[] = { 123, 45, 67, 89,10, 11, 12, };8EXAMPLE 6To demonstrate the redefinition rules, the following sequence is valid.#define#define#define#defineOBJ_LIKE(1-1)OBJ_LIKE/* white space */ (1-1) /* other */FUNC_LIKE(a)( a )FUNC_LIKE( a )( /* note the white space */ \a /* other stuff on this line*/ )But the following redefinitions are invalid:#define#define#define#define9EXAMPLE 7OBJ_LIKE(0)// different token sequenceOBJ_LIKE(1 - 1) // different white spaceFUNC_LIKE(b) ( a ) // different parameter usageFUNC_LIKE(b) ( b ) // different parameter spellingFinally, to show the variable argument list macro facilities:#define debug(...)fprintf(stderr, _ _VA_ARGS_ _)#define showlist(...)puts(#_ _VA_ARGS_ _)#define report(test, ...) ((test)?puts(#test):\printf(_ _VA_ARGS_ _))debug("Flag");debug("X = %d\n", x);showlist(The first, second, and third items.);report(x>y, "x is %d but y is %d", x, y);§6.10.3.5Language157ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256results infprintf(stderr, "Flag" );fprintf(stderr, "X = %d\n", x );puts( "The first, second, and third items." );((x>y)?puts("x>y"):printf("x is %d but y is %d", x, y));6.10.4 Line controlConstraints1The string literal of a #line directive, if present, shall be a character string literal.Semantics2The line number of the current source line is one greater than the number of new-linecharacters read or introduced in translation phase 1 (5.1.1.2) while processing the sourcefile to the current token.3A preprocessing directive of the form# line digit-sequence new-linecauses the implementation to behave as if the following sequence of source lines beginswith a source line that has a line number as specified by the digit sequence (interpreted asa decimal integer).
The digit sequence shall not specify zero, nor a number greater than2147483647.4A preprocessing directive of the form# line digit-sequence "s-char-sequenceopt" new-linesets the presumed line number similarly and changes the presumed name of the sourcefile to be the contents of the character string literal.5A preprocessing directive of the form# line pp-tokens new-line(that does not match one of the two previous forms) is permitted. The preprocessingtokens after line on the directive are processed just as in normal text (each identifiercurrently defined as a macro name is replaced by its replacement list of preprocessingtokens). The directive resulting after all replacements shall match one of the twoprevious forms and is then processed as appropriate.158Language§6.10.4WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC36.10.5 Error directiveSemantics1A preprocessing directive of the form# error pp-tokensopt new-linecauses the implementation to produce a diagnostic message that includes the specifiedsequence of preprocessing tokens.6.10.6 Pragma directiveSemantics1A preprocessing directive of the form# pragma pp-tokensopt new-linewhere the preprocessing token STDC does not immediately follow pragma in thedirective (prior to any macro replacement)152) causes the implementation to behave in animplementation-defined manner.
The behavior might cause translation to fail or cause thetranslator or the resulting program to behave in a non-conforming manner. Any suchpragma that is not recognized by the implementation is ignored.2If the preprocessing token STDC does immediately follow pragma in the directive (priorto any macro replacement), then no macro replacement is performed on the directive, andthe directive shall have one of the following forms153) whose meanings are describedelsewhere:#pragma STDC FP_CONTRACT on-off-switch#pragma STDC FENV_ACCESS on-off-switch#pragma STDC CX_LIMITED_RANGE on-off-switchon-off-switch: one ofONOFFDEFAULTForward references: the FP_CONTRACT pragma (7.12.2), the FENV_ACCESS pragma(7.6.1), the CX_LIMITED_RANGE pragma (7.3.4).152) An implementation is not required to perform macro replacement in pragmas, but it is permittedexcept for in standard pragmas (where STDC immediately follows pragma).
If the result of macroreplacement in a non-standard pragma has the same form as a standard pragma, the behavior is stillimplementation-defined; an implementation is permitted to behave as if it were the standard pragma,but is not required to.153) See ‘‘future language directions’’ (6.11.8).§6.10.6Language159ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N12566.10.7 Null directiveSemantics1A preprocessing directive of the form# new-linehas no effect.6.10.8 Predefined macro names1The following macro names154) shall be defined by the implementation:_ _DATE_ _ The date of translation of the preprocessing translation unit: a characterstring literal of the form "Mmm dd yyyy", where the names of themonths are the same as those generated by the asctime function, and thefirst character of dd is a space character if the value is less than 10.
If thedate of translation is not available, an implementation-defined valid dateshall be supplied._ _FILE_ _ The presumed name of the current source file (a character string literal).155)_ _LINE_ _ The presumed line number (within the current source file) of the currentsource line (an integer constant).155)_ _STDC_ _ The integer constant 1, intended to indicate a conforming implementation._ _STDC_HOSTED_ _ The integer constant 1 if the implementation is a hostedimplementation or the integer constant 0 if it is not._ _STDC_MB_MIGHT_NEQ_WC_ _ The integer constant 1, intended to indicate that, inthe encoding for wchar_t, a member of the basic character set need nothave a code value equal to its value when used as the lone character in aninteger character constant._ _STDC_VERSION_ _ The integer constant 199901L.156)_ _TIME_ _ The time of translation of the preprocessing translation unit: a characterstring literal of the form "hh:mm:ss" as in the time generated by theasctime function.
If the time of translation is not available, animplementation-defined valid time shall be supplied.154) See ‘‘future language directions’’ (6.11.9).155) The presumed source file name and line number can be changed by the #line directive.156) This macro was not specified in ISO/IEC 9899:1990 and was specified as 199409L inISO/IEC 9899/AMD1:1995. The intention is that this will remain an integer constant of type longint that is increased with each revision of this International Standard.160Language§6.10.8WG14/N12562Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3The following macro names are conditionally defined by the implementation:_ _STDC_IEC_559_ _ The integer constant 1, intended to indicate conformance to thespecifications in annex F (IEC 60559 floating-point arithmetic)._ _STDC_IEC_559_COMPLEX_ _ The integer constant 1, intended to indicateadherence to the specifications in informative annex G (IEC 60559compatible complex arithmetic)._ _STDC_ISO_10646_ _ An integer constant of the form yyyymmL (for example,199712L).
If this symbol is defined, then every character in the Unicoderequired set, when stored in an object of type wchar_t, has the samevalue as the short identifier of that character. The Unicode required setconsists of all the characters that are defined by ISO/IEC 10646, along withall amendments and technical corrigenda, as of the specified year andmonth.3The values of the predefined macros (except for _ _FILE_ _ and _ _LINE_ _) remainconstant throughout the translation unit.4None of these macro names, nor the identifier defined, shall be the subject of a#define or a #undef preprocessing directive.