typedef v/s macro

Discussion in 'C Programming' started by Tagore, Dec 24, 2008.

  1. Tagore

    Tagore Guest

    In my C text book, there is a section where use of typedef is compared
    against macro. In this section, it uses a sentence as :

    "Difference Between typedef int x[10] and #define x int[10]"

    There is no explanation of macro definition given in above sentence.
    I want to know that whether #define x int[10] is valid? If yes, then
    how it is used?
     
    Tagore, Dec 24, 2008
    #1
    1. Advertising

  2. Tagore <> writes:

    > In my C text book, there is a section where use of typedef is compared
    > against macro. In this section, it uses a sentence as :
    >
    > "Difference Between typedef int x[10] and #define x int[10]"
    >
    > There is no explanation of macro definition given in above sentence.
    > I want to know that whether #define x int[10] is valid?


    It's valid in the sense that it's perfectly legal.

    > If yes, then
    > how it is used?


    Generally, it shouldn't be.

    "typedef int x[10];" (note that you need the semicolon) declares the
    name "x" an an alias for the type "int[10]", i.e., array of 10 ints.

    "#define x int[10]" (note the lack of a semicolon) works on a lower
    level. It's processed during an early stage of compilation, when
    types don't yet exist. It causes the identifier "x" to be replaced by
    the sequence of 4 tokens:
    int [ 10 ]
    regardless of whether that sequence makes sense in context.

    If you want to declare an object of type "x", the typedef lets you do
    so:
    x obj;
    You can even declare pointers to type x, arrays of type x, and so
    forth:
    x arr[20]; /* an array of 20 "x"s */
    x *ptr; /* ptr is a pointer to an x */

    If x is instead defined by the above #define directive, then
    x obj;
    will expand to
    int[10] obj;
    which is a syntax error. Macro expansion works on token sequences; it
    ignores any higher-level syntax.

    The comp.lang.c FAQ is at <http://www.c-faq.com/>. See question 1.13,
    and follow the links to other questions.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 24, 2008
    #2
    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. Dead RAM
    Replies:
    20
    Views:
    1,120
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    582
    Jack Klein
    Sep 21, 2003
  3. Regarding typedef and macro

    , Dec 13, 2006, in forum: C Programming
    Replies:
    15
    Views:
    431
    Eric Sosman
    Dec 15, 2006
  4. oor
    Replies:
    0
    Views:
    1,358
  5. Uno

    typedef v macro

    Uno, Dec 1, 2010, in forum: C Programming
    Replies:
    3
    Views:
    427
    Keith Thompson
    Dec 5, 2010
Loading...

Share This Page