The #undef directive cancels a previously defined preprocessor macro, allowing developers to constrain macro scope to specific code sections or redefine macro values safely.

#undef Preprocessor Example

undef_example.cc
#include <stdio.h>
 
#define BUFFER_SIZE 512
 
void process_data(void) {
    printf("Processing with buffer size: %d\n", BUFFER_SIZE);
}
 
// Undefine macro to prevent scope leakage
#undef BUFFER_SIZE
 
int main(void) {
    process_data();
    // BUFFER_SIZE is no longer defined here
    return 0;
}