22
Maximum and minimum values for integers
- #include <stdio.h>
- #define NBITS 8
- #define UMAX(type) ((1U << sizeof (type) * NBITS) -1)
- #define SMAX(type) ((1U << sizeof (type) * NBITS - 1) -1)
- #define SMIN(type) (~SMAX(type))
- main() {
- printf("\t unsigned char max: %u\n", UMAX(char));
- printf("\t signed char max: %u\n", SMAX(char));
- printf("\t signed char min: %d\n", SMIN(char));
- printf("\t unsigned int max: %u\n", UMAX(int));
- printf("\t signed int max: %u\n", SMAX(int));
- printf("\t signed int min: %d\n", SMIN(int));
- printf("\t unsigned short max: %u\n", UMAX(short));
- printf("\t signed short max: %u\n", SMAX(short));
- printf("\t signed short min: %d\n", SMIN(short));
- printf("\t unsigned long max: %u\n", UMAX(long));
- printf("\t signed long max: %u\n", SMAX(long));
- printf("\t signed long min: %d\n", SMIN(long));
- }
$ gcc -o limits limits.c
limits.c: In function ‘main’:
limits.c:13: warning: left shift count >= width of type
limits.c:19: warning: left shift count >= width of type
$ ./limits
unsigned char max: 255
signed char max: 127
signed char min: -128
unsigned int max: 4294967295
signed int max: 2147483647
signed int min: -2147483648
unsigned short max: 65535
signed short max: 32767
signed short min: -32768
unsigned long max: 4294967295
signed long max: 2147483647
signed long min: -2147483648
Comments