Most of the library functions are implemented as C functions, a few as macros. Some library functions are implemented both as functions and as macros (see list below).
If a library function exists in both variants, the macro variant is generated for the call by default. A function call is generated if the name is enclosed in parentheses () or canceled with the #undef
directive.
Which version you select depends on whether and which aspects (performance, program size, restrictions) are relevant to the particular program.
A function is a compiled program section (module) that is available only once and is treated as an external subroutine at execution time. An organizational overhead is required for each function call during program execution; for example, management of the local, dynamic data of a function in the runtime stack, saving of register contents, return addresses, etc.
Some library functions can be generated inline, controlled via the OPTIMIZATION compiler option. In this case the function code is included directly in the call and the above mentioned administrative activities are not needed.
At present, the following functions can be generated inline: strcpy
, strcmp
, strlen
, strcat
, memcpy
, memcmp
, memset
, abs
, fabs
, labs
.
The C, C++ and C/C++ User Guides provide more detailed information on this topic.
A macro is a source program segment defined by the #define
directive. During compilation, the macro name in the program is replaced by the contents of the called macro each time the macro is called.
The use of a macro may result in better performance during program execution, since the runtime system is not required to perform administrative functions (see function). However, the compiled program increases in size due to macro expansions.
When using a macro, the following should also be taken into account:
The names of macros cannot be passed to other functions as arguments if they require a pointer to a function as an argument.
Using increment/decrement or compound assignment operators for macro arguments may produce unwanted side effects.
The include file which contains the macro definition must always be incorporated in the program.
List of library functions implemented as macros or as both macros and functions:
Macro: | Function: | Macro: | Function: | |
clearerr | (clearerr) |
| iswdigit | (iswdigit) |
clock | (clock) | iswgraph | (iswgraph) | |
feof | (feof) | iswlower | (iswlower) | |
ferror | (ferror) | iswprint | (iswprint) | |
getc | (getc) | iswpunct | (iswpunct) | |
getchar | (getchar) | iswspace | (iswspace) | |
getwc | (getwc) | iswupper | (iswupper) | |
isalnum | (isalnum) | iswxdigit | (iswxdigit) | |
isalpha | (isalpha) | putc | (putc) | |
iscntrl | (iscntrl) | putchar | (putchar) | |
isdigit | (isdigit) | toebcdic | (toebcdic) | |
isebcdic | (isebcdic) | tolower | (tolower) | |
isgraph | (isgraph) | toupper | (toupper) | |
islower | (islower) | towlower | (towlower) | |
ispunct | (ispunct) | towupper | (towupper) | |
isspace | (isspace) | assert | ||
isupper | (isupper) | offsetof | ||
isxdigit | (isxdigit) | va_arg | ||
iswalnum | (iswalnum) | va_end | ||
iswalpha | (iswalpha) | va_start | ||
iswcntrl | (iswcntrl) |