Thread: How to use conditional?

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    How to use conditional?

    I have seen in many link there is more theoretical description about preprocessor

    C Preprocessor and Macros


    I want to experiment by writing some code

    Code:
    #include <stdio.h>#include "other.h"
     
    int main(void)
    {
    
    
        return 0;
    }
    Code:
        #ifdef x      printf("hello\n");      
       #endif

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    > I want to experiment by writing some code

    Good luck!
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Zeus_ View Post
    > I want to experiment by writing some code

    Good luck!
    I do not understand what should be in main function that's why left main empty. I don't found complete code that's why tried with my own code

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by abhi143 View Post
    I do not understand what should be in main function that's why left main empty. I don't found complete code that's why tried with my own code
    Header files should contain extern function and extern Global data declarations, #defines, other preprocessor directives, etc..., but not executable code such as the printf statement.

    I would write the code you provided as:
    Code:
    #include <stdio.h>
    
    #define X 1
    
    int main(void)
    {
    
     #ifdef X
       printf("hello\n");
     #endif
    
       return 0;
    }
    #define macros are by tradition, all UPPERCASE.

    "#define X 1" could be put in a header file, but with code this simple, why bother.

    Not the best example of the use of #ifdef, etc...

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by rstanley View Post

    #define macros are by tradition, all UPPERCASE.

    "#define X 1" could be put in a header file, but with code this simple, why bother.

    Not the best example of the use of #ifdef, etc...
    As much as I have seen, we keep the micro in the header file. So I created two files main.c other.h

    What could be best example for #ifdef, #if, #defined, #else and #elseif directives ?

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Three examples I can see used are, stubbing out code temporarily, header file choice, and inclusion of debug code during development:
    Code:
    #ifdef DEBUG
        // Code with debug statements
    #else
        // Production code without any debug statements
    #endif
    Code:
    #ifdef  _WIN32
        // Windows version of a header file
    #else
        // Linux header file inclusion
    #endif
    Code:
    #if 0
        ...
        /*
            The best way to stub out a multiple line comment
        */
        ...
    #endif
    There are other uses of these and other Preprocessor directives.

    Rather than point you to an online tutorial, you should study a good book on the C Programming Language that will explain all preprocessor directives, and many other features you need to learn and use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional Operator
    By Waqas Asad in forum C Programming
    Replies: 4
    Last Post: 12-19-2011, 09:11 AM
  2. conditional operator
    By nasser in forum C Programming
    Replies: 2
    Last Post: 10-23-2011, 07:18 PM
  3. Conditional Operator
    By markcocoa10 in forum C Programming
    Replies: 7
    Last Post: 10-15-2010, 06:22 PM
  4. Conditional Statements
    By strokebow in forum C Programming
    Replies: 30
    Last Post: 11-22-2006, 06:08 PM
  5. conditional
    By bazzano in forum C Programming
    Replies: 9
    Last Post: 03-25-2006, 10:36 AM

Tags for this Thread