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

Lesson 15: Prototype & Macro

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

Function prototype — an "announcement" of a function

When you want to put a function definition below main, you need to announce it first 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 top to bottom. When it encounters a call to an undeclared function, it reports an error (or warning).
Bad example: Defining a function after main without a prototype — the compiler may warn or fail to type-check arguments.
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 used for constants and short function-like definitions.
#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) becomes ((x+1)*(x+1)); 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...
Advertisement

Related Lessons

Functions
Lesson 13: Functions (Basics)
Defining and calling C functions.
Functions
Lesson 14: 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 14: Functions (Advanced)
Next lesson →
Global Variables & Scope

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 actual function 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. It does not consume 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; <> searches the system's standard directories. Use "" for your own headers.

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

Recommended Books to Deepen Your Understanding

Combine this interactive site with books for more practice.

πŸ“˜
Painfully Learning C
by MMGames
A classic C book for beginners. Builds solid fundamentals with clear explanations.
View on Amazon
πŸ“—
New Clear C Language (Beginner)
by Bohyoh Shibata
Plenty of diagrams and exercises. Widely used as a university textbook.
View on Amazon
πŸ“™
The C Programming Language, 2nd Ed.
by Brian Kernighan & Dennis Ritchie
Known as K&R. The original C book. Ideal after completing the basics.
View on Amazon

* Links above are affiliate links. Purchases help support this site.