37

Is the C preprocessor able to perform integer arithmetic?

E.g.:

#define PI 3.1416
#define OP PI/100
#define OP2 PI%100

Is there a way OP and/or OP2 get calculated in the preprocessing phase?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Schim
  • 381
  • 1
  • 3
  • 4
  • 9
    PI is not an integer in the example; therefore, neither OP nor OP2 would be integers if the preprocessor was forced to evaluate them. And the operands of '%' cannot be floating point numbers. – Jonathan Leffler Oct 13 '09 at 13:58

5 Answers5

45

Integer arithmetic? Run the following program to find out:

#include "stdio.h"
int main() {
    #if 1 + 1 == 2
        printf("1+1==2\n");
    #endif
    #if 1 + 1 == 3
        printf("1+1==3\n");
    #endif
}

The answer is "yes". There is a way to make the preprocessor perform integer arithmetic, which is to use it in a preprocessor condition.

Note however that your examples are not integer arithmetic. I just checked, and GCC's preprocessor fails if you try to make it do float comparisons. I haven't checked whether the standard ever allows floating point arithmetic in the preprocessor.

Regular macro expansion does not evaluate integer expressions. It leaves it to the compiler, as can be seen by preprocessing (-E in GCC) the following:

#define ONEPLUSONE (1 + 1)
#if ONEPLUSONE == 2
   int i = ONEPLUSONE;
#endif

The result is int i = (1 + 1); (plus probably some stuff to indicate source file names and line numbers and such).

spaceKelan
  • 67
  • 7
Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • 15
    Chris: We live in the future now, and while the flying cars are still missing, default return values from the main function have arrived! – Thomas Padron-McCarthy Oct 13 '09 at 14:19
  • 15
    @Chris: A close curly brace, reaching which "returns a value of 0" (5.1.2.2.3, Program Termination). If I'm not allowed to assume standard C in a question tagged "C", then what's the world coming to? Can't you find someone who is using //-style comments in code that might be seen by a C89 compiler, and complain about them instead? ;-) – Steve Jessop Oct 13 '09 at 14:50
  • 1
    @Chris -- The closing brace? :-) – Chris J Oct 13 '09 at 14:51
  • 24
    Oh noes, I've just realised what Chris must have meant! Because of the way SO formats code, my program *doesn't have a terminating newline*! This isn't even an invalid program which the compiler must diagnose, it's undefined behaviour. Horror, alarm, and general panic! I must fix it at once. `;-p` – Steve Jessop Oct 13 '09 at 15:12
22

The code you wrote doesn't actually make the preprocessor do any calculation. A #define does simple text replacement, so with this defined:

#define PI 3.1416
#define OP PI/100

This code:

if (OP == x) { ... }

becomes

if (3.1416/100 == x) { ... }

and then it gets compiled. The compiler in turn may choose to take such an expression and calculate it at compile time and produce a code equivalent to this:

if (0.031416 == x) { ... }

But this is the compiler, not the preprocessor.

To answer your question, yes, the preprocessor CAN do some arithmetic. This can be seen when you write something like this:

#if (3.141/100 == 20)
   printf("yo");
#elif (3+3 == 6)
   printf("hey");
#endif
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
shoosh
  • 76,898
  • 55
  • 205
  • 325
  • 7
    Sadly, if you try your conditional compilation snippet, you get the message from GCC: `x.c:5:6: error: floating constant in preprocessor expression`. You can do integer arithmetic with the C pre-processor; you cannot do floating point arithmetic with it. – Jonathan Leffler Oct 13 '09 at 14:00
  • 4
    looks like gcc cut corners here, Stroustrup C++ 2nd edition r.16.5 `#if constant-expression`, `r.5.19 constant expression`: "... floating constants must be cast to integral types". – denis Feb 23 '13 at 17:23
9

Yes, it can be done with the Boost Preprocessor. And it is compatible with pure C, so you can use it in C programs with C-only compilations. Your code involves floating-point numbers though, so I think that needs to be done indirectly.

#include <boost/preprocessor/arithmetic/div.hpp>
BOOST_PP_DIV(11, 5) // expands to 2
#define KB 1024
#define HKB BOOST_PP_DIV(A,2)
#define REM(A,B) BOOST_PP_SUB(A, BOOST_PP_MUL(B, BOOST_PP_DIV(A,B)))
#define RKB REM(KB,2)

int div = HKB;
int rem = RKB;

This preprocesses to (check with gcc -S):

int div = 512;
int rem = 0;

Thanks to this question.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
6

Yes.

I can't believe that no one has yet linked to a certain obfuscated C contest winner. The guy implemented an ALU in the preprocessor via recursive includes. Here is the implementation, and here is something of an explanation.

Now, that said, you don't want to do what that guy did. It's fun and all, but look at the compile times in his hint file (not to mention the fact that the resulting code is unmaintainable). More commonly, people use the pre-processor strictly for text replacement, and evaluation of constant integer arithmetic happens either at compile time or run time.

As others noted however, you can do some arithmetic in #if statements.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
  • 2
    http://stackoverflow.com/questions/652788/what-is-the-worst-real-world-macros-pre-processor-abuse-youve-ever-come-across/1242177#1242177 - chaos-pp. Preprocessor abuse with style. – Andrew Y Oct 15 '09 at 16:50
6

Be careful when doing arithmetic: add parentheses.

#define SIZE4 4
#define SIZE8 8
#define TOTALSIZE SIZE4 + SIZE8

If you ever use something like:

unsigned int i = TOTALSIZE/4;

And expect i to be 3. You would get 4 + 2 = 6 instead.

Add parentheses:

#define TOTALSIZE (SIZE4 + SIZE8)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Phil
  • 390
  • 5
  • 5