Operational and functional differences - #define vs typedef ?

Discussion in 'C Programming' started by O Plameras, Dec 15, 2005.

  1. O Plameras

    O Plameras Guest

    Are there differences in terms of functionality
    of,

    #define
    and
    typedef ?

    By the above I mean any instance when the outcome
    obtained by running two versions (1) and (2) below
    of C codes are different ?

    For example,

    (1) In one C code, it has:

    #define u_ unsigned
    #define uc_ unsigned char

    (2) The same C code is re-writtened to:

    typedef unsigned u_;
    typdef unsigned char uc_;

    TIA.

    O Plameras
    O Plameras, Dec 15, 2005
    #1
    1. Advertising

  2. O Plameras <> writes:
    > Are there differences in terms of functionality
    > of,
    >
    > #define
    > and
    > typedef ?
    >
    > By the above I mean any instance when the outcome
    > obtained by running two versions (1) and (2) below
    > of C codes are different ?
    >
    > For example,
    >
    > (1) In one C code, it has:
    >
    > #define u_ unsigned
    > #define uc_ unsigned char
    >
    > (2) The same C code is re-writtened to:
    >
    > typedef unsigned u_;
    > typdef unsigned char uc_;


    #define creates a macro. It's expanded literally at each point where
    it's used. Most syntax checking and parsing occurs *after* macros are
    expanded.

    A typedef creates an alias for a type. Macros and typedefs can be
    used similarly, but they're really completely different things.

    For example, given:
    #define u_ unsigned
    you can then declare
    _u long x;
    because the "_u" expands to "unsigned" before the type name
    "unsigned long" is parsed.

    In general, typedefs are better than macros for defining type aliases,
    because that's what they're designed for, and because the syntax of
    type declarations is such that a macro isn't always going to work.

    In this particular case, I'd recommend using neither macros nor
    typedefs. Someone reading your code is going to know what "unsigned"
    and "unsigned char" mean. He won't know what "u_" and "uc_" mean
    unless he looks up your typedef.

    The names "unsigned" and "unsigned char" are perfectly clear; there's
    no benefit in inventing other names for them. (Saving keystrokes
    isn't much of a benefit, especially when it makes your code more
    difficult to understand.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 15, 2005
    #2
    1. Advertising

  3. O Plameras

    bluejack Guest

    Re: Operational and functional differences - #define vs typedef ?

    Keith Thompson wrote:
    > In this particular case, I'd recommend using neither macros nor
    > typedefs. Someone reading your code is going to know what "unsigned"
    > and "unsigned char" mean. He won't know what "u_" and "uc_" mean
    > unless he looks up your typedef.


    How do you feel about using typedefs to ensure consistency across
    platforms,
    as in the case of establishing int32_t and int64_t types? (Naturally,
    that requires
    putting your typedef definitions within preprocessor conditionals that
    ascertain
    the nature of the platform...)

    -bluejack
    bluejack, Dec 15, 2005
    #3
  4. O Plameras

    pete Guest

    O Plameras wrote:
    >
    > Are there differences in terms of functionality
    > of,
    >
    > #define
    > and
    > typedef ?


    /* BEGIN new.c */

    #define VOID_POINTER void *

    typedef void * void_pointer;

    int main(void)
    {
    void_pointer a, b;
    VOID_POINTER c, d; /* This line won't compile */

    return 0;
    }

    /* END new.c */

    --
    pete
    pete, Dec 15, 2005
    #4
  5. Re: Operational and functional differences - #define vs typedef ?

    "bluejack" <> writes:
    > Keith Thompson wrote:
    >> In this particular case, I'd recommend using neither macros nor
    >> typedefs. Someone reading your code is going to know what "unsigned"
    >> and "unsigned char" mean. He won't know what "u_" and "uc_" mean
    >> unless he looks up your typedef.

    >
    > How do you feel about using typedefs to ensure consistency across
    > platforms, as in the case of establishing int32_t and int64_t types?
    > (Naturally, that requires putting your typedef definitions within
    > preprocessor conditionals that ascertain the nature of the
    > platform...)


    That's a great idea -- which is why it's part of the C99 standard's
    <stdint.h> header.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 15, 2005
    #5
  6. O Plameras wrote:
    >
    > Are there differences in terms of functionality
    > of,
    >
    > #define
    > and
    > typedef ?
    >
    > By the above I mean any instance when the outcome
    > obtained by running two versions (1) and (2) below
    > of C codes are different ?
    >
    > For example,
    >
    > (1) In one C code, it has:
    >
    > #define u_ unsigned
    > #define uc_ unsigned char
    >
    > (2) The same C code is re-writtened to:
    >
    > typedef unsigned u_;
    > typdef unsigned char uc_;


    Given your "u_" and "uc_" examples, your #define would allow:

    u_ long l; /* unsigned long */

    whereas the typedef wouldn't.

    On the other hand, consider:

    #define PCHAR char *
    and
    typedef char * PCHAR;

    and then see what happens with:

    PCHAR pt1, pt2;

    There are also times when #define simply can't be used:

    typedef void (*VoidVoidFuncPtr)(void);


    If you want to define a type, use typedef.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Dec 15, 2005
    #6
  7. Re: Operational and functional differences - #define vs typedef ?

    On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <> wrote:

    > That's a great idea -- which is why it's part of the C99 standard's
    > <stdint.h> header.


    Cool. I haven't researched all the differences between old ansi/k&r C's
    (which is what I use, mostly) and C99, but that's a nice thing to know,
    and a very handy feature.

    (Also, I switched off google's cruddy web interface. Correct quoting
    ensues.)

    -bluejack

    /* Work as though you live in the younger days of a better nation */
    Blunt Jackson, Dec 16, 2005
    #7
  8. O Plameras

    Richard Bos Guest

    Re: Operational and functional differences - #define vs typedef ?

    "Blunt Jackson" <> wrote:

    > On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <> wrote:
    >
    > > That's a great idea -- which is why it's part of the C99 standard's
    > > <stdint.h> header.

    >
    > Cool. I haven't researched all the differences between old ansi/k&r C's
    > (which is what I use, mostly)


    Note that those are two separate versions of C. First there was what is
    called K&R C, which was the C that Kernighan and Ritchie described in
    the _first_ edition of "The C Programming Language"; and then there was
    ANSI C89, which is (more or less) what is described in K&R _2_. (And
    ANSI C89 was adopted by ISO, so it's also ISO C90.)
    Lastly, there was (is now) ISO C99, which was also ratified by ANSI
    (being participant in ISO), so that's also ANSI C99.

    The differences between K&R C and C89 are larger, IYAM, than between
    both ISO Cs. For example, ANSI C89 was the one that introduced
    prototypes; they weren't in K&R C.

    > (Also, I switched off google's cruddy web interface. Correct quoting
    > ensues.)


    And there was much rejoicing.

    Richard
    Richard Bos, Dec 16, 2005
    #8
  9. Re: Operational and functional differences - #define vs typedef ?

    (Richard Bos) writes:
    > "Blunt Jackson" <> wrote:
    >
    >> On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <> wrote:
    >>
    >> > That's a great idea -- which is why it's part of the C99 standard's
    >> > <stdint.h> header.

    >>
    >> Cool. I haven't researched all the differences between old ansi/k&r C's
    >> (which is what I use, mostly)

    >
    > Note that those are two separate versions of C.


    For sufficiently large values of "two".

    [description of three versions of C snipped]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 16, 2005
    #9
  10. O Plameras

    Richard Bos Guest

    Re: Operational and functional differences - #define vs typedef ?

    Keith Thompson <> wrote:

    > (Richard Bos) writes:
    > > "Blunt Jackson" <> wrote:
    > >
    > >> On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <> wrote:
    > >>
    > >> > That's a great idea -- which is why it's part of the C99 standard's
    > >> > <stdint.h> header.
    > >>
    > >> Cool. I haven't researched all the differences between old ansi/k&r C's
    > >> (which is what I use, mostly)

    > >
    > > Note that those are two separate versions of C.

    >
    > For sufficiently large values of "two".


    No, the _old_ ANSI and K&R C are two versions. The _new_ ISO C99 is the
    third, which Blunt Jackson mentioned separately.

    Richard
    Richard Bos, Dec 19, 2005
    #10
  11. Re: Operational and functional differences - #define vs typedef ?

    (Richard Bos) writes:
    > Keith Thompson <> wrote:
    >> (Richard Bos) writes:
    >> > "Blunt Jackson" <> wrote:
    >> >> On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <> wrote:
    >> >> > That's a great idea -- which is why it's part of the C99 standard's
    >> >> > <stdint.h> header.
    >> >>
    >> >> Cool. I haven't researched all the differences between old ansi/k&r C's
    >> >> (which is what I use, mostly)
    >> >
    >> > Note that those are two separate versions of C.

    >>
    >> For sufficiently large values of "two".

    >
    > No, the _old_ ANSI and K&R C are two versions. The _new_ ISO C99 is the
    > third, which Blunt Jackson mentioned separately.


    Sorry, I read "those are two separate versions" as "there are two
    separate versions".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 19, 2005
    #11
    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. John A Grandy
    Replies:
    1
    Views:
    368
    John Timney \(MVP\)
    Jun 26, 2006
  2. dkilanko
    Replies:
    0
    Views:
    7,538
    dkilanko
    Aug 11, 2006
  3. robin liu
    Replies:
    3
    Views:
    821
    Robin Liu
    Apr 21, 2006
  4. Home_Job_opportunity
    Replies:
    0
    Views:
    497
    Home_Job_opportunity
    Jan 8, 2009
  5. Home_Job_opportunity
    Replies:
    0
    Views:
    584
    Home_Job_opportunity
    Jan 14, 2009
Loading...

Share This Page