Home > OS >  What is portable and correct way to print int_fast_32_t etc data-type
What is portable and correct way to print int_fast_32_t etc data-type

Time:09-29

I have been reading this and have a doubt what is correct and portable way to print this datatype?

uint_fast8_t
uint_fast16_t
uint_fast32_t
uint_fast64_t    

Signed as well.

CodePudding user response:

include <inttypes.h> and use the macros therein

#include <inttypes.h> // <inttypes.h> also includes <stdint.h> on its own
uint_fast8_t a = 0;
uint_fast16_t b = 0
uint_fast32_t c = 0;
uint_fast64_t d = 0;

printf("%" PRIuFAST8 " %" PRIuFAST16 " %" PRIuFAST32 " %" PRIuFAST64 "\n", a, b, c, d);

CodePudding user response:

Based on @pmg answer, just for easy cut/paste / testing/reference.

#include <inttypes.h> // <inttypes.h> also includes <stdint.h> on its own
#include <stdio.h>

int main (void){

  uint_fast8_t  a = 100;
  uint_fast16_t b = 1001;
  uint_fast32_t c = 10001;
  uint_fast64_t d = 100001;

  int_fast8_t   ai = -100;
  int_fast16_t  bi = -1001;
  int_fast32_t  ci = -10001;
  int_fast64_t  di = -100001;

  uint_least8_t at = 200;
  int_least32_t ct = -200;
  

  printf ("sizeof: a:%zu b:%zu c:%zu d:%zu\n",
      sizeof (a), sizeof (b), sizeof (c), sizeof (d));
  
  printf ("sizeof: ai:%zu bi:%zu ci:%zu di:%zu\n",
      sizeof (ai), sizeof (bi), sizeof (ci), sizeof (di));
  
  printf ("sizeof (at):%zu sizeof (ct):%zu\n", sizeof (at), sizeof(ct));
  
  printf ("ufast8:%"  PRIuFAST8  " fast8:%"  PRIiFAST8  "\n", a, ai);
  printf ("ufast16:%" PRIuFAST16 " fast16:%" PRIiFAST16 "\n", b, bi);
  printf ("ufast32:%" PRIuFAST32 " fast32:%" PRIiFAST32 "\n", c, ci);
  printf ("ufast64:%" PRIuFAST32 " fast32:%" PRIiFAST64 "\n", d, di);
  
  printf ("unitleast8:%" PRIiLEAST8 " intleast32:%" PRIiLEAST32 "\n",
      at, ct);
  
}
  •  Tags:  
  • c
  • Related