Learn C function prototypes and the #define macro, with examples.
#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; }
#include it.#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 }
SQ(x+1) becomes ((x+1)*(x+1)); without the parentheses, the arithmetic breaks. Always wrap each argument and the whole body in parentheses.Check your understanding of this lesson!
Prototypes let the compiler type-check calls that appear before the actual function definition.
#define PI 3.14 define?#define is a preprocessor macro — it performs text substitution before compilation. It does not consume memory at runtime.
#include "myfile.h" and #include <stdio.h>?"" searches the current directory first; <> searches the system's standard directories. Use "" for your own headers.
Combine this interactive site with books for more practice.
* Links above are affiliate links. Purchases help support this site.