What is Macro (#define)?

In C, a text replacement given a name with #define. There are object-like macros naming constants, as in #define BUF_SIZE 64, and function-like macros taking arguments, as in #define SQUARE(x) ((x) * (x)); the preprocessor expands them before compilation. Expansion is only text replacement, so without parentheses around the arguments and the whole body, operator precedence can produce an unintended expression, and an argument with side effects such as SQUARE(i++) is evaluated more than once.

Japanese name: マクロ

Macro (#define) appears in 3 practice questions across 1 topic, including C Programming Basics.

Field: C Programming Basics

Macro (#define) in the explanations

Questions that mention Macro (#define) (3)

  1. Which of the following is the output when the code above, which uses the macro SQUARE defined with #define, is executed?
  2. The header file sensor.h is written as above. Which of the following is the purpose of the three lines #ifndef, #define and #endif?
  3. Which of the following is the output when the code above is written inside the main function and executed?

Books on C Programming Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Preprocessor Argument Undefined behavior struct (C structure) Alignment and padding Bit-field Buffer overflow Dangling pointer Dynamic memory allocation Function pointer

Back to the glossary