How to use typedef to rename types?

Hi!

In my code, I created a header file that renames some of the default types to abbreviated names. (i.e. uint8_t becomes u8) I used typedef for the declarations and it failed compiling (conflicting declaration 'typedef uint8_t u8'). I've thought of using #define to rename the variables but I hear it's bad practice. What should I do?

Here's the code (from the header file):

#define GET_BIT(x, n) (((x) >> (n)) & 1)

typedef uint8_t   u8;   typedef int8_t  s8;
typedef uint16_t  u16;  typedef int16_t s16;
typedef uint32_t  u32;  typedef int32_t s32;
typedef uint64_t  u64;  typedef int64_t s64;

Thanks in advance!

Works on my system:

typedef uint8_t u8;

u8 abyte;

void setup()
{
  abyte = 0;
}

void loop()
{
  Serial.println(abyte++);
  delay(1000);
}

You may have a conflict with a library that defines a type named "u8".

EDIT: I get a conflict with "u16" since this is declared in "USBAPI.h" as "typedef unsigned short u16". Enable "verbose ouput" and "show all warnings" for the compiler and you will know more about the conflicts you encounter.

Interesting idea.

As an aside, what does the _t in uint8_t mean? Why is it not just uint8?

_t is short for type

PerryBebbington:
Interesting idea.

As an aside, what does the _t in uint8_t mean? Why is it not just uint8?

uint8_t = unsigned integer 8bit _type. I guess the _t (short for type) was added to reduce the chance of conflicts with existing types.

Thank you both :slight_smile:
++Karma;
(My nephew has a 1st class degree in extreme computer cleverness and he didn't seem to know :confused: )

PerryBebbington:
As an aside, what does the _t in uint8_t mean? Why is it not just uint8?

The _t usually wraps an opaque type definition.

POSIX Standard (1003.1) states

B.2.12 Data Types

Defined Types

The requirement that additional types defined in this section end in “_t” was prompted by the problem of name space pollution. It is difficult to define a type (where that type is not one defined by POSIX.1-2017) in one header file and use it in another without adding symbols to the name space of the program. To allow implementors to provide their own types, all conforming applications are required to avoid symbols ending in “_t”, which permits the implementor to provide additional types. Because a major use of types is in the definition of structure members, which can (and in many cases must) be added to the structures defined in POSIX.1-2017, the need for additional types is compelling.

The types, such as ushort and ulong, which are in common usage, are not defined in POSIX.1-2017 (although ushort_t would be permitted as an extension). They can be added to <sys/types.h> using a feature test macro (see POSIX.1 Symbols). A suggested symbol for these is _SYSIII. Similarly, the types like u_short would probably be best controlled by _BSD.

Basically the Standard says that since there are good chances of extending the Standard types’ list, the Standard restricts the _t namespace for its own use…

So you are not supposed to do that yourself to avoid conflicts with future versions of Standard C and POSIX, even though many of us do for adding convenience

Thank you JML, another sprinkling of Karma....

Lol thx :slight_smile:
+1 for your generosity :wink:

epicface2304:
In my code, I created a header file that renames some of the default types to abbreviated names.

Bad idea.

It just makes your code unreadable by everyone else. However much you dislike the standard system the reason people use it is because it is standard.

…R

Turns out that u16 was already defined in USBAPI.h, like Danois90 said. I removed it and my code compiled, without a problem.