-
-
Save cota/9ff275d4dddde302cade to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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