JS
C

JavaScript to C

10 lessons

Progress0%
1Variables & Types2Functions3Arrays & Pointers4Objects → Structs5Memory Management6Preprocessor & Headers7String Handling in Depth8Enums and Bitwise Operations9Bit-Fields and Unions10Multi-File Projects and Linking
All Mirror Courses
JS
C
Preprocessor & Headers
MirrorLesson 6 of 10
Lesson 6

Preprocessor & Headers

Code organization and compilation

Introduction

In this lesson, you'll learn about preprocessor & headers in C. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with code organization and compilation.

C
In C:

C has its own approach to code organization and compilation, which we'll explore step by step.

The C Way

Let's see how C handles this concept. Here's a typical example:

C
C Example
/* math.h — header file (declarations) */
#ifndef MATH_H  /* include guard */
#define MATH_H

#define PI 3.14159
int add(int a, int b);

#endif /* MATH_H */

/* math.c — implementation */
#include "math.h"
int add(int a, int b) { return a + b; }

/* main.c — usage */
#include <stdio.h>   /* angle brackets = system */
#include "math.h"    /* quotes = local */

#define DEBUG 1

int main() {
    #if DEBUG
    printf("debug mode\n");
    #endif
    printf("%.5f\n", PI);
    printf("%d\n", add(2, 3));
    return 0;
}

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
// math.js — module
export function add(a, b) { return a + b; }
export const PI = 3.14159;

// main.js
import { add, PI } from "./math.js";

// Constants
const DEBUG = process.env.NODE_ENV !== "production";
if (DEBUG) console.log("debug mode");
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C uses .h header files for declarations; JS uses import/export

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C #define creates text-substitution macros; JS has no preprocessor

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

#include guard (#ifndef) prevents double-inclusion; JS handles this automatically

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C compilation: preprocess → compile → link; JS has no separate compile step

Step-by-Step Breakdown

1. Header Files

C header files (.h) declare functions and types that other files can use. The .c file provides the actual implementation.

JS
JavaScript
// math.js — export function add() {...}
C
C
/* math.h */ int add(int a, int b); // declaration only
/* math.c */ int add(int a, int b) { return a+b; } // definition

2. Include Guards

Without include guards, a header included twice causes duplicate declarations. #ifndef/#define/#endif prevents this.

C
C
#ifndef MY_HEADER_H
#define MY_HEADER_H
// ... declarations ...
#endif

3. #define Macros

#define is pure text substitution — dangerous if misused. Prefer const and inline functions for type safety.

C
C
#define MAX(a,b) ((a)>(b)?(a):(b)) // macro — no types
// vs:
static inline int max(int a, int b) { return a > b ? a : b; } // safer

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • C uses .h header files for declarations; JS uses import/export
  • C #define creates text-substitution macros; JS has no preprocessor
  • #include guard (#ifndef) prevents double-inclusion; JS handles this automatically
Common Pitfall
Don't assume C works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Header files (.h) declare; .c files define
  • Include guards prevent double-inclusion
  • #define for macros and conditional compilation
  • System headers use < >; local headers use quotes
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in C to practice these concepts.
PreviousNext