Odd Macro Problem

Discussion in 'C Programming' started by kid joe, Jan 29, 2011.

  1. kid joe

    kid joe Guest

    Hello all

    Long time no posting! I've mostly been using Java these couple years, but
    now I'm working on a C project again. The group seems a lot quieter than
    it used to be and lots of familiar names no longer appear in the recent
    post lists - quite a change!

    Anyway, I have a macro that can be used like:

    E(errno);

    but for reasons of brevity I frequently use the form:

    E(errno = EINVAL);

    But I would like to be able to have two definitions such that it evaluates
    to an active form like:

    printf("%d\n", errno);
    OR
    printf("%d\n", errno = EINVAL);

    and an inactive form that does nothing. The problem is, if I define the
    macro as simply:

    #ifdef INACTIVATE
    #define E(val)
    #else
    #define E(val) printf("%d\n", (val))
    #endif

    then the inactive form will not emit the 'errno = EINVAL' expression
    and set the value. If I define the inactive macro as:

    #define E(val) (val)

    that can emit a useless expression like:

    errno;

    Is there a way to write this macro to solve this problem and still
    satisfy all of the other requirements?

    --


    ( )
    ( )
    G O O D ( ) M O R N I N G ! !
    ( )
    ) )
    ( ( /\
    (_) / \ /\
    ________[_]________ /\/ \/ \
    /\ /\ ______ \ / /\/\ /\/\
    / \ //_\ \ /\ \ /\/\/ \/ \
    /\ / /\/\ //___\ \__/ \ \/
    / \ /\/ \//_____\ \ |[]| \
    /\/\/\/ //_______\ \|__| \
    / \ /XXXXXXXXXX\ \
    \ /_I_II I__I_\__________________\
    I_I| I__I_____[]_|_[]_____I
    I_II I__I_____[]_|_[]_____I
    I II__I I XXXXXXX I
    ~~~~~" "~~~~~~~~~~~~~~~~~~~~~~~~
     
    kid joe, Jan 29, 2011
    #1
    1. Advertising

  2. kid joe

    Eric Sosman Guest

    On 1/29/2011 4:17 PM, kid joe wrote:
    >
    > Anyway, I have a macro that can be used like:
    >
    > E(errno);
    >
    > but for reasons of brevity I frequently use the form:
    >
    > E(errno = EINVAL);
    >
    > But I would like to be able to have two definitions such that it evaluates
    > to an active form like:
    >
    > printf("%d\n", errno);
    > OR
    > printf("%d\n", errno = EINVAL);
    >
    > and an inactive form that does nothing. The problem is, if I define the
    > macro as simply:
    >
    > #ifdef INACTIVATE
    > #define E(val)
    > #else
    > #define E(val) printf("%d\n", (val))
    > #endif
    >
    > then the inactive form will not emit the 'errno = EINVAL' expression
    > and set the value. If I define the inactive macro as:
    >
    > #define E(val) (val)
    >
    > that can emit a useless expression like:
    >
    > errno;
    >
    > Is there a way to write this macro to solve this problem and still
    > satisfy all of the other requirements?


    Live with the useless expression: it does no harm, aside
    from (perhaps) eliciting a warning message from the compiler.

    A better approach, I think, is to step back and ask yourself
    why you want one macro to do two rather different things: be both
    a debugging aid (I guess) and an integral part of the program logic.
    You may get it to work, but the "do one thing well" principle seems
    to suggest you shouldn't try unless there's a compelling reason.

    "It's a floor wax *and* a dessert topping!" -- SNL

    "Simplify, simplify!" -- HDT

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 29, 2011
    #2
    1. Advertising

  3. kid joe

    Shao Miller Guest

    kid joe wrote:
    >
    > #ifdef INACTIVATE
    > #define E(val)
    > #else
    > #define E(val) printf("%d\n", (val))
    > #endif
    >


    What about:

    #ifdef INACTIVE
    # define E(val) ((void)(val))
    #else
    # define E(val) (printf("%d\n", (val)))
    #endif

    ? :)
     
    Shao Miller, May 8, 2011
    #3
  4. On May 8, 7:01 am, Shao Miller wrote:
    > kid joe wrote:
    >
    > >   #ifdef INACTIVATE
    > >   #define E(val)
    > >   #else
    > >   #define E(val) printf("%d\n", (val))
    > >   #endif

    >
    > What about:
    >
    >    #ifdef INACTIVE
    >    #  define E(val) ((void)(val))
    >    #else
    >    #  define E(val) (printf("%d\n", (val)))
    >    #endif
    >
    > ? :)


    Your solution, while valid, was posted more than three months after
    the previous post in the thread, which already suggested the same
    thing.
     
    Harald van Dijk, May 9, 2011
    #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. 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:
    585
    Jack Klein
    Sep 21, 2003
  3. Michael B Allen

    Odd Macro Problem

    Michael B Allen, Feb 27, 2005, in forum: C Programming
    Replies:
    1
    Views:
    311
    Eric Sosman
    Feb 27, 2005
  4. zets
    Replies:
    7
    Views:
    422
    Netocrat
    Aug 11, 2005
  5. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,114
    Richard Heathfield
    Feb 18, 2007
Loading...

Share This Page