Thread: Cannot understand this C preprocessor function

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    55

    Cannot understand this C preprocessor function

    Hi, I am looking a third party library and I cannot understand how the following lines work.

    In .c file there is the #if directive
    Code:
    #if NRFX_CHECK(NRFX_SAADC_ENABLED)
    some code...
    #endif
    In .h file is the declaration of NRFX_CHECK. I cannot understand how this definition expands and work.. What happens if module_enabled = 1 and what happens when module_enabled = 0?

    Code:
    #define NRFX_CHECK(module_enabled)  (module_enabled)
    Thanks
    Nick

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,160
    There's this trick for "commenting out" large swathes of code when testing something:
    Code:
    #if 0
    some code...
    #endif
    This NRFX_CHECK function-style macro does the same thing. If NRFX_SAADC_ENABLED is 0, then NRFX_CHECK(NRFX_SAADC_ENABLED) resolves to (0), so you're "commenting out" the code. If NRFX_SAADC_ENABLED is 1, then NRFX_CHECK(NRFX_SAADC_ENABLED) resolves to (1), so you end up with #if (1) so the code is not "commented out".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    55
    I see, thanks!! So this macro
    Code:
    #define NRFX_CHECK(module_enabled)  (module_enabled)
    is like an if statement right? This is how you define if statements with macro?

    For example

    Code:
    #define function(variable) (variable)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,160
    No, that's just a function-style macro. The "if statement" is the #if macro, but of course it is a preprocessor directive, not an if statement.

    The NRFX_CHECK macro might be for flexibility so that in case you want to enable/disable regardless of the value of NRFX_SAADC_ENABLED (and possibly other macros), you can do so in just one place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    55
    Thank you for your responce,

    What I cannot understand, is how NRFX_CHECK resolves for either 1 or 0. Basically I so understand this part

    Cannot understand this C preprocessor function-screenshot_1-png

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,160
    Recall that macro substitution is a fairly simple replacement mechanism. So if you have a function-style macro like this:
    Code:
    #define ADD(x, y) ((x) + (y))
    and you use it like this:
    Code:
    printf("%d\n, ADD(1, 2));
    what happens is that after preprocessing, it is as if you had written:
    Code:
    printf("%d\n, ((1) + (2)));
    The "extra" parentheses are there because you might write something like this:
    Code:
    printf("%d\n, ADD(1, 2 + 3) * 4);
    which becomes:
    Code:
    printf("%d\n, ((1) + (2 + 3)) * 4);
    whereas if you had omitted the parentheses it would become:
    Code:
    printf("%d\n, 1 + 2 + 3 * 4);
    which is probably not what you wanted due to how precedence comes into play.

    So if we go back to NRFX_CHECK, you can see that it is exceedingly simple (and if you don't care about the flexibility or whatever else the author had in mind, you arguably don't need it). NRFX_CHECK(0) resolves to (0) because for this particular macro substition, 0 is module_enabled, and the rule says that the macro is replaced by (module_enabled), i.e., it becomes (0). That's it. Don't overthink.

    If you really really cannot understand, then consider this function:
    Code:
    int foo(int module_enabled)
    {
        return module_enabled;
    }
    We now call this function:
    Code:
    printf("%d\n", foo(0));
    What is the output, and why? The function-style macro works along similiar lines, though you must keep in mind that it is a macro, not a function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    55
    Wooo what a great explanation!! Not this concept is clear to me!! Thanks for your effort!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-18-2011, 07:14 PM
  2. Replies: 4
    Last Post: 02-20-2011, 06:29 PM
  3. I don't understand this for function.
    By Dorky King in forum C Programming
    Replies: 12
    Last Post: 06-12-2007, 03:18 PM
  4. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  5. I don't understand how to do this function.
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 12:23 PM

Tags for this Thread