Thread: Error: missing binary operator before token "("

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    3

    Error: missing binary operator before token "("

    Hi,


    I have a problem with #if in preprocesor (gcc-4.8.1).


    Such program works fine:


    Code:
    #include <stdio.h>
    #define NUMB1 8000000UL 
    #define NUMB2 64 
    
    
    #define RESULT (int)((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5) 
    
    
    int main(void) { 
      printf("%d",RESULT); 
      return 0; 
    }
    the RESULT has correct value 189.


    But when I want define warning reaction of value of RESULT like this:


    Code:
    #if RESULT > 100
      # warning "Some text ...."
    #endif

    the compiler does not compile the program:


    Code:
    #include <stdio.h>
    
    
    #define NUMB1 8000000UL
    #define NUMB2 64
    
    
    #define RESULT (int)((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5)
    
    
    #if RESULT > 100
      # warning "Some text ...."
    #endif
    
    
    int main(void) {
      printf("%d",RESULT);
      return 0;
    }

    errors:


    Code:
    prog.c:6:21: error: missing binary operator before token "("
     #define RESULT (int)((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5)
                         ^
    prog.c:8:5: note: in expansion of macro ‘RESULT’
     #if RESULT > 100
         ^

    What am I doing wrong?
    Last edited by JakubST; 08-02-2013 at 02:33 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    #define RESULT (int)((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5)
    try this instead
    Code:
    #define RESULT ((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5)
    Likely the prepossessing does NOT support cast to int.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Code:
    #include <stdio.h> 
    #define NUMB1 8000000UL
    #define NUMB2 64
     
    #define RESULT ((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5)
     
    #if RESULT > 100
      # warning "Some text ...."
    #endif
     
    int main(void) {
      printf("%12.6f",RESULT);
      return 0;
    }
    Errors:
    Code:
    prog.c:6:18: error: floating constant in preprocessor expression
     #define RESULT ((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5)
                      ^
    prog.c:8:5: note: in expansion of macro ‘RESULT’
     #if RESULT > 100
         ^
    prog.c:6:44: error: floating constant in preprocessor expression
     #define RESULT ((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5)
                                                ^
    prog.c:8:5: note: in expansion of macro ‘RESULT’
     #if RESULT > 100
         ^
    prog.c:6:50: error: floating constant in preprocessor expression
     #define RESULT ((1778E-6 * NUMB1 / NUMB2 * 0.85)+0.5)
                                                      ^
    prog.c:8:5: note: in expansion of macro ‘RESULT’
     #if RESULT > 100
         ^

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    error: floating constant in preprocessor expression
    Seems pretty clear, you can't use floating point constants in your pre-processor expressions. Sounds like your preprocessor isn't able to do floating point calculations to evaluate that expression and compare it to emit the conditional warning. You don't say which compiler you're using, but if it's GCC, you're out of luck: The C Preprocessor: Conditionals. I can't find anything in the standard that requires the implementation to either support or explicitly not support floats in preprocessor directives, so I don't know how universal this behavior is.

    Considering NUMB1 and NUMB2 are known ahead of time, you should know the value of result and thus whether the warning will be emitted. Perhaps precompute the value and #define your macro as a single constant instead of an expression. Don't even use a #if to check if the result > 100, you will know.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Thanks, now works fine.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by anduril462 View Post
    I can't find anything in the standard that requires the implementation to either support or explicitly not support floats in preprocessor directives, so I don't know how universal this behavior is.
    You need to read carefully (because it is not said outright, so it is necessary to track through the specification of a number of elements of syntax) but it is in the standard.

    Even if it wasn't standard, the results of all floating point operations are - strictly speaking, due to limited precision and accuracy - implementation defined. Enough programmers get confused by the fact that 0.1 and 1.0/10.0 are not necessarily equal. Imagine the confusion if that uncertainty affected the preprocessor (which can completely change the behaviour of a program).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-21-2012, 11:50 PM
  2. Replies: 10
    Last Post: 08-09-2012, 12:48 PM
  3. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  4. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  5. Replies: 2
    Last Post: 06-29-2007, 07:55 AM