Skip to content

Instantly share code, notes, and snippets.

@cota

cota/foobar.c Secret

Created October 14, 2014 02:14
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 cota/9ff275d4dddde302cade to your computer and use it in GitHub Desktop.
Save cota/9ff275d4dddde302cade to your computer and use it in GitHub Desktop.
/*
* foobar.c
* Invoke with:
* gcc -o foobar -Wall -Wextra -Wno-unused-parameter foobar.c && ./foobar
*/
#include <stdio.h>
#if 1
#define caa_cast_long_keep_sign(v) \
((unsigned long) (v))
#else
#define caa_is_signed_type(type) ((type) -1 < (type) 0)
#define caa_cast_long_keep_sign(v) \
(caa_is_signed_type(__typeof__(v)) ? \
(unsigned long) (long) (v) : \
(unsigned long) (v))
#endif
#define xstr(s) str(s)
#define str(s) #s
#define pr(var) \
printf(#var":\t0x%lx %ld\n", caa_cast_long_keep_sign(var), caa_cast_long_keep_sign(var))
#define pru(var) \
printf(#var":\t0x%lx %lu\n", caa_cast_long_keep_sign(var), caa_cast_long_keep_sign(var))
int main(int argc, char *argv[])
{
char c = -128;
unsigned char uc = ~0;
long l = __LONG_MAX__;
unsigned long ul = ~0;
void *p = NULL;
void *pp = (void *)0xdeadface;
pr(c);
pru(uc);
pr(l);
pru(ul);
pru(p);
pru(pp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment