#include / #define
Preprocessor directives are executed before compilation. Use #include to load header files and #define to define constants. Both start with # at the beginning of the line and do not require a semicolon.
Syntax
// Loads a standard library header. #include <header_name.h> // Loads a user-defined header file. #include "header_name.h" // Defines a constant macro (no semicolon at the end). #define MACRO_NAME value // Examples of constant definitions. #define PI 3.14159265358979 #define MAX 100 #define APP_NAME "MyApp"
Common Standard Header Files
| Header | Description |
|---|---|
| <stdio.h> | Provides standard I/O functions such as printf, scanf, and fopen. |
| <stdlib.h> | Provides memory management (malloc, free), random numbers (rand), and conversions (atoi). |
| <string.h> | Provides string manipulation functions such as strlen, strcpy, and strcmp. |
| <math.h> | Provides math functions such as sqrt, sin, cos, and pow. |
| <time.h> | Provides functions for retrieving and converting dates and times. |
| <ctype.h> | Provides character classification functions such as isdigit, isalpha, and toupper. |
| <stdint.h> | Provides fixed-width integer types such as uint8_t and int32_t. |
| <stdbool.h> | Provides the bool type along with true and false (C99 and later). |
Sample Code
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// Define constant macros.
#define PI 3.14159265358979
#define MAX_SIZE 10
#define APP_NAME "Circle Calculator"
// Note that macros have no type.
#define GRAVITY 9.8
int main(void) {
printf("== %s ==\n", APP_NAME);
// Use the constants in calculations.
double radius = 5.0;
double area = PI * radius * radius;
double circ = 2.0 * PI * radius;
printf("Circle with radius %.1f\n", radius);
printf("Area: %.4f\n", area); // Outputs "78.5398".
printf("Circumference: %.4f\n", circ); // Outputs "31.4159".
// Use MAX_SIZE to define the array size.
int arr[MAX_SIZE];
for (int i = 0; i < MAX_SIZE; i++) {
arr[i] = i * i;
}
printf("arr[5] = %d\n", arr[5]); // Outputs "25".
// Use sqrt from math.h (may require -lm at link time).
printf("sqrt(2) = %.6f\n", sqrt(2.0)); // Outputs "1.414214".
// Calculate the distance fallen using GRAVITY.
double t = 2.0; // seconds
double distance = 0.5 * GRAVITY * t * t;
printf("Distance fallen after %.1f s: %.1f m\n", t, distance); // Outputs "19.6 m".
return 0;
}
Notes
#include <...> searches the compiler's standard include paths, while #include "..." searches the directory of the current file first. Double quotes are the conventional choice for user-defined headers.
Constants defined with #define are handled by simple text substitution at the preprocessor stage. Because macros have no type, they bypass the compiler's type checking. Where possible, prefer const variables or enum to write type-safe code.
For function-like macros (macros that accept arguments), see #define (function macros). For conditional compilation, see #ifdef / #ifndef.
If you find any errors or copyright issues, please contact us.