C sucks but it's not like there's anything more widely supported. Most of my complaints are syntactic stuff, a nitpick that void isn't unified with the rest of the type system, and undefined behavior though, the semantics are mostly fine. Zig is exciting since it's C with a new coat of paint and something functionally equivalent to dependent types but it will be some time before it's ready I think.
I could never remember if it was typedef foo int or typedef int foo until someone told me that you don’t actually have to put typedef at the beginning of the line. That made me realise it’s literally just a specifier like const or static, which also makes it obvious that you put the new name at the end. Forget east const, we can do int const unsigned long typedef long u64
Typedef is used for most types in embedded C, I've never seen an enum or a struct defined without a typedef attached (the most respectful codebase add a _t suffix so you know it's a type)
_t is a pseudo-reserved thing for future posix/libc extensions, but embedded gets away with it since they tend to not use either posix or a modern general purpose libc which is why you don't see more usage of it
i agree. it's not reserved in c++, but they reserve double leading underscores over there so there's also that.
personally i do scope_name_t with scope usually being something that would be closer to a namespace like rabbit_layer_config_t. it is extremely unlikely that they end up colliding like this and also helps organize things a little bit.
got it. yeah I pretty much agree. but you know, people use bad practices all the time and it sucks when you have to play in their backyard. so I get it.
writing win32 code is cool because you'll have a type that is typedefed to another typedef which is a macro that resolves to another typedef which is a pointer to another typedef, and it all just comes out to a void pointer in the end
some people still think that you can replace the entirety of C++ with macros (yes, that's me and i am willing to die on this hill. not wholeheartedly anymore because i finally have encountered cases where it can't reasonably be)
I like typedef for distinct ints, and I would fucking love distinct integer types, meaning that you need to explicitly convert them to another, or get warnings on wrong conversions.
Otherwise I actually prefer to use the explicit struct prefix you normally have in C, on enums I don't really like it, and I'm split on union, but you don't really pass around unions that often.
But as many things, it really depends on the style of program you write. Sometimes it's more worth it to be entirely specific when you use a primitive type, or a struct, and it's easier if you can just see it in every local declaration.
77
u/RedAndBlack1832 23h ago
Typedefs are fine, except when you obscure pointer types. I don't like that.
53
u/MissinqLink 1d ago
“Most” C devs
This fucking sucks actually 👉 C🛹
33
u/GoogleIsYourFrenemy 1d ago
Hey, in C++ we now have "using" which makes declaring function pointers types suck less.
16
u/CadmiumC4 18h ago
windows after inventing 500 different names for unsigned int and unsigned int*
13
u/Maximilian_Tyan 21h ago
Typedef is used for most types in embedded C, I've never seen an enum or a struct defined without a typedef attached (the most respectful codebase add a _t suffix so you know it's a type)
18
u/Koshin_S_Hegde 1d ago
could someone explain? i use typedef for every struct :3
5
u/nimrag_is_coming 16h ago
writing win32 code is cool because you'll have a type that is typedefed to another typedef which is a macro that resolves to another typedef which is a pointer to another typedef, and it all just comes out to a void pointer in the end
4
u/realrealvantheman 22h ago
Who says this bro typedef is great
3
u/nedovolnoe_sopenie 22h ago
some people still think that you can replace the entirety of C++ with macros (yes, that's me and i am willing to die on this hill. not wholeheartedly anymore because i finally have encountered cases where it can't reasonably be)
3
u/Vladislav20007 16h ago
"with great memory manipulation, comes great memory management." – K&R or IBM training manual or Ben Eater idrkoc
2
u/ilnarildarovuch 1d ago
Well, maybe
2
u/Wertbon1789 16h ago
I like typedef for distinct ints, and I would fucking love distinct integer types, meaning that you need to explicitly convert them to another, or get warnings on wrong conversions.
Otherwise I actually prefer to use the explicit struct prefix you normally have in C, on enums I don't really like it, and I'm split on union, but you don't really pass around unions that often.
But as many things, it really depends on the style of program you write. Sometimes it's more worth it to be entirely specific when you use a primitive type, or a struct, and it's easier if you can just see it in every local declaration.
2
u/GhostVlvin 16h ago
Am I right that only thing that typename is different in is that unlike "#define" compiler says "type (aka orig_type)"?
1
u/AstronomerStrange165 21h ago
It isn't the coolest thing ever. I only use it for function pointers and whenever I have a good reason to.
1
u/Horta-horta 19h ago
I dont like typedefs because you cant undefine a typedef
1
u/hypersonicwilliam569 11h ago
I don’t use typedefs
I’m too ignorant to :-)
1
u/MadProgrammer12 2h ago
i use typedefs on unions, but for structs, pointers, or anything else, i prefer using the raw type
1
u/Key_River7180 23h ago
typedef sucks