Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Created December 11, 2023 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlabbe/074109231031839b21f0c7ae80749e36 to your computer and use it in GitHub Desktop.
Save mlabbe/074109231031839b21f0c7ae80749e36 to your computer and use it in GitHub Desktop.
C Print sizeof all types
#include <stdio.h>
#include <stdint.h> // uintptr_t
#include <stddef.h> // size_t
// enable printing sizes as ints to avoid faliing on compilers that don't support %zu
#define SIZEOF(expr) (int)sizeof(expr)
#define PRINT_SIZEOF(expr) printf("sizeof(%-11s) = %1d\n", #expr, SIZEOF(expr))
int main(void) {
PRINT_SIZEOF(char);
PRINT_SIZEOF(short);
PRINT_SIZEOF(int);
PRINT_SIZEOF(long);
PRINT_SIZEOF(long long);
PRINT_SIZEOF(float);
PRINT_SIZEOF(double);
PRINT_SIZEOF(long double);
PRINT_SIZEOF(uintptr_t);
PRINT_SIZEOF(size_t);
return 0;
}
@mlabbe
Copy link
Author

mlabbe commented Dec 11, 2023

Results from my systems:

_windows 64-bit vc++_

sizeof(char       ) = 1
sizeof(short      ) = 2
sizeof(int        ) = 4
sizeof(long       ) = 4
sizeof(long long  ) = 8
sizeof(float      ) = 4
sizeof(double     ) = 8
sizeof(long double) = 8
sizeof(uintptr_t  ) = 8
sizeof(size_t     ) = 8

_windows 32-bit vc++_

sizeof(char       ) = 1
sizeof(short      ) = 2
sizeof(int        ) = 4
sizeof(long       ) = 4
sizeof(long long  ) = 8
sizeof(float      ) = 4
sizeof(double     ) = 8
sizeof(long double) = 8
sizeof(uintptr_t  ) = 4
sizeof(size_t     ) = 4

_emscripten32_

sizeof(char       ) = 1
sizeof(short      ) = 2
sizeof(int        ) = 4
sizeof(long       ) = 4
sizeof(long long  ) = 8
sizeof(float      ) = 4
sizeof(double     ) = 8
sizeof(long double) = 16
sizeof(uintptr_t  ) = 4
sizeof(size_t     ) = 4

_macos arm64_

sizeof(char       ) = 1
sizeof(short      ) = 2
sizeof(int        ) = 4
sizeof(long       ) = 8
sizeof(long long  ) = 8
sizeof(float      ) = 4
sizeof(double     ) = 8
sizeof(long double) = 8
sizeof(uintptr_t  ) = 8
sizeof(size_t     ) = 8

_linux x64_

sizeof(char       ) = 1
sizeof(short      ) = 2
sizeof(int        ) = 4
sizeof(long       ) = 8
sizeof(long long  ) = 8
sizeof(float      ) = 4
sizeof(double     ) = 8
sizeof(long double) = 16
sizeof(uintptr_t  ) = 8
sizeof(size_t     ) = 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment