Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active August 16, 2023 01:25
Show Gist options
  • Save scivision/85eb64a6f0fcb0b3bcbb965229486428 to your computer and use it in GitHub Desktop.
Save scivision/85eb64a6f0fcb0b3bcbb965229486428 to your computer and use it in GitHub Desktop.
Use ssize_t on POSIX systems and with MSVC compilers
#include <stdint.h> /* C23: SIZE_WIDTH, PTRDIFF_WIDTH */
#include <stddef.h>
#include <stdio.h>
#define ssize_t ptrdiff_t
int main(void){
size_t s;
ssize_t t;
#ifdef SIZE_WIDTH
printf("SIZE_WIDTH: %d PTRDIFF_WIDTH %d\n", SIZE_WIDTH, PTRDIFF_WIDTH);
#endif
printf("sizeof(size_t): %zu sizeof(ssize_t): %zu\n", sizeof(s), sizeof(t));
return 0;
}
#include <cstdint> // C23: SIZE_WIDTH, PTRDIFF_WIDTH
#include <cstddef> // std::size_t
#include <bit> // std::bit_width
#include <iostream>
#define ssize_t std::ptrdiff_t
int main()
{
size_t s;
ssize_t t;
#ifdef SIZE_WIDTH
std::cout << "SIZE_WIDTH: " << SIZE_WIDTH << " PTRDIFF_WIDTH: " << PTRDIFF_WIDTH << std::endl;
#endif
#ifdef __cpp_lib_int_pow2
std::cout << "std::bit_width(size_t): " << std::bit_width(SIZE_WIDTH) << " std::bit_width(ssize_t): " << std::bit_width(PTRDIFF_WIDTH) << std::endl;
#endif
std::cout <<"sizeof(size_t): " << sizeof(s) << " sizeof(ssize_t): " << sizeof(t) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment