Precompiler problem

This is a discussion on Precompiler problem within the C++ Programming forums, part of the General Programming Boards category; Hi, if define A as Code: #define A 100 and write Code: #if (A == 0) line1; #else line2; #endif ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    24

    Precompiler problem

    Hi, if define A as

    Code:
    #define A 100
    and write

    Code:
    #if (A == 0)
       line1;
    #else
       line2;
    #endif
    line1 is grayed out as is should be and line2 is active as it should be. But if I redefine A as

    Code:
    #define A 100.0
    then line1 becomes active and line2 is grayed out. Why? A is not equal to 0! How can I test in compile time if a defined constant is zero or not, even if it is not an integer? Thanks in advance.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    You can't.
    Quote Originally Posted by ISO/IEC 9899 6.10.1
    The expression that controls conditional inclusion shall be an integer constant expression ...
    This comes from C, but since C++ inherited the preprocessor from C, I think it is sufficient enough. (I actually skimmed the C++ standard after writing this the first time and came up with nothing but mentions of the preprocessor. So I'm referring to C.)

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    24
    > "The expression that controls conditional inclusion"

    I don't want to conditionally include a file, I just want to select between two snippets of code. But I take it all preprocessor conditions only can handle integer expressions, and that they can't do any arithmetic operations containing float values?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Quote Originally Posted by then View Post
    > "The expression that controls conditional inclusion"

    I don't want to conditionally include a file, I just want to select between two snippets of code.
    I cited the relevant section. The section the C standard that details source file inclusion is actually 6.10.2. If you want to look for yourself there is a sticky in the C forum with drafts of the standard in it.

    But I take it all preprocessor conditions only can handle integer expressions, and that they can't do any arithmetic operations containing float values?
    Testing for equality really isn't an arithmetic operation. Conditional inclusion has to be controlled by an integral constant expression, which means no floating point expressions.
    Last edited by whiteflags; 05-06-2012 at 08:05 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by then View Post
    > "The expression that controls conditional inclusion"

    I don't want to conditionally include a file, I just want to select between two snippets of code. But I take it all preprocessor conditions only can handle integer expressions, and that they can't do any arithmetic operations containing float values?
    Right, well it says "conditional inclusion". It doesn't say "conditional inclusion of files". So that means it even applies to conditionally including anything, even a single line of code which is exactly what you are doing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    24
    Quote Originally Posted by iMalc View Post
    Right, well it says "conditional inclusion". It doesn't say "conditional inclusion of files". So that means it even applies to conditionally including anything, even a single line of code which is exactly what you are doing.
    Ah, okay, that makes sense. Well, thanks for the answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 4
    Last Post: 10-16-2008, 08:30 PM
  3. Can the precompiler process JUST the #define s?
    By tripptripp5 in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 06:41 PM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21