Thread: Preprocessor Directivies

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    56

    Preprocessor Directivies

    Hi all,

    I am struggling to understand how to implement the below preprocessor directives. If for example I want to use the FORD code, how would I use/ access this code. Do I put this code in main.c, or in a header.h file. Then if I have a function initialize() in main.c, how would I call FORD from this function?

    Look forward to your feedback.

    Thanks Rocketman46

    Code:
    #if defined FORD
            
    	// code
    
    
    #elif defined HONDA
            
    	// code
    
    
    #endif
     
            // code
     
    #if defined FORD
    
    
            // code
    
    
    #elif defined HONDA
    
    
            // code
    
    
    #endif
            
    	// code
     
    #ifdef FORD
    
    
            // code

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Presumably, if you want the FORD code, you would #define FORD at some point, either prior to any header inclusion that requires it, or on the command line.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Hi,

    Thanks for your reply. So the below brief example, when initialize() is called, only the FORD code will be implemented.

    Thanks

    Rocketman46

    Code:
    #define FORD
    #include <stdio.h>
    
    void initialize(void)
    {
    
    #if defined FORD
    
        // code
    
    
    #elif defined HONDA
    
        // code
    
    
    #endif
    
        // code
    
    #if defined FORD
    
    
        // code
    
    
    #elif defined HONDA
    
    
        // code
    
    
    #endif
    
        // code
    
    #ifdef FORD
    
    
        // code
    
    }
    
    int main(void)
    {
       initialize();
    
       while(1)
       {
         // execute code
       }
    
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Yes, that would work, except the end of your initialize() function, which has no matching #endif.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Preprocessor help
    By royen in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2009, 10:56 AM
  2. preprocessor help
    By brunogon in forum C Programming
    Replies: 13
    Last Post: 06-16-2008, 11:14 AM
  3. preprocessor!!
    By nishu1988 in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2007, 11:31 PM
  4. Preprocessor
    By manutd in forum C++ Programming
    Replies: 5
    Last Post: 11-29-2006, 04:37 PM
  5. C preprocessor
    By Unregisterd in forum C Programming
    Replies: 1
    Last Post: 09-04-2001, 12:17 PM