11
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
this post was submitted on 29 Sep 2024
11 points (100.0% liked)
The C Programming Language
606 readers
9 users here now
Everything related to the C programming language.
founded 5 years ago
MODERATORS
I'm going to take a shot at answering this, but please bear in mind that it's been a long time since I looked into the C pre-parser.
#if allows for arbitrary Boolean logic, eg #if build_env "local"
#if defined us to see if a thing has been defined already, quite often used to make sure that a header file is only imported once, redeclaration of headers is a compiler error. Eg. #if !defined(__SOME_UNIQUE_FILE_IDENTIFIER) (then define the class) then write your #endif
#ifdef wasn't always standard and was added later in the ANSI spec be a keyword, it's shorthand for the same thing as #if defined. -
This is my understanding anyway, I'm going with the principal that someone who knows more will be more likely to post a rebuttal (which I encourage).
The standard agrees with you that
#ifdef
and#ifndef
are equal to#if defined
and#if !defined
, but your#if build_env "local"
example does not make much sense the me.#if
only takes constant expressions which can be evaluated at translation time. It can't compare strings.You are correct, I had misremembered how it works. It can evaluate build arguments, but they have to be numerical. However you can define new variables (in the pre-processor, not in code - ie, after a #) to replace those numbers, to make the intent clearer. Eg.
#define DEBUG_1 1
#define DEBUG_2 2
#if DEBUG_LEVEL >= DEBUG_1
Then pass the build arg DEBUG_LEVEL at compile time
More info here:
https://learn.microsoft.com/en-us/cpp/preprocessor/hash-if-hash-elif-hash-else-and-hash-endif-directives-c-cpp?view=msvc-170
Edit: formatting of code snippet