Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if uint8_t exists as a type, instead of unsigned char?

Tags:

c++

c

embedded

I have two compilers, one that recognizes uint8_t(GCC ARM-EABI), and one that doesn't(Renesas M16 Standard Toolchain).

The Renesas Toolchain is NOT ANSI C compliant, so you can throw out . So uint8_t, uint16_t,... aren't defined as existing types.

In order to maintain portability, I would like to have the same types(preferably uint8_t, due to the ambiguity of int).

Also my platforms are different size processors(ARM is 32 bit, and Renesas is 16 bit). Causing int to be different values.

Is there a way to check if uint8_t exists as a type?

And if not, declare it(and others uint16_t, uint32_t,...) as a type?

like image 725
Ashitakalax Avatar asked Nov 28 '22 05:11

Ashitakalax


2 Answers

Is there a way to check if uint8_t exists as a type?

Use:

#include <stdint.h>

#ifdef UINT8_MAX
...
#endif
like image 147
ouah Avatar answered Dec 05 '22 09:12

ouah


uint8_t is not a built-in type, it is defined in stdint.h. So it is not a matter of the compiler "recognising" uint8_t, but rather simply a case of making stdint.h available.

If your toolchain does not provide stdint.h, you can easily provide your own implementation, using the compiler documentation to determine the built-in types that correspond to the specific sizes. On the toolchain without stdint.h you simply provide your own to the project, on the toolchain with stdint.h you don't. That way the code (other than stdint.h itself) will be identical across platforms - you don't need to conditionally define uint8_t.

One problem you may come across (on some TI DSP's for example) is that memory may not be 8 bit addressable and a char will be 16 bit (or larger). In that case uint8_t or indeed any 8 bit integer type will not be supported at all. A char is always the smallest data type for the specific platform, but may be larger than 8 bit.

like image 21
Clifford Avatar answered Dec 05 '22 10:12

Clifford