The forgotten #error C macro

in programming •  7 years ago  (edited)

I have have honestly never seen this macro used until a few days ago, but I find it pretty useful. The #error C macro helps you set up conditions it your program that must be met before your program compiles, otherwise it will throw an error specifying the file, line number and error message that were thrown. This is useful because in some situations you don't want your program to compile with default options and want the user to set those before they continue.

An example of a way you would use it would be something like this:

#ifdef OPT_A
    // Option A chosen
#elif OPT_B
    // Option B chosen
#else
    #error Please choose an option
#endif

You could use it for any option that MUST be defined for your program to compile correctly, or any constants your program might need.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!