Error Handling in C
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.

Unlock the power of C preprocessors — learn directives like #include, #define, macros, conditional compilation, and file inclusion for cleaner, flexible code.
Preprocessors in C are tools that manipulate your source code before it's compiled. They enable you to perform various tasks such as including header files, defining constants, and performing conditional compilation. In this tutorial, we'll explore preprocessors in C, including their usage, directives, and examples.
Table of contents [Show]
The C preprocessor is a part of the C compiler that handles directives beginning with a # symbol. These directives are processed before the actual compilation begins. Preprocessors are used to modify or generate C code and can significantly enhance code readability, reusability, and maintainability.
#includeThe #include directive is used to include the contents of header files in your code. This allows you to reuse code from other files.
#include // Include the standard I/O header
#include "myheader.h" // Include a user-defined header
#defineThe #define directive is used to create macros and constants. Macros are like functions that get replaced with code during preprocessing.
#define MAX_VALUE 100 // Define a constant
#define SQUARE(x) ((x) * (x)) // Define a macro
#ifdef, #ifndef, #else, and #endifThese directives are used for conditional compilation. You can include or exclude code based on predefined macros.
#ifdef DEBUG
// Debug-specific code
#else
// Release code
#endif
#pragmaThe #pragma directive is used to give special instructions to the compiler. It's often used for compiler-specific optimizations and settings.
#pragma pack(1) // Specify byte alignment
Let's explore some example programs to demonstrate the use of preprocessors in C.
In this example, we use conditional compilation to include different code sections based on whether the DEBUG macro is defined or not.
#include
#define DEBUG
int main() {
#ifdef DEBUG
printf("Debug mode enabled.\n");
#else
printf("Release mode enabled.\n");
#endif
return 0;
}
Compile without the -DDEBUG flag for release mode, and with it for debug mode.
Here, we define a constant and a macro to calculate the square of a number.
#include
#define MAX_VALUE 100
#define SQUARE(x) ((x) * (x))
int main() {
int num = 5;
if (num > MAX_VALUE) {
printf("Number exceeds the maximum value.\n");
}
int square = SQUARE(num);
printf("The square of %d is %d.\n", num, square);
return 0;
}
In this example, we use the #include directive to include a user-defined header file.
// myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H
void myFunction();
#endif
// main.c
#include
#include "myheader.h"
int main() {
printf("Hello, world!\n");
myFunction();
return 0;
}
#ifndef, #define, and #endif) in header files to prevent multiple inclusion.Preprocessors in C are a powerful tool for code organization, reuse, and conditional compilation. By using preprocessor directives like #include, #define, and conditional statements, you can create more modular and maintainable C code. However, it's essential to use these features judiciously and follow best practices to avoid code complexity and maintain code readability.
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Unlock flexibility in C—learn how to implement variadic functions using stdarg.h: handling ellipses (...), va_list, va_start, va_arg, va_end, with clear examples.
Leverage C command-line arguments—learn argc, argv[], parsing techniques, optional envp, data conversion, and flag handling with practical code examples.
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.


