0

I was experimenting with some c programming and encountered an error in which gcc issues the warning:

 warning: unknown conversion type character 'h' in format [-Wformat=]
   21 |     printf("This bytes value is %hhu \n", *i);
      |                                   ^

with "i" being a char pointer I use to read some data byte by byte.

After some research, I learned that this feature has been included since C99.
I am currently using C17 (_STDC_VERSION_ 201710L) and GCC 12.2. Therefore this feature should be included.

I recently switched to Chocolatey for managing MinGW. Before I had an older version of C manually installed and used which I removed with the switch. Could this be an issue?

I don't see why the compiler struggles to interpret this besides maybe using the wrong stdio.h version, but I can't seem to fix this.

Here is the remaining part of this experiment in case that this could be relevant:

#include <stdio.h>


typedef struct{
char hight;
char width; 
char data[3];
} Byte_Buffer;

int main (int argc, char **argv)
{
  
  Byte_Buffer buffer; 

  printf ("size of buffer is %lu \n",(unsigned long) sizeof buffer);

  char* i =(char *) &buffer;
  
  for(char* end = (char*) &buffer + sizeof buffer; i <= end ; i++)
  {
    printf("This bytes (%p) value is %hhu \n", i,*i);
  }
  return 0; 
}

The code works fine using %hu or % u instead btw.

Thanks for the help :)

GlaDOS
  • 1
  • 1
  • This is a FAQ. Various older versions of Mingw use trash standard libs. The work-around is `#define __USE_MINGW_ANSI_STDIO 1` before `#include `. – Lundin Aug 17 '23 at 13:58
  • I linked a duplicate regarding `printf` of `size_t` but it's the very same issue as you have here. See [this](https://stackoverflow.com/a/52383920/584518) in particular. – Lundin Aug 17 '23 at 14:00
  • The easy solution is to get a newer version though. Your code compiles cleanly for me in Mingw. `--VERSION` gives `(MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 12.1.0`. You can download similar builds from their site here: https://winlibs.com/. (The official download from Sourceforge is toasted since many years back.) – Lundin Aug 17 '23 at 14:04
  • @GlaDOS, Aside: avoid issues with negative values. Use `unsigned char* end = (unsigned char*) &buffer + sizeof buffer` – chux - Reinstate Monica Aug 17 '23 at 15:13

0 Answers0