Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The #undef directive is a C preprocessor command that removes a previously established macro definition, effectively unbinding an identifier from its associated replacement token sequence. Once undefined, the preprocessor ceases to expand subsequent occurrences of that identifier within the translation unit.

Syntax

#undef identifier
  • identifier: The exact name of the macro to be removed.

Preprocessor Mechanics

  • Lexical Scope: The removal of the macro takes effect immediately following the #undef directive. Any code parsed prior to the #undef directive retains the expanded macro values. The identifier remains undefined until the end of the translation unit unless explicitly redefined using another #define directive.
  • Function-like Macros: When undefining a function-like macro, only the base identifier is provided. The parameter list must be omitted.
  • Idempotency (No-op behavior): Applying #undef to an identifier that is not currently defined—either because it was never defined or has already been undefined—is strictly valid in standard C. The preprocessor treats this as a safe no-op and will not generate a compilation error or warning.
  • Standard Restrictions: The C standard dictates that standard predefined macros (such as __FILE__, __LINE__, __DATE__, __TIME__, and __STDC__) and the defined operator must not be the subject of an #undef directive. Attempting to undefine these reserved identifiers results in undefined behavior.
  • Phase of Translation: #undef operates during Phase 4 of the C translation process (preprocessing). It manipulates the preprocessor’s internal symbol table before the compiler performs syntactic or semantic analysis.

Syntax Visualization

#define BUFFER_SIZE 1024
#define COMPUTE_MAX(x, y) ((x) > (y) ? (x) : (y))

/* Preprocessor expands these normally */
int current_buffer = BUFFER_SIZE; 
int max_val = COMPUTE_MAX(10, 20);

/* Remove the object-like macro */
#undef BUFFER_SIZE

/* Remove the function-like macro (parameters omitted) */
#undef COMPUTE_MAX

/* Safe no-op: Undefining an identifier that does not exist */
#undef NON_EXISTENT_MACRO

/* 
 * ILLEGAL: The C standard prohibits undefining predefined macros 
 * or the 'defined' operator.
 * #undef __FILE__
 * #undef defined
 */

/* 
 * Subsequent use of BUFFER_SIZE or COMPUTE_MAX here will not be expanded 
 * by the preprocessor, resulting in standard compiler errors for 
 * undeclared identifiers.
 */
Master C with Deep Grasping Methodology!Learn More