Last active
January 23, 2019 02:16
-
-
Save vu3rdd/ff0ddb881d9a23ce2d8b5f0ab709a952 to your computer and use it in GitHub Desktop.
A simple C debug macro (needs a version of gcc compiler which supports the C11 standard)
This file contains hidden or 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
/* Copyright 2019 Ramakrishnan Muthukrishnan | |
* | |
* To the extent possible under law, the author(s) have dedicated | |
* all copyright and related and neighboring rights to this software | |
* to the public domain worldwide. This software is distributed without | |
* any warranty. | |
* | |
* You should have received a copy of the CC0 Public Domain Dedication | |
* along with this software. | |
* If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. | |
*/ | |
#ifndef DBG_H_INCLUDED | |
#define DBG_H_INCLUDED 1 | |
#if defined (__GNUC__) | |
#if defined (__STDC_VERSION__) | |
#if __STDC_VERSION__ >= 201112L | |
#include <stdio.h> | |
#define PRINTF_FMT_STR(x) _Generic((x), \ | |
char: "%c", \ | |
signed char: "%hhd", \ | |
unsigned char: "%hhu", \ | |
signed short: "%hd", \ | |
unsigned short: "%hu", \ | |
signed int: "%d", \ | |
unsigned int: "%u", \ | |
long int: "%ld", \ | |
unsigned long int: "%lu", \ | |
long long int: "%lld", \ | |
unsigned long long int: "%llu", \ | |
float: "%f", \ | |
double: "%f", \ | |
long double: "%Lf", \ | |
char *: "%s", \ | |
void *: "%p", \ | |
bool: "%s" \ | |
) | |
#define DBG(x) \ | |
({ typeof (x) x_; \ | |
x_ = (x); \ | |
fprintf(stderr, "[%s:%d] %s = " , __FILE__, __LINE__, #x); \ | |
fprintf(stderr, PRINTF_FMT_STR(x), x_); \ | |
fprintf(stderr, "\n"); \ | |
x_; \ | |
}) | |
#endif /* __STDC_VERSION >= XXXX */ | |
#endif /* __STDC_VERSION__ */ | |
#endif /* __GNUC__ */ | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the version of factorial program example given in the Rust blog post:
And the output:
Here is the equivalent C version:
and the corresponding output:
Thanks to Sajith for the inspiration and Olve Maudal for many suggestions.