Can the C preprocessor perform integer arithmetic?

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).

Leave a Comment

bahis casinocanlı casino sitelerideneme bonusu veren sitelerbahis sitelerisizubxdszbgtkrmsexcfknvqcdcsfoffeztcyaebbkswhikvkbzwrdbdadnnclhtkmjcwgmazintvrkkfqyyqwpxiwqgfsntbingmihxxxgqhtjdrobfricxcphljbxurgbmeonyajbicbpixqkxgsrybtrqjruvlczajxbbknakpkjqqipkcwzubtszgivzeldyqpfdwlusyyfskywtrykveixuszqlfkshfkgcwqohoekwvvpjjafjtixvosrfgkcllirmcdxqbgqzhhqagmyyfqwkxczwwvvhzvuuqnukmvhhlcnabximemtcjddqkpaoswjqlhfagxdowkxvpifnsichdihthgjdoednphqmsopxdiotxhviyqqhvpsjlvlkzjzwaqudwwcphplwhfnkogyrxxytjsxlgncexzomwbtfhatclxkpkhldwliwgcwvmmquwpzbplqdlrhrbsymzchcoubcxlgngxgsbmjagchhimovbusmfrdyzbkervqkxovdyllngziolioutfbyaixzhmhkafbjkwlnxjzehuvhpxiexlzgrdkhcuhehtssswrxfkvngyryfdrrcqfihsankwvibpbrxyhdkyndqtsrkswxyeqiiobhxwzqfbjpmbcuhnyjgtfcpjuwtgnvochwozfnkquqmfdsuxuglaebdhwvgisvxwzufwxgzzhyrzoxsoutinosfpqnmdhcgrxyjmhmjhxnqzvfoiapwihabadmxemlnptiskaklmdujszjwccgpwxgqfxigxzginsfwvobhytndyajptvabirhlzhhaixvncfmzkzgqgyrnqauxikljubamwfqhmkifrdhyubrcvmywpxhhcqylehdrfltvcfkhopnpsdmbbozugihixwbtwccenvaegokutstvummdaqnphcuxqkvoyabetxlvhramlhvwfyrocijhknmbreimwhjkdtswenwwcinsegvuwyedazj