Skip to content

Instantly share code, notes, and snippets.

@opsJson
Last active December 26, 2021 00:46
Show Gist options
  • Save opsJson/83d08d29e4eec311ff929c5f04285e19 to your computer and use it in GitHub Desktop.
Save opsJson/83d08d29e4eec311ff929c5f04285e19 to your computer and use it in GitHub Desktop.
Easily gets the type of a variable.
#include <stdio.h>
#include <limits.h>
#define typeof(x) \
(((x == 0) ? (x = 0.00000000001) : (x)) / INT_MAX == 0) \
? ("0cs0i0000"[sizeof(x)]) \
: ("0000f000d"[sizeof(x)])
int main() {
char c;
int i;
float f;
double d;
short int s;
printf("char: %c\n", typeof(c));
printf("int: %c\n", typeof(i));
printf("float: %c\n", typeof(f));
printf("double: %c\n", typeof(d));
printf("short int: %c\n", typeof(s));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment