C Function-like Macros

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Function-like Macros?

A function-like macro takes arguments: #define SQUARE(x) ((x) * (x)). Arguments are substituted as text, so wrap each parameter and the whole result in parentheses. #x turns an argument into a string; a ## b joins two tokens. Beware that arguments with side effects may run more than once.

Remember
#define SQUARE(x)   ((x) * (x))
#define MAX(a, b)   ((a) > (b) ? (a) : (b))
#define SHOW(x)     printf(#x " = %d\n", (x))
#define JOIN(a, b)  a##b

Cheat Sheet

Keep these facts handy while you work with Function-like Macros in C.

TopicDetail
No spaceNo space between the name and (
#xStringize the argument
a ## bPaste tokens
Alternativestatic inline functions

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Conditional Compilation.

Example 1 — SQUARE, MAX, and SHOW

The parenthesized form gives correct results for expressions.

C
#include <stdio.h>

#define SQUARE(x) ((x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define SHOW(x)   printf(#x " = %d\n", (x))

int main(void) {
    int n = 7;

    SHOW(SQUARE(3 + 1));
    SHOW(MAX(4, 9));
    SHOW(n * 2);
    return 0;
}

How It Works

  1. SQUARE(3 + 1) is ((3 + 1) * (3 + 1)) = 16.
  2. #x converts the argument text into a string literal.
  3. Adjacent string literals are joined, so #x " = %d\n" becomes one format string.

Edge Cases & Pitfalls

Check these before relying on Function-like Macros in real programs.

twice

Double evaluation

MAX(i++, j++) increments the larger value twice. Use an inline function.

space

Space after the macro name

#define F (x) x defines an object-like macro whose text begins with (x).

Key Takeaways

  • Parenthesize everything.
  • Avoid side effects in arguments.
  • Use inline when you can.

One line: A function-like macro takes arguments: #define SQUARE(x) ((x) * (x)).

Frequently Asked Questions

A macro is text substitution with no type checking and may evaluate arguments more than once. An inline function is type-checked and evaluates each argument once.
It stringizes the argument, turning the argument's source text into a string literal.
Continue with "C Conditional Compilation" next. Include or exclude code with #if, #ifdef, #elif, #else, and #endif. The C Preprocessor hub lists every topic in order.

Next: Conditional Compilation

Include or exclude code with #if, #ifdef, #elif, #else, and #endif.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful