Skip to content

Instantly share code, notes, and snippets.

@siiky
Last active April 8, 2017 21:11
Show Gist options
  • Save siiky/88915e123ee2db4d49701e48657a0a34 to your computer and use it in GitHub Desktop.
Save siiky/88915e123ee2db4d49701e48657a0a34 to your computer and use it in GitHub Desktop.
C types size and values
#!/usr/bin/env sh
# How to use:
# 1. `sh sizeof.sh`
# 2. ????
# 3. PROFIT!!!
cat << EOF | cc -Wall -Wextra -pedantic -O3 -static -o sizeof -x c - &&
#include <stdio.h>
#define issigned(type) ((type)(0-1)>0)
#define umaxof(t) (((0x1ULL<<((sizeof(t)*8ULL)-1ULL))-1ULL)|(0xFULL<<((sizeof(t)*8ULL)-4ULL)))
#define smaxof(t) (((0x1ULL<<((sizeof(t)*8ULL)-1ULL))-1ULL)|(0x7ULL<<((sizeof(t)*8ULL)-4ULL)))
#define maxof(t) ((unsigned long long)(issigned(t)?umaxof(t):smaxof(t)))
int main(void){printf(
"+-------------------------------------------------------+\n"
"| Type | Size (B) | Max Value |\n"
"+---------------------+----------+----------------------+\n"
"| unsigned char | %2lu | %20llu |\n"
"| unsigned short | %2lu | %20llu |\n"
"| unsigned int | %2lu | %20llu |\n"
"| unsigned long | %2lu | %20llu |\n"
"| unsigned long long | %2lu | %20llu |\n"
"| char | %2lu | %20llu |\n"
"| short | %2lu | %20llu |\n"
"| int | %2lu | %20llu |\n"
"| long | %2lu | %20llu |\n"
"| long long | %2lu | %20llu |\n"
"+---------------------+----------+----------------------+\n",
sizeof(unsigned char),maxof(unsigned char),sizeof(unsigned short),maxof(unsigned short),sizeof(unsigned int),maxof(unsigned int),sizeof(unsigned long),maxof(unsigned long),sizeof(unsigned long long),maxof(unsigned long long),sizeof(char),maxof(char),sizeof(short),maxof(short),sizeof(int),maxof(int),sizeof(long),maxof(long),sizeof(long long),maxof(long long));return 0;}
EOF
strip -s sizeof && ./sizeof && rm sizeof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment