In this video tutorial, lets see how we can make use of #error preprocessor command or directive.
Page Contents
If you’re a Software developer working with C programming language, then your project will have a lot of code and dependency source codes. While compiling, compiler will take a lot of time. In big projects usually you forget the purpose of compilation by the time it completes compilation. In such cases, make use of #error directive. If there are any errors at certain locations the compiler will stop further compilation as and when it encounters #error directive. This saves a lot of time and resources. So #error directive is usually helpful in debugging your project code.
It’s also helpful in projects with critical user inputs. Suppose user enters wrong data due to which the project itself might crash and utilize a lot of resource while trying to execute. In such cases it’s better to terminate the program execution.
#include<stdio.h> #define iOS #ifndef iOS #error First Include iOS Macro #else int main() { printf("I Love Apple Devices!\n"); return 0; } #endif // iOS
Output:
I Love Apple Devices!
Here #ifndef directive checks if the macro iOS is defined. If its NOT defined it returns true or 1. If it’s defined then it returns 0 or false.
So if the macro iOS is not defined, then the code inside #ifndef gets compiled and executed, else the code inside #else block gets compiled and executed.
When Macro iOS is not defined, the code inside #ifndef gets compiled, where the compiler encounters #error directive. Now the compiler immediately stops any further compilation and displays the error message associated with #error directive.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert