Macros the devil

This is a discussion on Macros the devil within the C++ Programming forums, part of the General Programming Boards category; I want to use a macro defined in one c file into another c file. For example if file a.c ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    1

    Macros the devil

    I want to use a macro defined in one c file into another c file.

    For example if file a.c looks like:

    #define TEN 10


    So Now if i want to use TEN in b.c how can i do it?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You would have to #include a.c in b.c. But that is not considered good practice. A better alternative would be to move the #define to a header file and include that header in both a.c and b.c. Another solution if you cannot move the #define would be just to duplicate it inside b.c. This is a maintenance headache, but if it is unlikely to change and you can't move it to a header it's not so bad.

    BTW, there is a separate forum for C questions, so questions like these would go better in there. If this were C++ I'd say don't use a #define.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    #include "myfile.h"

    not

    #include <myfile.h>

    for user-made header files.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by reachnitinb View Post
    I want to use a macro defined in one c file into another c file.
    Don't do that. If a macro must be used in more than one place, it should be in a header file, not a source file.

    For example if file a.c looks like:

    #define TEN 10
    I hope that's just an example and not something you're really doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 02:51 AM
  3. Macros vs Inline Functions
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 08-02-2007, 11:51 AM
  4. Check out DEVIL Screensaver System!
    By hartwork in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-31-2005, 06:28 PM
  5. Macros Using #s
    By Krak in forum C++ Programming
    Replies: 21
    Last Post: 07-18-2005, 01:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21