Thread: difference between #define and #define ()

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    45

    difference between #define and #define ()

    hi,

    what is the difference between the 2 constructions

    #define (value), and,
    #define value

    thx in advance!

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    The first one can be used to declare constants

    Code:
    #define PI 3.14159
    Whereas the second one can be used to declare functions

    Code:
    #define RADTODEG(x) ((x) * 57.29578)
    or

    Code:
    #define MIN(a,b) ((a)>(b)?(b):(a))
    Note in this one, every element must be nested inside parentheses in order to avoid faulty comparison.

    eg: if it were defined like this

    Code:
    #define MIN(a,b) (a>b?b:a)
    then the following code -

    Code:
    int i = MIN(1,1+1);
    which will not work as expected due to operator precedence and would expand to
    Code:
    1 > 1 + 1 ? 1 + 1 : 1;

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Spidey View Post
    The first one can be used to declare constants
    ...
    Whereas the second one can be used to declare functions
    It would be better worded: The first one declares a macro which you do not pass arguments to, while the second allows you to pass arguments.

    Neither however, actually declares a function. It may seem like it's function-like, but it's not the same as a function. For one, there is no type checking...

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    It would be better worded: The first one declares a macro which you do not pass arguments to, while the second allows you to pass arguments.

    Neither however, actually declares a function. It may seem like it's function-like, but it's not the same as a function. For one, there is no type checking...
    Ah, Yes. My Mistake.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    thx guys. but if i get things like

    Code:
    #define OK (0)
    #define NOT_OK (-1)
    does that mean you can actually pass arguments to 'OK' and 'NOT_OK'? i was under the impression that these are constants.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bored_guy
    does that mean you can actually pass arguments to 'OK' and 'NOT_OK'?
    No. The space between the macro name and the opening parenthesis means that the macro is not a function style macro.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by bored_guy View Post
    thx guys. but if i get things like

    Code:
    #define OK (0)
    #define NOT_OK (-1)
    does that mean you can actually pass arguments to 'OK' and 'NOT_OK'? i was under the impression that these are constants.
    Every instance of OK would simply be replaced by (0) in this case. Nothing different from the #define sort.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    hey thx ppl that pretty much clears it

  9. #9
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    It is good practice to put parentheses around your definitions if they're anything more than a simple number. Then you can be sure that the definition is evaluated as a single unit wherever the preprocessor drops it in.

    For example:
    #define A 1
    #define B 2
    #define C (A + B)
    You'd want to make sure to preserve the desired order of operations wherever C is expanded in the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Implenting Function Keys
    By frassunit in forum C Programming
    Replies: 14
    Last Post: 11-12-2008, 11:31 AM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM