3 ms·
> #ifdefs don't protect you from misspelled words, #ifs do. That's not (typically) correct. If an undefined identifier is used in a preprocessor expression, it
by _kst_ 11y ago
> #ifdefs don't protect you from misspelled words, #ifs do.
That's not (typically) correct. If an undefined identifier is used in a preprocessor expression, it expands (contracts?) to a constant 0. gcc does not warn about using undefined identifiers by default, and if there's an option to make it do so I'm not familiar with it. (EDIT: I am now, it's "-Wundef". Thanks, sigjuice.) For example, this program:
#include <stdio.h>
#define MISPELLED
int main(void) {
#if MISPELED
puts("MISPELED is true");
#else
puts("MISPELED is false");
#endif
#ifdef MISPELED
puts("MISPELED is defined");
#else
puts("MISPELED is not defined");
#endif
}
produces no compile-time diagnostics and prints this output:
MISPELED is false
MISPELED is not defined
If you have a C compiler that warns about this, that's great -- but you're likely to get spurious warnings for some code.
- sigjuice 11y agogcc has -Wundef (Warn if an undefined identifier is evaluated in an #if directive.)
- PeCaN 11y agoWhich is a enabled by default with -Wall. I'm a fan of compiling with -Wall -Wextra and manually disabling any irrelevant warnings (which is rare, and preferable to do on a per-file basis in the Makefile).