Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C Language Dictionary

  1. Home
  2. C Language Dictionary
  3. #include / #define

#include / #define

Since: C89(1989)

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

#include <header_name.h>
#include "header_name.h"
#define MACRO_NAME value
#define PI     3.14159265358979
#define MAX    100
#define APP_NAME "MyApp"

Common Standard Header Files

HeaderDescription
<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

sample_include_define.c
#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;
}

Run the following command:

gcc include_define.c -o include_define -lm
./include_define
== Circle Calculator ==
Circle with radius 5.0
Area: 78.5398
Circumference: 31.4159
arr[5] = 25
sqrt(2) = 1.414214
Distance fallen after 2.0 s: 19.6 m

Common Mistakes

Common Mistake: Adding a Semicolon to #define

Putting a semicolon at the end of a #define line causes a spurious semicolon to be inserted wherever the macro is expanded, leading to compile errors or unintended behavior.

define_semicolon_ng.c
#include <stdio.h>

/* NG: the semicolon makes the macro expand to "100;" */
#define MAX_NG 100;

int main(void) {
    int arr[MAX_NG]; /* expands to arr[100;] — compile error */
    (void)arr;
    return 0;
}

Run the following command:

gcc define_semicolon_ng.c -o define_semicolon_ng
define_semicolon_ng.c: error: expected ']' before ';' token
define_semicolon_ok.c
#include <stdio.h>

/* OK: no semicolon */
#define MAX_OK 100

int main(void) {
    int arr[MAX_OK];
    for (int i = 0; i < MAX_OK; i++) arr[i] = i;
    printf("arr[99] = %d\n", arr[99]); /* Prints 'arr[99] = 99'. */
    return 0;
}

Run the following command:

gcc define_semicolon_ok.c -o define_semicolon_ok
./define_semicolon_ok
arr[99] = 99

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 .