The preprocessor runs in translation phase 4. It includes files, expands macros, and drops conditional regions. The compiler then sees a single translation unit of C tokens. It is not Java import and not a package.
Mental model: #include pastes a header’s tokens into this file. If you paste a definition into two .c files, the linker sees two definitions. Headers should contain declarations, macros, typedefs, and static inline (careful) — not a global int n = 1;.
#include <stdio.h> /* system / implementation headers */ #include "util.h" /* your project header; search starts locally */
stdio.h.#ifndef UTIL_STRINGS_H #define UTIL_STRINGS_H int str_eq(const char *a, const char *b); #endif
UTIL_STRINGS_H.#pragma once is widely supported and not ISO. Guards are portable. Either is fine if the project picks one.#define MAX_N 256 #define SQUARE(x) ((x) * (x))
SQUARE(i++) increments twice. Prefer a static inline function (C99) for typed operations.((x) * (x)).#define true 1 fights <stdbool.h>. Do not.# stringifies a parameter; ## concatenates tokens. Use rarely./* Incorrect */
#define SQUARE(x) x * x
SQUARE(1 + 2) /* 1 + 2 * 1 + 2 */
/* Correct */
#define SQUARE(x) ((x) * (x))
static inline int square(int x) { return x * x; }
#if defined(DEBUG) && DEBUG fprintf(stderr, "n=%d\n", n); #endif #ifdef _WIN32 /* … */ #else /* … */ #endif
#error "message" stops compilation with a reason.#define on the command line: cc -DDEBUG=1.do { … } while (0) so if (c) MACRO(); else … breaks..c.~ Good luck with your revision! ~