Thread: What is #if and #elif when using macros??

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    67

    What is #if and #elif when using macros??

    I've been reading up on what macros are and how their used, but then I came across #if and #elif and I have no idea what their about and how their used? Can any one explain to me their use and purpose and also post a program to show how to use them and describe why?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Code:
    #if this is true
      compile this line;
    #elif that is true
      compile that line;
    #else
      compile me instead;
    #endif
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Quote Originally Posted by GReaper View Post
    Code:
    #if this is true
      compile this line;
    #elif that is true
      compile that line;
    #else
      compile me instead;
    #endif
    That looks really similiar to if statements.
    So.. could one look like this?:

    Code:
    #include <stdio.h>
    #define EXAMPLE 100
    
    int main()
    {
      
    #if EXAMPLE == 100
    printf("The Macro is equal to 100!");
    #elif EXAMPLE > 100
    printf(This can't be right? Because I defined EXAMPLE as 100????");
    #else
    printf("This also can't be right!?");
    #endif
    
    }
    Would that be possible to do, and if so is it even correctly done? What other program examples could I do this in? Calculator? Pinball? ect..

    Thank you for your previous comment too, I greatly appreciate it!

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, its name being conditional compilation, it's useful when you want your code do be configurable and portable. Configurable in the sense that the end-user would choose how the code would be compiled, eg defining USE_DOUBLE if he/she wanted the program to use double precision. Portable meaning that it would be able to compile on multiple platforms with minimal or even no changes at all. Macros defined by the compiler can be used at using eg Linux system functions instead of Windows ones.

    About your code, yes that is possible to do, but keep in mind that you're checking at compile time, at the preprocessing state.
    If you've read header code before, you may have noticed that it's almost always like this:
    Code:
    #ifndef FILENAME_H // Or similar
    #define FILENAME_H
    
    // Code here
    
    #endif
    That is a guard, and it guarantees that the file will be included only once at each translation unit. That is also conditional compilation, in a way.

    I hope my info was helpful.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use Macros??
    By greg677 in forum C Programming
    Replies: 3
    Last Post: 05-05-2010, 07:19 AM
  2. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  3. Macros
    By jsbeckton in forum C Programming
    Replies: 11
    Last Post: 12-02-2005, 02:10 AM
  4. Macros Using #s
    By Krak in forum C++ Programming
    Replies: 21
    Last Post: 07-18-2005, 01:03 AM
  5. macros
    By rpc2005 in forum C Programming
    Replies: 23
    Last Post: 06-14-2005, 08:56 AM