Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Preprocessor directives in C are essential tools that help manage code before compilation. They enable code reuse, improve readability, and allow for conditional compilation, making your programs more efficient and easier to maintain. Understanding these directives is key to mastering C programming.
#include
#include <stdio.h>) or user-defined files (e.g., #include "myfile.h").#define
#define NAME value, where NAME is replaced by value during preprocessing.#ifdef, #ifndef, #endif
#ifdef NAME checks if NAME is defined; #ifndef NAME checks if it is not defined.#if, #elif, #else
#if evaluates an expression; #elif allows for additional conditions; #else provides a fallback.#undef
#undef NAME, which removes the definition of NAME.#pragma
#error
#error "message", which stops compilation and displays the message.Predefined macros (FILE, LINE, DATE, TIME)
__FILE__ gives the name of the current source file.__LINE__ provides the current line number, useful for debugging and logging.Macro functions
#define to create inline functions that can take arguments.#define MACRO_NAME(arg1, arg2) (expression).Conditional compilation