How to printf a sint32 in C using gcc compiler tricore v3.4.6?

1.8k views Asked by At

I am working with the tricore v3.4.6 compiler.

Suppose I have a signed integer like sint32 a = -1 and want to print that with printf.

I tried printf("Signed number %i", a) as well as printf("Signed number %d", a) which both gives me compiler warnings, for example

warning: int format, sint32 arg

2

There are 2 answers

1
DomTomCat On BEST ANSWER

unlike suggested in the comments to the question, sint32 is not uncommon in safety critical and embedded systems and usually falls back to a typecast for int. (e.g. in some MISRA environments).

Hence

sint32 a = -1;
printf("%d", a); 

should do the trick anyways. tested with gcc v5.2.1 and arm-gcc v5.2.1 (-Wall and no warnings).

If it still gives you a warning try to figure out what sint32 really maps to and try long-print: printf("%ld"). However, then double check if the byte length of sint32 really is 32bits? (and some systems may even have less than a 32bit architecture)

0
chux - Reinstate Monica On

To printf() any signed integer that lacks a defined matching prefix like "l", "ll", "h", etc. , simply cast to the widest known type.

#include <stdint.h>
sint32 a = -1;
printf("Signed number %jd", (intmax_t) a);

// or lacking intmax_t
sint32 a = -1;
printf("Signed number %lld", (long long) a);