CodeBlanks Logo Icon CodeBlanks

inline

Learn the inline keyword and macro difference.

inline

The inline keyword suggests the compiler replace a function call with the body. The call overhead disappears; small functions gain performance.

Example: inline int square_inline(int x) { return x * x; }. A macro has a similar effect: #define SQUARE(x) ((x) * (x)). The difference: inline offers type safety and evaluates the argument once; a macro is a text substitution and doubles side effects. Modern C prefers inline.

Square example

The square of 5 is 25. square_inline(5) and SQUARE(5) both return 25; the difference is at compile time.

Function definition: inline int square_inline(int x) { return x * x; }. Macro definition: #define SQUARE(x) ((x) * (x)). The parentheses in the macro are required; without them SQUARE(a+b) gives a wrong result. In main you write printf("inline: %d\n", square_inline(5)); and printf("macro: %d\n", SQUARE(5));. The output is 25 for both.

Practice inline and macro square

Write inline int square_inline(int x) that returns x * x. Define the macro #define SQUARE(x) ((x) * (x)). In main print printf("inline: %d\n", square_inline(5)); and printf("macro: %d\n", SQUARE(5));.

Starter code

main.c
#include <stdio.h>

// Write inline int square_inline(int x) returning x*x
// Define the SQUARE macro: ((x) * (x))
// main: print the square of 5 both ways

int main(void) {
    return 0;
}

Sample solution

main.c
#include <stdio.h>

inline int square_inline(int x) { return x * x; }
#define SQUARE(x) ((x) * (x))

int main(void) {
    printf("inline: %d\n", square_inline(5));
    printf("macro: %d\n", SQUARE(5));
    return 0;
}

Expected output

inline: 25
macro: 25

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main difference between macro and inline?

  • Macros are slower
  • inline lacks type safety
  • They are identical
  • Macros double side effects Correct answer

Explanation: Macros are text substitution and evaluate side effects twice.

2 Match inline and macro concepts

Term Meaning
inline Type safe
#define Text substitution
call overhead Removed by inline
side effect Doubled in macro

3 square_inline signature

Question
inline int square_inline(____) { return x * x; }
Answer
inline int square_inline(int x) { return x * x; }

Explanation: The int x parameter carries the value to square.

All lessons in Advanced