Re: [CODING] Int Types

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 08/09/02


On Thu, 8 Aug 2002, Tiago wrote:

> But when I'll print the var to the char, I'm getting a problem with the
> sprintf function, it don't accept that variable type whith the normal
> representations (%d,%ld,%x,%X,...). Anyone knows how to solve that ?

Since you didn't tell us what error you're actually getting, I'm guessing
you're simply not using the right length modifier for int8_t and int16_t,
which are not of type 'int'.  An example should serve to illustrate:

  #include <stdint.h>
  #include <stdio.h>

  int main (void)
  {
      uint8_t c = 100;
      uint16_t h = 16000;
      uint32_t i = 3235839725u;
      printf("Dec: %hhd %hd %d\n", c, h, i); /* Signed */
      printf("Dec: %hhu %hu %u\n", c, h, i); /* Unsigned */
      printf("Hex: %hhx %hx %X\n", c, h, i); /* Unsigned hex */
      return 0;
  }

As you can see, you need to add a modifier in front of the conversion
character (d/u/x).  The modifier is typically one of "hh" for short short,
"h" for short, "l" for long, or "ll" for long long, although others exist
(see the printf man pages).  The above code compiles cleanly on (at least)
gcc 3.0.4 and produces:

  Dec: 100 16000 -1059127671
  Dec: 100 16000 3235839725
  Hex: 64 3e80 C0DEFEED

All of these are standard C99 types that are typedef'd (to char, short
int, int, long int, etc.) in <stdint.h> on supporting compilers.  A look
there will reveal a long list of integer types for a variety of purposes.

In the event that none of the above helps you, I would recommend taking
your query to a C newsgroup or mailing list -- a number exist for help and
discussion on questions like yours.

-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT