C Academy · Lesson

Macros and Inline Functions

Explore macro definitions and their advantages over functions.

Lesson 2 of 310 steps

Macros and Inline Functions is a free C Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

Introduction to Macros

Macros and Inline Functions

Macros and inline functions help optimize code by reducing function call overhead.

In this lesson, you will learn:

  • How to define and use macros.
  • The benefits of inline functions over macros.
  • How macros and inline functions improve performance.
Macros and Inline Functions — illustration 1

What is a Macro?

A macro is a preprocessor directive that replaces code before compilation.

Example:

#define SQUARE(x) (x * x)

Now, SQUARE(5) is replaced with (5 * 5) during compilation.

Example: Using Macros

This program demonstrates how to use a macro for calculations.

#include <stdio.h>

#define SQUARE(x) (x * x)

int main() {
    int num = 4;
    printf("Square of %d is %d\n", num, SQUARE(num));
    return 0;
}

Limitations of Macros

Macros do not perform type checking and may cause unexpected results.

Example issue:

#define MULTIPLY(x, y) x * y

Calling MULTIPLY(2 + 3, 4) expands to 2 + 3 * 4, leading to incorrect results.

Introducing Inline Functions

Inline functions provide an alternative to macros while maintaining type safety.

Syntax:

inline int square(int x) { return x * x; }

They reduce function call overhead but ensure proper type checking.

Example: Using Inline Functions

This program demonstrates an inline function replacing a macro.

#include <stdio.h>

inline int square(int x) { return x * x; }

int main() {
    int num = 4;
    printf("Square of %d is %d\n", num, square(num));
    return 0;
}

Advantages of Inline Functions

Inline functions offer several benefits over macros:

  • Type safety.
  • Better debugging (they appear in stack traces).
  • No unexpected expansion errors.

When to Use Macros vs Inline Functions

Use macros when:

  • Defining simple constants.
  • Performing minor text replacements.

Use inline functions when:

  • Performing complex operations.
  • Ensuring type safety.

Summary

In this lesson, you learned:

  • How to define and use macros.
  • The limitations of macros.
  • How inline functions provide a safer alternative.

Next, we will explore conditional compilation!

Macros and Inline Functions — illustration 10
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Macros and Inline Functions” lesson free?

Yes — the full text of “Macros and Inline Functions” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.

What will I learn in “Macros and Inline Functions”?

Explore macro definitions and their advantages over functions. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Macros and Inline Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding the C Preprocessor
  2. Macros and Inline Functions
  3. Conditional Compilation
← Back to C Academy