Re: Defining a list of macro parameters.

Discussion in 'C Programming' started by Peter Nilsson, May 1, 2008.

  1. Paul wrote:
    > ...Could anyone suggest a way of making this work (using
    > the preprocessor)?
    >
    > #define MyList 1,2,3,4
    >
    > #define Sum(a,b,c,d) a+b+c+d
    >
    > x = Sum(MyList);
    >
    > (n.b. i can pass 'MyList' to a function, but i'd rather pass it to a macro).


    #include <stdio.h>

    #define MyList 1, 2, 3, 4
    #define Sum(a,b,c,d) ((a) + (b) + (c) + (d))

    #define SUM(LIST) Sum(LIST)

    int main(void)
    {
    printf("%d\n", SUM(MyList));
    return 0;
    }

    --
    Peter
     
    Peter Nilsson, May 1, 2008
    #1
    1. Advertising

  2. Peter Nilsson

    user923005 Guest

    On Apr 30, 4:11 pm, Peter Nilsson <> wrote:
    > Paul wrote:
    > > ...Could anyone suggest a way of making this work (using
    > > the preprocessor)?

    >
    > > #define MyList  1,2,3,4

    >
    > > #define Sum(a,b,c,d) a+b+c+d

    >
    > >  x = Sum(MyList);

    >
    > > (n.b. i can pass 'MyList' to a function, but i'd rather pass it to a macro).

    >
    >   #include <stdio.h>
    >
    >   #define MyList 1, 2, 3, 4
    >   #define Sum(a,b,c,d) ((a) + (b) + (c) + (d))
    >
    >   #define SUM(LIST) Sum(LIST)
    >
    >   int main(void)
    >   {
    >     printf("%d\n", SUM(MyList));
    >     return 0;
    >   }
    >



    C:\tmp>cl /W4 /Ox oopsie.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
    for 80x86
    Copyright (C) Microsoft Corporation. All rights reserved.

    oopsie.c
    oopsie.c(12) : warning C4003: not enough actual parameters for macro
    'Sum'
    oopsie.c(12) : error C2059: syntax error : ')'

    C:\tmp>type oopsie.c
    #include <stdio.h>

    #define MyList 1, 2, 3, 4
    #define Sum(a,b,c,d) ((a) + (b) + (c) + (d))


    #define SUM(LIST) Sum(LIST)


    int main(void)
    {
    printf("%d\n", SUM(MyList));
    return 0;
    }

    My effort:
    #include <stdio.h>
    int main(void) {puts("10");return 0;}

    It might be possible to accomplish the OP's task, but this one is even
    worse than the sizeof without sizeof as far as brain dead requests go.
    Someone needs to get "The C Puzzle Book" of the professor's desk and
    give him a real textbook.
    IMO-YMMV

    P.S.
    Let's head this one off at the pass:

    6.11: I came across some "joke" code containing the "expression"
    5["abcdef"] . How can this be legal C?

    A: Yes, Virginia, array subscripting is commutative in C. This
    curious fact follows from the pointer definition of array
    subscripting, namely that a[e] is identical to *((a)+(e)), for
    *any* two expressions a and e, as long as one of them is a
    pointer expression and one is integral. This unsuspected
    commutativity is often mentioned in C texts as if it were
    something to be proud of, but it finds no useful application
    outside of the Obfuscated C Contest (see question 20.36).

    References: Rationale Sec. 3.3.2.1; H&S Sec. 5.4.1 p. 124,
    Sec. 7.4.1 pp. 186-7.
     
    user923005, May 1, 2008
    #2
    1. Advertising

  3. Peter Nilsson

    user923005 Guest

    On May 1, 2:23 am, "Paul" <-> wrote:
    > Harald and Peter: Thank you for your solutions, cheers!
    >
    > "user923005" <> wrote...
    > >My effort:
    > >#include <stdio.h>
    > >int main(void) {puts("10");return 0;}
    > >It might be possible to accomplish the OP's task, but this one is even
    > >worse than the sizeof without sizeof as far as brain dead requests go.
    > >Someone needs to get "The C Puzzle Book" of the professor's desk and
    > >give him a real textbook.

    >
    > IMO-YMMV
    >
    > user, thankyou for your contribution, however the line puts("10") does not
    > work properly for my intended application (which i simplified for posting):
    >
    > #define GreenLed ( GPIOB, 10, OutputOpenDrain, InvertedOut)
    > #define InvertedOut &=~,|=
    > #define Port(Function, Name) Function Name
    > #define TurnOn(Port,Pin,Type,On,Off) Port->ODR On Bit(Pin)
    > Port(TurnOn,GreenLed);


    Don't use macros. Use functions.
     
    user923005, May 1, 2008
    #3
  4. user923005 wrote:
    > Peter Nilsson <> wrote:
    > > #include <stdio.h>
    > >
    > > #define MyList 1, 2, 3, 4
    > > #define Sum(a,b,c,d) ((a) + (b) + (c) + (d))
    > >
    > > #define SUM(LIST) Sum(LIST)
    > >
    > > int main(void)
    > > {
    > > printf("%d\n", SUM(MyList));
    > > return 0;
    > > }

    >
    > C:\tmp>cl /W4 /Ox oopsie.c
    > Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
    > for 80x86
    > Copyright (C) Microsoft Corporation. All rights reserved.
    >
    > oopsie.c
    > oopsie.c(12) : warning C4003: not enough actual parameters for macro
    > 'Sum'
    > oopsie.c(12) : error C2059: syntax error : ')'


    Don't let me stop you filing a bug report with Microsoft.

    --
    Peter
     
    Peter Nilsson, May 1, 2008
    #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. johny smith
    Replies:
    8
    Views:
    451
    Peter Koch Larsen
    Jul 2, 2004
  2. defining a macro

    , Sep 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    285
  3. Max TenEyck Woodbury

    Help with defining a macro?

    Max TenEyck Woodbury, Nov 7, 2006, in forum: C Programming
    Replies:
    4
    Views:
    348
    Max TenEyck Woodbury
    Nov 8, 2006
  4. Tagore

    Defining a global MACRO

    Tagore, Oct 8, 2009, in forum: C Programming
    Replies:
    6
    Views:
    1,379
    Keith Thompson
    Aug 21, 2013
  5. Noob
    Replies:
    3
    Views:
    153
    Philip Lantz
    Sep 1, 2013
Loading...

Share This Page