C preprocessor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • francesco

    #1

    C preprocessor

    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.
  • jacob navia

    #2
    Re: C preprocessor

    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)


    Comment

    • Emmanuel Delahaye

      #3
      Re: C preprocessor

      In 'comp.lang.c', r.nospam@virgil io.it (francesco) wrote:
      [color=blue]
      > 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.[/color]

      Yes. You can't use a preprocessor directive in a macro.
      [color=blue]
      > How should I do?[/color]

      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/

      Comment

      • francesco

        #4
        Re: C preprocessor

        > You could use enum() instead of #define[color=blue]
        >
        > #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.)[/color]

        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.

        Comment

        • Chris Dollin

          #5
          Re: C preprocessor

          francesco wrote:
          [color=blue][color=green]
          >> 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.)[/color]
          >
          > OK, but the problem is the same: enums are variables,[/color]

          They're not.
          [color=blue]
          > so occupy memory space,[/color]

          They need not (and typically won't).
          [color=blue]
          > 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.[/color]

          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-newsgrou...mp.lang.c.html
          C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html

          Comment

          • Emmanuel Delahaye

            #6
            Re: C preprocessor

            In 'comp.lang.c', r.nospam@virgil io.it (francesco) wrote:
            [color=blue][color=green]
            >> 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.)[/color]
            >
            > OK, but the problem is the same: enums are variables, so occupy memory[/color]

            No. enums are constants. Don't mix a constant literal defined by an enum and
            a variable of type enum.
            [color=blue]
            > 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.[/color]

            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/

            Comment

            • francesco

              #7
              Re: C preprocessor

              > 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!

              Comment

              Working...