Thread: Preprocessor ?

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    1

    Question Preprocessor ?

    I recently came across a #define in a C program I was looking at and I was not quite sure why they surrounded the defined value with () as seen below. To me they look both essentially the same.

    Code:
    #define NUM_VIC_VECT_PRIORITY_REGS  (32)
    
    #define VIC_VECT_PRIORITY_HIGHEST   0
    I am just curious why they did not do

    Code:
    #define NUM_VIC_VECT_PRIORITY_REGS  32
    instead of

    Code:
    #define NUM_VIC_VECT_PRIORITY_REGS  (32)
    Does it make any difference or is there some special meaning here?


  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,595
    It is a habit I did for a while; that is needed for compound values like (3+1).
    I often have two MACROs where the second one is just 1 more.

    Code:
    #define MAX_STRING_SIZE 31
    #define MAX_STRING_SIZE_WITH_NUL   (MAX_STRING_SIZE+1)
    I have never had something weird happen without () for non compound values. I am not an expect; so, it might be possible.

    But, I have had issues with compound values without ().

    Tim S.
    Last edited by stahta01; 03-09-2013 at 05:42 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ZK57 View Post
    Does it make any difference or is there some special meaning here?
    The main difference is that the form with brackets prevents the macro being concatenated by other macros.

    The circumstances in which you would want other macros to concatenate macros of this form are rare, and the brackets prevents that occurring by accident.

    Personally, rather than preventing concatenation of macros, I think it is better to minimise ANY use of macro trickery. If you're not using excessive macro trickery, no need for additional macro trickery to prevent accidents.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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