22

Maximum and minimum values for integers

  1. #include <stdio.h>
  2.  
  3. #define NBITS 8
  4.  
  5. #define UMAX(type) ((1U << sizeof (type) * NBITS) -1)
  6. #define SMAX(type) ((1U << sizeof (type) * NBITS - 1) -1)
  7. #define SMIN(type) (~SMAX(type))
  8.  
  9. main() {
  10.     printf("\t  unsigned char max: %u\n", UMAX(char));
  11.     printf("\t    signed char max: %u\n", SMAX(char));
  12.     printf("\t    signed char min: %d\n", SMIN(char));
  13.     printf("\t   unsigned int max: %u\n", UMAX(int));
  14.     printf("\t     signed int max: %u\n", SMAX(int));
  15.     printf("\t     signed int min: %d\n", SMIN(int));
  16.     printf("\t unsigned short max: %u\n", UMAX(short));
  17.     printf("\t   signed short max: %u\n", SMAX(short));
  18.     printf("\t   signed short min: %d\n", SMIN(short));
  19.     printf("\t  unsigned long max: %u\n", UMAX(long));
  20.     printf("\t    signed long max: %u\n", SMAX(long));
  21.     printf("\t    signed long min: %d\n", SMIN(long));
  22. }
$ 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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].