C preprocessor

Discussion in 'C Programming' started by francesco, Jul 2, 2004.

  1. francesco

    francesco Guest

    I'm programming an embedded system, on a Fujitsu microcontroller.
    Stated that I'm not sure about constants optimization, I'm trying to
    declare constants in two ways, one for debug version and one for
    release version.

    In debug, I would like to use const declaration, as I found it more
    safer:
    for example: const uint8_t SPEED_FAST = 10;

    In release, I would like to use #define, as the constant value is
    substituted inside the code, thus optimizing RAM usage:
    for example: #define SPEED_FAST 10

    I've written this macro:

    #ifdef NDEBUG
    #define constant(type, name, value) #define name value
    #else
    #define constant(type, name, value) const type name = value;
    #endif /* NDEBUG */

    Actually, this code doesn't work as in the NDEBUG version I read this
    error:
    Misplaced '#' or '##' in a macro definition.

    How should I do?

    Thanks.
     
    francesco, Jul 2, 2004
    #1
    1. Advertising

  2. francesco

    jacob navia Guest

    You can't make a #define directive within a macro.
    The standard says:

    The resulting completely macro-replaced preprocessing token sequence is not
    processed
    as a preprocessing directive even if it resembles one,

    (6.10.3.4.3, page 155)
     
    jacob navia, Jul 2, 2004
    #2
    1. Advertising

  3. In 'comp.lang.c', (francesco) wrote:

    > I'm programming an embedded system, on a Fujitsu microcontroller.
    > Stated that I'm not sure about constants optimization, I'm trying to
    > declare constants in two ways, one for debug version and one for
    > release version.
    >
    > In debug, I would like to use const declaration, as I found it more
    > safer:
    > for example: const uint8_t SPEED_FAST = 10;
    >
    > In release, I would like to use #define, as the constant value is
    > substituted inside the code, thus optimizing RAM usage:
    > for example: #define SPEED_FAST 10
    >
    > I've written this macro:
    >
    > #ifdef NDEBUG
    > #define constant(type, name, value) #define name value
    > #else
    > #define constant(type, name, value) const type name = value;
    > #endif /* NDEBUG */
    >
    > Actually, this code doesn't work as in the NDEBUG version I read this
    > error:
    > Misplaced '#' or '##' in a macro definition.


    Yes. You can't use a preprocessor directive in a macro.

    > How should I do?


    You could use enum() instead of #define

    #ifdef NDEBUG
    #define constant(type, name, value) enum {name = value}
    #else
    #define constant(type, name, value) const type name = value
    #endif /* NDEBUG */

    (Note : trailing ';' removed. They belong to the coding.)

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jul 2, 2004
    #3
  4. francesco

    francesco Guest

    > You could use enum() instead of #define
    >
    > #ifdef NDEBUG
    > #define constant(type, name, value) enum {name = value}
    > #else
    > #define constant(type, name, value) const type name = value
    > #endif /* NDEBUG */
    >
    > (Note : trailing ';' removed. They belong to the coding.)


    OK, but the problem is the same: enums are variables, so occupy memory
    space, while using define I could substitute them inside the code and
    occupy no space. In this sense, it is better to use constants, as they
    occupy just an unsigned char space (generally one byte), while enums
    are int and generally occupy 4 byte on a 32 bit machine.

    I begin to think that my problem is without solution.

    Thanks anyway.
     
    francesco, Jul 5, 2004
    #4
  5. francesco

    Chris Dollin Guest

    francesco wrote:

    >> You could use enum() instead of #define
    >>
    >> #ifdef NDEBUG
    >> #define constant(type, name, value) enum {name = value}
    >> #else
    >> #define constant(type, name, value) const type name = value
    >> #endif /* NDEBUG */
    >>
    >> (Note : trailing ';' removed. They belong to the coding.)

    >
    > OK, but the problem is the same: enums are variables,


    They're not.

    > so occupy memory space,


    They need not (and typically won't).

    > while using define I could substitute them inside the code and
    > occupy no space. In this sense, it is better to use constants, as they
    > occupy just an unsigned char space (generally one byte), while enums
    > are int and generally occupy 4 byte on a 32 bit machine.


    Objects of an enum type might indeed be four bytes wide. But that
    doesn't mean that every enum constant occupies four bytes of
    variable (or constant) space, or that it needs four bytes of
    code to load.

    OT though it may be, what does your compiler do with

    enum Spoo { zero, one, seventeen = 17, fortyTwo = 42 };

    int example( int a, int b, int c, int d )
    {
    int A = a + zero, B = b + one;
    int C = c + seventeen, d = d + fortyTwo;
    return A + B + C + D;
    }

    ??


    --
    Chris "electric hedgehog" Dollin
    C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
     
    Chris Dollin, Jul 5, 2004
    #5
  6. In 'comp.lang.c', (francesco) wrote:

    >> You could use enum() instead of #define
    >>
    >> #ifdef NDEBUG
    >> #define constant(type, name, value) enum {name = value}
    >> #else
    >> #define constant(type, name, value) const type name = value
    >> #endif /* NDEBUG */
    >>
    >> (Note : trailing ';' removed. They belong to the coding.)

    >
    > OK, but the problem is the same: enums are variables, so occupy memory


    No. enums are constants. Don't mix a constant literal defined by an enum and
    a variable of type enum.

    > space, while using define I could substitute them inside the code and
    > occupy no space. In this sense, it is better to use constants, as they
    > occupy just an unsigned char space (generally one byte), while enums
    > are int and generally occupy 4 byte on a 32 bit machine.
    >
    > I begin to think that my problem is without solution.


    Don't surrender so easily. I have given a solution to you. They are others.

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jul 5, 2004
    #6
  7. francesco

    francesco Guest

    > Don't surrender so easily. I have given a solution to you. They are others.

    Sorry to have given the feeling that I was surrending. I'm searching
    again.

    Anyway, I would like to thank you for your valuable help. Surely this
    is a solution and I'm sorry if I made you believe not to be gratefull.

    Thanks guy!
     
    francesco, Jul 6, 2004
    #7
    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. Chris P
    Replies:
    0
    Views:
    441
    Chris P
    Oct 28, 2003
  2. The Weiss Family

    VHDL Preprocessor

    The Weiss Family, Jul 14, 2004, in forum: VHDL
    Replies:
    2
    Views:
    3,227
    The Weiss Family
    Jul 14, 2004
  3. =?Utf-8?B?SSBhbSBTYW0=?=

    C# Preprocessor

    =?Utf-8?B?SSBhbSBTYW0=?=, Mar 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,580
    =?Utf-8?B?SSBhbSBTYW0=?=
    Mar 13, 2005
  4. Replies:
    0
    Views:
    2,678
  5. Cronus
    Replies:
    1
    Views:
    676
    Paul Mensonides
    Jul 15, 2004
Loading...

Share This Page