Test the size of integers in the preprocessor

This is an example C program which demonstrates how to test for the size of integers in the preprocessor. Using this you can ensure that your program does not get compiled when it shouldn't.

#include <stdio.h>
#include <limits.h>

/* The following will pass on most computers. */

#if UINT_MAX < 0XFFFFFFFFL
#error "Your computer does not have 32-bit integers"
#endif

/* The following one will fail on some computers. */

#if UINT_MAX < 0X1FFFFFFFFL
#error "Your computer does not have 33-bit integers"
#endif

int main ()
{
    return 0;
}

(download)

The output of attempting compilation looks like this:

tis.c:13:2: error: "Your computer does not have 33-bit integers"
#error "Your computer does not have 33-bit integers"
 ^
1 error generated.

Incidentally, here is the makefile used to generate the example output:

C=tis.c

all: tis.txt

tis.txt: $(C)
        -$(CC) $(C) >$@ 2>&1

clean:
        -rm -f tis tis.txt

(download)


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer