notebook/notes/c17/macros.md

3.9 KiB

title TARGET DECK FILE TAGS tags
Macros Obsidian::STEM c17::macro
c17

Overview

Macros refer to #define directives that specify terms that should be textually replaced by the preprocessor during compilation:

#define NAME ...

For types that don't have literals that describe their constants, we can use compound literals on the replacement side of the macro:

#define NAME (T){ INIT }

%%ANKI Basic What preprocessor directive is used to define macros? Back: #define Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic How are compound literals specified in a macro definition, say MACRO? Back:

#define MACRO (T){ INIT }

Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What term is used to refer to the replacement side of the following macro?

#define MACRO (T){ INIT }

Back: A compound literal. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What is the difference between the following two lines?

#define MACRO (T){ INIT }
# define MACRO (T){ INIT }

Back: N/A. They are equivalent. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What is the difference between the following two lines?

#define MACRO (T){ INIT }
#define MACRO(T){ INIT }

Back: The first defines a compound literal. The latter defines a function-like macro. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What is T a reference to in the following compound literal?

#define MACRO (T){ INIT }

Back: A type-specifier. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What is INIT a reference to in the following compound literal?

#define MACRO (T){ INIT }

Back: An initializer. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic Why aren't compound literals suitable for ICE? Back: They are objects, not constants. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic How can the following be rewritten so that MACRO is an object?

#define MACRO 5

Back:

#define MACRO (int){5}

Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What is the difference between the following two lines?

#define MACRO 5
#define MACRO (int){5}

Back: The former is a literal whereas the latter is a compound literal. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic Why should compound literals be, generally speaking, const-qualified? Back: Doing so gives the optimizer more room to generate good binary code. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic How do we write macro definitions that span more than one line? Back: Ending all but the last line with a \ character. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic Generally speaking, what character should not be specified at the end of a macro definition? Back: ; Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

Bibliography

  • Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).