Skip to content

Instantly share code, notes, and snippets.

@dreamer
Last active February 6, 2020 06:58
Show Gist options
  • Save dreamer/27190792c165e4f9d19510e82f8a4d02 to your computer and use it in GitHub Desktop.
Save dreamer/27190792c165e4f9d19510e82f8a4d02 to your computer and use it in GitHub Desktop.
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#include <cinttypes>
#include <cstdint>
#include <cstdio>
#include <cstring>
template<typename T>
bool test(int x, const char *fmt)
{
char buf_1[20] = {0};
snprintf(buf_1, sizeof(buf_1), "%d", x);
const T y = x;
char buf_2[20] = {0};
snprintf(buf_2, sizeof(buf_2), fmt, y);
const bool result = (strcmp(buf_1, buf_2) == 0);
if (result)
fprintf(stderr, "Ok (%s)\n", fmt);
else
fprintf(stderr, "Error (%s) \"%s\" != \"%s\"\n", fmt, buf_1, buf_2);
return result;
}
int main(int argc, char* argv[])
{
// https://en.cppreference.com/w/cpp/types/integer
test <uintptr_t> (1, "%" PRIuPTR);
test <uint_least64_t> (1, "%" PRIuLEAST64);
test <uint_fast64_t> (1, "%" PRIuFAST64);
test <uint64_t> (1, "%" PRIu64);
test <intptr_t> (1, "%" PRIdPTR);
test <int_least64_t> (1, "%" PRIdLEAST64);
test <int_fast64_t> (1, "%" PRIdFAST64);
test <int64_t> (1, "%" PRId64);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment