(I presume nested loops.) Is responsible? Go ahead!
More modern languages however have labelled loops, so that you can use continue and break and disambiguate which of the nested loops you're breaking out of, and is basically a safe goto.
When I'm bored I write retry loops with goto just to fuck with whoever might read my code. But I'm too scared too, so I only do it in functions that are already small and only do one thing.
People always forget that you can make typedefs for function types (without pointer!). That way you don't ever have to use the ugly function pointer syntax.
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.
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
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.
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)
Yes, but the original type is something weird, like a multidimensional array of function pointers, you can make your code much clearer with a typedef instead of using the raw type.
If typedef didn't exist people would write void* everywhere because lazy.
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.
Typedefs are just sugar syntax that hides functionality - like by "making a type" your hiding that actual type of the variable and i dont want to dig around include headers to figure out storage sizes or how many times i have to deference.
Man. Maybe I just write too much code just to get things done, and too much MPI code and too many Python extensions, but if you took my typedef away I WOULD unapologetically double down on #define and nobody wants that. (Jk, I don't actually give a damn. Whatever works. I thought my code was nasty with it until I looked at the MPI source files to get insight into a bug, and now I live without shame.) Are "most" C devs babies?
127
u/RedAndBlack1832 6d ago
Typedefs are fine, except when you obscure pointer types. I don't like that.
80
u/MissinqLink 6d ago
“Most” C devs
This fucking sucks actually 👉 C🛹
42
u/GoogleIsYourFrenemy 6d ago
Hey, in C++ we now have "using" which makes declaring function pointers types suck less.
27
u/CadmiumC4 6d ago
windows after inventing 500 different names for unsigned int and unsigned int*
20
u/Maximilian_Tyan 6d 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)
8
u/nimrag_is_coming 6d 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
18
u/Koshin_S_Hegde 6d ago
could someone explain? i use typedef for every struct :3
6
u/Vladislav20007 6d ago
"with great memory manipulation, comes great memory management." – K&R or IBM training manual or Ben Eater idrkoc
5
u/nedovolnoe_sopenie 6d 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)
5
u/realrealvantheman 6d ago
Who says this bro typedef is great
4
u/GhostVlvin 6d ago
Am I right that only thing that typename is different in is that unlike "#define" compiler says "type (aka orig_type)"?
3
u/MadProgrammer12 5d ago
i use typedefs on unions, but for structs, pointers, or anything else, i prefer using the raw type
2
u/ilnarildarovuch 6d ago
Well, maybe
2
u/Wertbon1789 6d 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/hypersonicwilliam569 5d ago
I don’t use typedefs
I’m too ignorant to :-)
1
u/AstronomerStrange165 6d 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 6d ago
I dont like typedefs because you cant undefine a typedef
1
u/Flashy_Pollution_996 5d ago
Yeah that’s shit
1
u/clippy_is_a_prick 2d ago
Typedefs are just sugar syntax that hides functionality - like by "making a type" your hiding that actual type of the variable and i dont want to dig around include headers to figure out storage sizes or how many times i have to deference.
That and the ternary operator can get fucked.
1
u/Internal-Cellist-920 1d ago
Man. Maybe I just write too much code just to get things done, and too much MPI code and too many Python extensions, but if you took my typedef away I WOULD unapologetically double down on #define and nobody wants that. (Jk, I don't actually give a damn. Whatever works. I thought my code was nasty with it until I looked at the MPI source files to get insight into a bug, and now I live without shame.) Are "most" C devs babies?
0
u/Key_River7180 6d ago
typedef sucks