πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Lesson 28: Prototype & Macro

Learn C function prototypes and the #define macro, with examples.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • Define constants with #define PI 3.14159
  • Prototype: int add(int, int);
  • Macros are plain text substitution
⭐ Read if you have time
  • Function-like macro pitfalls (SQUARE(3+1))
  • #ifdef / #endif conditional compile
  • const is type-safer

Function prototype — announcing a function up front

When you want to put a function's definition below main, you first need to announce it with a prototype.
#include<stdio.h>

int add(int a, int b);   // prototype (no body, semicolon required)

int main(void) {
  printf("%d\n", add(3,5));  // can use it now
}

int add(int a, int b) {      // definition can come later
  return a + b;
}

Why is a prototype needed?

The C compiler reads code from top to bottom. If it hits a call to a function it hasn't seen yet, it raises an error (or at least a warning).
Bad example: defining a function after main without a prototype. The compiler may warn and can't type-check the arguments properly.
Three ways to fix it:
  1. Put the function above main.
  2. Put a prototype above main (the standard approach).
  3. Put the prototype in a header file (.h) and #include it.

Macros (#define) — constants and shorthand

#define is a directive that performs text substitution before compilation. It's handy for constants and short function-like shortcuts.
#define PI 3.14159       // constant macro
#define MAX_SIZE 100
#define SQ(x) ((x)*(x))   // function-like macro

int main(void) {
  double area = PI * 2.0 * 2.0;
  int a[MAX_SIZE];
  printf("5^2 = %d\n", SQ(5)); // -> ((5)*(5)) = 25
}
Caution: macros are plain text substitution. SQ(x+1) expands to ((x+1)*(x+1)) β€” but without the parentheses, the arithmetic breaks. Always wrap each argument and the whole body in parentheses.

Try it yourself — prototype & macro

proto.c
Output
Click "Run" to execute...
πŸ’‘ Try these ideas too

Related Lessons

Functions
Lesson 24: Functions (Basics)
Defining and calling C functions.
Functions
Lesson 25: Functions (Advanced)
Deep dive into C functions: pass-by-value, call stack, recursion.
Functions
Global Variables & Scope
C global variables, scope, lifetime, and static.
← Previous lesson
Lesson 27: Random Number Generation
Next lesson →
Lesson 29: Global Variables

Review Quiz

Check your understanding of this lesson!

Q1. What is the purpose of a function prototype?

To define the function body
To tell the compiler the function's signature in advance
To define a macro

Prototypes let the compiler type-check calls that appear before the function's actual definition.

Q2. What does #define PI 3.14 define?

A variable
A macro (constant)
A function

#define is a preprocessor macro — it performs text substitution before compilation, so it doesn't use any memory at runtime.

Q3. What is the difference between #include "myfile.h" and #include <stdio.h>?

No difference
They search different directories
One is C++-only

"" searches the current directory first, while <> looks in the system's standard directories. Use "" for your own headers.

Share this article
Share on X (Twitter) Share on Facebook Send on LINE Hatena