2

code:

int8_t ret;
int8_t buf[8];
bytes_written = snprintf(buf, 8, "%" PRId8, 2);

warning:

warning: pointer targets in passing argument 1 of ‘snprintf’ differ in signedness [-Wpointer-sign]
     bytes_written = snprintf(buf, 8, "%" PRId8, 2);
     ^
/usr/include/stdio.h:386:12: note: expected ‘char * __restrict__’ but argument is of type ‘int8_t *’
 extern int snprintf (char *__restrict __s, size_t __maxlen,

I know this can be fixed by taking buf as *char, but

int8_t is typedef as unsigned char

checked with pre-processor output i.e. gcc main.c | grep int8_t

So why compiler is not able to understand that?

With buf as uint8_t also I get the same warning.

Edit:

int8_t is typedef as signed char (By mistake I have written as unsigned in original post)

Patrick
  • 2,464
  • 3
  • 30
  • 47
  • What is `PRId8`. Should the call to `snprintf` not be `bytes_written = snprintf(buf, 8, "%PRId8", 2);` ? – Nishant Jun 01 '16 at 06:24
  • have a look at this http://stackoverflow.com/questions/17744226/int8-t-vs-char-which-is-the-best-one – piyushj Jun 01 '16 at 06:24
  • 3
    @Nishant No, `PRId8` is a CPP macro. – a3f Jun 01 '16 at 06:29
  • @Lundin I use CPP to mean C Preprocessor. – a3f Jun 01 '16 at 06:30
  • 1
    @a3f Aah :) No, CPP is an abbreviation for C++. – Lundin Jun 01 '16 at 06:31
  • 1
    @Nishant No that part it is correct. Take a look at [this SO post](http://stackoverflow.com/a/6299346/3436922) – LPs Jun 01 '16 at 06:34
  • @Lundin "cpp" *is* the name of the C pre-processor, at least as it comes with GCC. – alk Jun 01 '16 at 06:42
  • 1
    @alk So the G++ pre-processor is called CPPPP? – Lundin Jun 01 '16 at 06:45
  • I'd say G++ and GCC use the same CPP. ;-) @Lundin – alk Jun 01 '16 at 06:46
  • Nice one, Lundin. By way of interest there are only three occurrences of "cpp" is the ISO C11 standard and none of them refer to the preprocessor. In a *stunningly apt* use of the English language, ISO insists on calling this the ... wait for it ... preprocessor :-) – paxdiablo Jun 01 '16 at 06:48

1 Answers1

4

The sprintf family of calls require a char * buffer in which to write their data.

You currently have it as int8_t * (a signed 8-bit value) and the fact that it's complaining about the signedness of the type almost certainly means the naked char is unsigned on your system (the standard leaves it open as to whether or not char is a signed or unsigned type) or that gcc is clever enough to realise this may be a portability issue on other compilers.

Be aware that's a warning so it will probably still work. However, I like to clean up code like that so as to make my life easier down the track.

The fix is, of course, to use the correct type (either by changing the type to char buf[8] or by explicitly casting it in the snprintf call, thereby telling the compiler you know what you're doing).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • `char` is a distinct type from `signed char` and `unsigned char` regardless. – o11c Jun 01 '16 at 06:34
  • @paxdiablo It gave same warning with *buf* as uint8_t also. I am compiling code with gcc and that consider char as signed char by default – Patrick Jun 01 '16 at 06:49