Home > Enterprise >  What is the '_u' data type in this #define declaration?
What is the '_u' data type in this #define declaration?

Time:11-25

Hope I'm phrasing this question correctly... I have begun working through some coding examples for a micrcontroller device, and I see many expressions similar to the following:

#define REG_CONFIG _u(0xF5)

I think this preprocessor directive declares that REG_CONFIG is a constant of type unsigned int and value 0xF5. What I don't understand is the use of _u(value); _u is apparently a data type but AFAIK, it's not a standard type (like unsigned int). I'm also under the impression that data types are declared as a suffix instead of a prefix.

Could someone explain this to an inexperienced programmer?

CodePudding user response:

It seems to be a macro defined to conditionally add a u suffix to the literal to give it unsigned type. The macro seems to be used so that headers containing macros with such literals can be reused for assembler sources which don't support the u suffix.

See a github search here resulting in the following snippet from one of multiple possible platform_defs.h headers:

#ifndef _u
#ifdef __ASSEMBLER__
#define _u(x) x
#else
#define _u(x) x ## u
#endif
#endif

CodePudding user response:

According to this github PR comment: https://github.com/raspberrypi/pico-examples/pull/127#discussion_r660001009

Yeah, we use _u(3) or whatever in the SDK in case the constant is included by assembly, which doesn't understand 3u - i.e. we define _u to either suffix u or not based on where we are.

  • Related