CSE/ENGR 142 Getting 4-Byte Integers at Home

If you are using Think C on a Macintosh, see the online tip on Think C compiler options. Four-byte ints is NOT the "factory setting" that came with your copy of ThinkC, so you will have to change it. To be sure that you do not suffer integer overflows with integers much greater than 64000, you should check that your compiler settings include "4-byte integers". This means that integer memory locations are 4 bytes long rather than only 2. That is, you get twice as many digits.

The simplest way to check this is to put the statements

    printf("%d\n", sizeof(int));
    printf("%d\n", sizeof(long int));
in a program. This will print the number of bytes used to store two sorts of integers. If the first statement prints 4, you are all set. If it prints 2 and the second statement prints 4, then the simplest solution is to use the type long int in all of those declarations where you would ordinarily use int and you are concerned that the values may exceed, say, 16000. In addition, you will use the conversion character "%ld" where you would ordinarily use "%d" in printf and scanf statements involving those variables. Finally, to designate a constant as long, use the suffix L, for example, 1000000L. Note that you need to do this with literal constants and #define constants that are to be printed with a %ld placeholder, or they won't print correctly. E.g.
    Wrong                           Right
    -----                           -----
    #define TWO 2                   #define TWO 2L
    printf("%ld%ld", TWO, 3);       printf("%ld%ld", TWO, 3L);   

You can also try to change the compiler settings to always use 4-byte integers as the default. (You may have to do this if both statements print 2.) If you are using a PC, see the user's manual that came with your C compiler.