#if CONSTANT == 1 checking problem

Discussion in 'C Programming' started by Timo, Oct 3, 2003.

  1. Timo

    Timo Guest

    I am having a problem with some preprocessor constant value checking:

    header.h:

    #define MY_CONSTANT 1

    file.c

    #if MY_CONSTANTTT == 1 // <- note the typo!!
    // dostuff...
    #endif

    I have always thought that C-compiler should give error message of
    that, because the constant name "MY_CONSTANTTT" doesn't exist.
    However,
    I got no error message with the line (Tested with MS Visual C++ 6.0
    and
    with one emdedded system compiler, no other compilers available now).

    What I am trying to achieve is to define some product features which
    should be included to compilation, in common header file. Usually this
    is done with #define USE_FEATURE_XXX. But if programmer writes
    "USE_FEATURE_XXX" incorrectly in C source, the code is not
    included in compilation, but no error message is got. Example:

    #ifdef USE_FEATURE_X // <-typo
    // do something
    #endif

    I am trying to catch possible typing error with this "#if
    USE_FEATURE_XXX == 1"
    syntax, but it seems not to be working. Have I misunderstood C
    standard, or are the compilers I use non-standard?

    BR,
    Timo
     
    Timo, Oct 3, 2003
    #1
    1. Advertising

  2. Timo <> scribbled the following:
    > I am having a problem with some preprocessor constant value checking:


    > header.h:


    > #define MY_CONSTANT 1


    > file.c


    > #if MY_CONSTANTTT == 1 // <- note the typo!!
    > // dostuff...
    > #endif


    > I have always thought that C-compiler should give error message of
    > that, because the constant name "MY_CONSTANTTT" doesn't exist.
    > However,
    > I got no error message with the line (Tested with MS Visual C++ 6.0
    > and
    > with one emdedded system compiler, no other compilers available now).


    Yes, this is perfectly standard. The #if directive considers
    nonexistent macro names as equal to 0, AFAIK.

    > What I am trying to achieve is to define some product features which
    > should be included to compilation, in common header file. Usually this
    > is done with #define USE_FEATURE_XXX. But if programmer writes
    > "USE_FEATURE_XXX" incorrectly in C source, the code is not
    > included in compilation, but no error message is got. Example:


    > #ifdef USE_FEATURE_X // <-typo
    > // do something
    > #endif


    > I am trying to catch possible typing error with this "#if
    > USE_FEATURE_XXX == 1"
    > syntax, but it seems not to be working. Have I misunderstood C
    > standard, or are the compilers I use non-standard?


    You have misunderstood C standard. I don't know (yet) of any way of
    doing what you want, but with some preprocessing trickery it might be
    possible.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "C++ looks like line noise."
    - Fred L. Baube III
     
    Joona I Palaste, Oct 3, 2003
    #2
    1. Advertising

  3. Timo wrote:
    > #define MY_CONSTANT 1
    >
    > file.c
    >
    > #if MY_CONSTANTTT == 1 // <- note the typo!!
    > // dostuff...
    > #endif
    >
    > I have always thought that C-compiler should give error message of
    > that, because the constant name "MY_CONSTANTTT" doesn't exist.


    Undefined identifiers in an "#if" directive are replaced with "0".

    Jeremy.
     
    Jeremy Yallop, Oct 3, 2003
    #3
  4. Timo wrote:
    >
    > I am having a problem with some preprocessor constant value checking:
    >
    > header.h:
    >
    > #define MY_CONSTANT 1
    >
    > file.c
    >
    > #if MY_CONSTANTTT == 1 // ?- note the typo!!
    > // dostuff...
    > #endif
    >
    > I have always thought that C-compiler should give error message of
    > that, because the constant name "MY_CONSTANTTT" doesn't exist.
    > However,
    > I got no error message with the line (Tested with MS Visual C++ 6.0
    > and
    > with one emdedded system compiler, no other compilers available now).
    >
    > What I am trying to achieve is to define some product features which
    > should be included to compilation, in common header file. Usually this
    > is done with #define USE_FEATURE_XXX. But if programmer writes
    > "USE_FEATURE_XXX" incorrectly in C source, the code is not
    > included in compilation, but no error message is got. Example:
    >
    > #ifdef USE_FEATURE_X // ?-typo
    > // do something
    > #endif
    >
    > I am trying to catch possible typing error with this "#if
    > USE_FEATURE_XXX == 1"
    > syntax, but it seems not to be working. Have I misunderstood C
    > standard, or are the compilers I use non-standard?
    >
    > BR,
    > Timo


    The precompiler does not distinguiush between symbols defined in the
    current file (or any #include'd file) and defined environment variables.
    Thus there is no way for it to know that you have made a typo.

    For example, if you typed "export MY_CONSTANTTT=1" in the command line,
    then compiled your program, the block delimited by "#if MY_CONSTANTTT ==
    1 " would be compiled.


    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Common User Interface Services
    M/S 2R-94 (206)544-5225
     
    Fred L. Kleinschmidt, Oct 3, 2003
    #4
  5. On Fri, 3 Oct 2003, Fred L. Kleinschmidt wrote:
    >
    > Timo wrote:
    > >
    > > #if MY_CONSTANTTT == 1 // ?- note the typo!!


    > The precompiler does not distinguiush between symbols defined in the
    > current file (or any #include'd file) and defined environment variables.
    > Thus there is no way for it to know that you have made a typo.
    >
    > For example, if you typed "export MY_CONSTANTTT=1" in the command line,
    > then compiled your program, the block delimited by "#if MY_CONSTANTTT ==
    > 1 " would be compiled.


    That's one of the more bizarre incorrect explanations I've heard
    here in a while. At first, I didn't even recognize the context;
    I thought Fred was giving a Lisp-environment answer in the wrong
    newsgroup or something. (Then a few seconds later I realized he
    was probably using a proprietary compiler on a (proprietary?)
    *nix system with 'export' being an OS shell command.)

    'export' is not a feature of the C language.

    > --
    > Fred L. Kleinschmidt
    > Boeing Associate Technical Fellow


    Hoo, dear.

    -Arthur
     
    Arthur J. O'Dwyer, Oct 3, 2003
    #5
  6. Timo

    nrk Guest

    [OT] Re: #if CONSTANT == 1 checking problem

    Arthur J. O'Dwyer wrote:

    >
    > On Fri, 3 Oct 2003, Fred L. Kleinschmidt wrote:
    >>
    >> Timo wrote:
    >> >
    >> > #if MY_CONSTANTTT == 1 // ?- note the typo!!

    >
    >> The precompiler does not distinguiush between symbols defined in the
    >> current file (or any #include'd file) and defined environment variables.
    >> Thus there is no way for it to know that you have made a typo.
    >>
    >> For example, if you typed "export MY_CONSTANTTT=1" in the command line,
    >> then compiled your program, the block delimited by "#if MY_CONSTANTTT ==
    >> 1 " would be compiled.

    >
    > That's one of the more bizarre incorrect explanations I've heard
    > here in a while. At first, I didn't even recognize the context;
    > I thought Fred was giving a Lisp-environment answer in the wrong
    > newsgroup or something. (Then a few seconds later I realized he
    > was probably using a proprietary compiler on a (proprietary?)
    > *nix system with 'export' being an OS shell command.)
    >
    > 'export' is not a feature of the C language.
    >


    Not only is it bizzare... It would probably drive me crazy (perhaps, because
    I've never used such a weird system ever). Talk about compiler messages
    that make you lose your hair fast, such an environment would just be loaded
    with them. As a matter of self-defense, I ask the OP to reveal the system
    under consideration, so I can keep a safe distance.

    -nrk.
     
    nrk, Oct 3, 2003
    #6
    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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,094
  2. Martin Magnusson
    Replies:
    2
    Views:
    508
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    564
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    339
    Keith Thompson
    Dec 14, 2006
  5. Replies:
    13
    Views:
    12,930
    Kai-Uwe Bux
    Jan 22, 2007
Loading...

Share This Page