18

Valeurs maximum et minimum des entiers

  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

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[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]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].