Difference between Preprocessor and Compiler in C

What is a Preprocessor in C?

The preprocessor is a tool that runs before the actual compilation begins. It handles all the preprocessing directives, which are lines in your code that start with a # symbol. These directives are instructions to the compiler to perform specific tasks before compiling the code.

Common Preprocessor Directives

  • #include (file inclusion): Adds contents from another file.
  • #define: Defines constants or macros.
  • #ifdef, #ifndef, #endif: Conditional compilation.

Example:

#include <stdio.h>
#define QUIZ_SCORE 100
 
int main()
{
    printf("Score: %d\n", QUIZ_SCORE);
    return 0;
}

The preprocessor will replace QUIZ_SCORE with 100 before the compiler sees the code.

What is a Compiler in C?

The compiler is a program that converts your human-readable C code into machine code (binary code) that the computer can understand and execute.

advertisement

It starts working after the preprocessing step is complete.

Compilation Stages:

  • Lexical Analysis – Breaks code into tokens.
  • Syntax Analysis – Checks for grammar and structure.
  • Semantic Analysis – Ensures the code makes logical sense.
  • Code Optimization – Improves performance.
  • Code Generation – Converts to machine code.

Step-by-Step Compilation Flow

Free 30-Day Java Certification Bootcamp is Live. Join Now!

Here’s a simplified version of the C compilation process:

Source Code (.c file)
      ↓
Preprocessor
      ↓
Modified Code (no # directives)
      ↓
Compiler
      ↓
Assembly Code
      ↓
Assembler
      ↓
Object Code (.o file)
      ↓
Linker
      ↓
Executable File (.exe or a.out)

Difference between Preprocessor and Compiler in C

Feature Preprocessor Compiler
Purpose Prepares code for compilation Translates code into machine language
Processes Handles directives like #define, #include Performs syntax and semantic analysis
Execution Time Before compilation During compilation
Error Detection Limited (e.g., missing files) Extensive (syntax, semantics, etc.)
Output Preprocessed code Executable machine code

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.