Macros are text replacements created with #define that insert code into your program. Macros may take parameters like a function but the macro code and parameters are always inserted into code by text substitution.


  • Are evaluated by the preprocessor
  • Are not executable code themselves
  • Can control the generation of code before the compilation process
  • Provide shortcuts

Simple Macros


#define label text

  • Every instance of label in the current file will be replaced by text
  • text can be anything you can type into your editor
  • Arithmetic expressions evaluated at compile time


Argument Macros


#define label(arg1,…, argn) code

  • The code must fit on a single line or use "\" to split lines
  • Text substitution used to insert arguments into code
  • Each instance of the label() will be expanded into code
  • This is not the same as a C function


Extreme care must be exercised when using macros. Consider the following use of the below macro:

The result will be the following:

x = 30
i = 7

Wrong Answers!
x = square(i++);
expands to:
x = ((i++)*(i++));
So i gets incremented twice, not once at the end as expected.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.