logo

utils

~/.local/bin tools and git-hooks

sizeof.c (1200B)


      1 #if __OpenBSD__
      2 #include <stdlib.h>
      3 #else
      4 #include <bsd/stdlib.h>
      5 #endif
      6 
      7 #include <limits.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 
     11 static const char *format   = "sizeof(%s) == %d bytes; %d bits\n";
     12 static const char *format_m = "sizeof(%s) == %d bytes; %d bits; (MIN:MAX) == (%d:%d)\n";
     13 
     14 static void print_size(size_t size, char *type)
     15 {
     16 	int c;
     17 	c = size;
     18 	printf(format, type, c, c * CHAR_BIT);
     19 }
     20 
     21 int main(void)
     22 {
     23 	int c;
     24 
     25 	printf("CHAR_BIT == %d\n", CHAR_BIT);
     26 
     27 	c = sizeof(int);
     28 	printf(format_m, "int", c, c * CHAR_BIT, INT_MIN, INT_MAX);
     29 	c = sizeof(char);
     30 	printf(format_m, "char", c, c * CHAR_BIT, CHAR_MIN, CHAR_MAX);
     31 
     32 	print_size(sizeof(uint8_t), "uint8_t");
     33 	print_size(sizeof(short), "short");
     34 	print_size(sizeof(double), "double");
     35 	print_size(sizeof(long), "long");
     36 	print_size(sizeof(double long), "double long");
     37 	print_size(sizeof(long long), "long long");
     38 	print_size(sizeof(long int), "long int");
     39 	print_size(sizeof(char[BUFSIZ]), "char[BUFSIZ]");
     40 	print_size(sizeof(char[256]), "char[256]");
     41 	print_size(sizeof(char[32]), "char[32]");
     42 	print_size(sizeof(char[2]), "char[2]");
     43 	print_size(sizeof('a'), "'a'");
     44 	print_size(sizeof(arc4random()), "arc4random()");
     45 
     46 	return 0;
     47 }