Thread: Checking a macro's sign in order to define other macro

  1. #1
    Registered User Egalestrenge's Avatar
    Join Date
    Dec 2015
    Location
    Spain
    Posts
    14

    Checking a macro's sign in order to define other macro

    Greetings. As the title suggests, I would like to define a macro cheking out first the sign of other macro. I've never used macros before, so I don't even really know if this is possible to do. Here's the idea:

    Code:
    #if V<0#define DT -(DX / (2*V)) // CFL condition for the linear transport equation
    #endif /* V<0 */
    
    
    #if V>=0
    #define DT (DX / (2*V)) // CFL condition for the linear transport equation
    #endif /* V>=0 */
    where "V" is another macro defined on other header file. I'm sure that this use of "#if" and "#define" is not correct, but how could I get this expression of "DT" in a MACRO-definition way?

    Thank you very much in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,679
    Oh for the sake of simple experimentation.....
    Code:
    $ cat foo.c
    #include<stdio.h>
    #include<stdlib.h>
    
    #define V 10
    
    int main()
    {
    #if V < 0
      printf("V is negative\n");
    #else
      printf("V is positive\n");
    #endif
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    V is positive
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Egalestrenge's Avatar
    Join Date
    Dec 2015
    Location
    Spain
    Posts
    14
    Quote Originally Posted by Salem View Post
    Oh for the sake of simple experimentation.....
    Code:
    $ cat foo.c
    #include<stdio.h>
    #include<stdlib.h>
    
    #define V 10
    
    int main()
    {
    #if V < 0
      printf("V is negative\n");
    #else
      printf("V is positive\n");
    #endif
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    V is positive
    Oh, so indeed I can use "#if" and "#else" statements. That's good, but it seems that I cannot use them outside of enviroments. And that's my problem, because I want to define a macro depending of the sign of "V" outside the main() or another enviroment. Is there a way to do it?

    And thank you for your response, by the way .

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,679
    I've still no idea what your problem is.
    Code:
    $ cat foo.c
    #include<stdio.h>
    #include<stdlib.h>
    
    #define V 10
    #if V < 0
    #define BAR 10
    #else
    #define BAR 10000
    #endif
    
    int main()
    {
      printf("V=%d, BAR=%d\n", V, BAR );
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    V=10, BAR=10000
    > That's good, but it seems that I cannot use them outside of enviroments.
    What does 'environments' mean?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Egalestrenge's Avatar
    Join Date
    Dec 2015
    Location
    Spain
    Posts
    14
    Quote Originally Posted by Salem View Post
    I've still no idea what your problem is.
    You know what? Me either . I've written again the code and now it works...

    Anyway, thank you very much for your attention!

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Learning to use the pre-processor, and use it well, is a good thing. However...
    Quote Originally Posted by Egalestrenge View Post
    You know what? Me either . I've written again the code and now it works...
    This highlights an important point: debugging macros can be an incredibly complex and difficult process. The pre-processor is basically a fancy text substitution engine. Macro bugs, since they are done in the pre-processing stage, can lead to very strange compile-time errors that are difficult to pinpoint. If you don't absolutely need something to be a macro, then don't make it one. Just write a function that does what you want. Macro info is often not available when using a debugger, and even when it is, it's not always as intuitive as to what it's doing, compared to regular code.

    And always remember, pre-processor code is still code. As much as possible, you should follow all the best practices you would for normal code: formatting/indentation, variable naming, and using the correct programming constructs. Salem pointed out to you that #else exists (there's also #elif, #ifdef, and a whole lot more). Make sure to use the right tool for the right job. Shameless self-plug for my blog-that-never-really-was: Monkey C, Monkey Do: Good Preprocessor Usage. Hopefully you find that useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How overload a define macro?
    By 6tr6tr in forum C++ Programming
    Replies: 19
    Last Post: 04-24-2008, 03:52 PM
  2. Is it possible to set type for define macro?
    By 6tr6tr in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2008, 01:32 PM
  3. Newbie question about the define macro
    By lawina in forum C++ Programming
    Replies: 11
    Last Post: 05-18-2006, 06:47 PM
  4. Tricky #define macro...
    By willkoh in forum Windows Programming
    Replies: 4
    Last Post: 04-06-2005, 12:09 PM
  5. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 05:01 PM

Tags for this Thread