Like Tree2Likes
  • 1 Post By stahta01
  • 1 Post By grumpy

Error: missing binary operator before token "("

This is a discussion on Error: missing binary operator before token "(" within the C Programming forums, part of the General Programming Boards category; Hi, I have a problem with #if in preprocesor ( gcc-4.8.1) . Such program works fine: Code: #include <stdio.h> #define ...

  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
    2,577
    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.
    anduril462 likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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,496
    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,295
    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).
    anduril462 likes this.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

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