typedef vs #define

Discussion in 'C Programming' started by riccardo, Oct 5, 2010.

  1. riccardo

    riccardo Guest

    Hi NG,
    could you please tell me which is the best option among the following two?

    /* 1) */ typedef int foodata1;

    /* 2) */ #define foodata2 int

    Thanks in advance.
     
    riccardo, Oct 5, 2010
    #1
    1. Advertising

  2. On 2010-10-05 14:20, riccardo wrote:
    > Hi NG,
    > could you please tell me which is the best option among the following two?
    >
    > /* 1) */ typedef int foodata1;
    >
    > /* 2) */ #define foodata2 int
    >
    > Thanks in advance.


    http://c-faq.com/decl/typedefvsdefine.html


    /August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
     
    August Karlstrom, Oct 5, 2010
    #2
    1. Advertising

  3. riccardo <> writes:

    > could you please tell me which is the best option among the following two?
    >
    > /* 1) */ typedef int foodata1;
    >
    > /* 2) */ #define foodata2 int


    Most people will go for 1. It is the proper way to write a type
    definition. There will be circumstances where 2 is useful (for example
    if you need to string-ify the type or you need to construct macros that
    incorporate the type name) but you don't give any context and these are
    unusual situations.

    --
    Ben.
     
    Ben Bacarisse, Oct 5, 2010
    #3
  4. riccardo

    John Bode Guest

    On Oct 5, 7:20 am, riccardo <> wrote:
    > Hi NG,
    > could you please tell me which is the best option among the following two?
    >
    >    /* 1) */ typedef int foodata1;
    >
    >    /* 2) */  #define foodata2 int
    >
    > Thanks in advance.


    If you need to create a synonym for a type name, use typedef (option
    1); that's what it's there for. For simple types like "int" or
    "double" it's hard to see a difference, but when you have something
    truly ugly like

    double *(*(*f)())[10];

    the typedef solution is the safer and easier way to go:

    typedef double *(*ptrArrPtrDbl)[10]; // ptrArrPtrDbl== double
    *(*)[10]
    typedef ptrArrPtrDbl (*uglyFuncPtr)(); // uglyFuncPtr == double
    *(*(*)())[10]
    ...
    uglyFuncPtr myUglyFuncPtr;
    ...
    dblPtrArrPtr myArr = myUglyFuncPtr(); // or (*myUglyFuncPtr)();
     
    John Bode, Oct 5, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. O Plameras
    Replies:
    10
    Views:
    548
    Keith Thompson
    Dec 19, 2005
  2. ramu

    #define and typedef

    ramu, Jan 18, 2006, in forum: C Programming
    Replies:
    2
    Views:
    323
    Jaspreet
    Jan 18, 2006
  3. robin liu
    Replies:
    3
    Views:
    824
    Robin Liu
    Apr 21, 2006
  4. Vladimir Oka

    define versus typedef

    Vladimir Oka, Jun 8, 2006, in forum: C Programming
    Replies:
    10
    Views:
    526
    Christian Christmann
    Jun 8, 2006
  5. oor
    Replies:
    0
    Views:
    1,358
Loading...

Share This Page