Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does everybody typedef over standard C types?

Tags:

c++

c

stdint

If you want to use Qt, you have to embrace quint8, quint16 and so forth.

If you want to use GLib, you have to welcome guint8, guint16 and so forth.

On Linux there are u32, s16 and so forth.

uC/OS defines SINT32, UINT16 and so forth.

And if you have to use some combination of those things, you better be prepared for trouble. Because on your machine u32 will be typedefd over long and quint32 will be typedefd over int and the compiler will complain.

Why does everybody do this, if there is <stdint.h>? Is this some kind of tradition for libraries?

like image 204
Amomum Avatar asked Jul 24 '16 12:07

Amomum


People also ask

Why do we use typedef in C?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

Why typedef better than definition?

typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc. typedef interpretation is performed by the compiler where #define statements are performed by preprocessor.

Should I use typedef in C?

typedef'ing structs is one of the greatest abuses of C, and has no place in well-written code. typedef is useful for de-obfuscating convoluted function pointer types and really serves no other useful purpose.

Does typedef make code run faster?

No difference in execution time, so you should be fine using either for competitive programming. I personally avoid #define and use typedef for types as it is more cleaner and readable.


1 Answers

stdint.h didn't exist back when these libraries were being developed. So each library made its own typedefs.

like image 173
Edward Karak Avatar answered Oct 08 '22 11:10

Edward Karak