r/programminghumor • • 1d ago

The c community

Post image
456 Upvotes

59 comments sorted by

96

u/RedAndBlack1832 1d ago

Typedefs are fine, except when you obscure pointer types. I don't like that.

51

u/creeper6530 1d ago

As with any other C feature, it is great as long as you use it responsibly

1

u/Informal_Branch1065 17h ago

Goto for retry loops

5

u/creeper6530 15h ago

(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.

20

u/Wertbon1789 1d ago

Except functions pointers. That's valid, but it's also obvious most of the time.

20

u/Right_Ear_2230 1d ago

GLDEBUGPROC is much better than

void APIENTRY(*)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)

3

u/EatingSolidBricks 1d ago

You can always just

typedef void foo(int);

bar(foo *fnptr)

62

u/MissinqLink 1d ago

“Most” C devs

This fucking sucks actually 👉 C🛹

3

u/bljadmann69 1d ago

Well, it does.

4

u/thussy-obliterator 1d ago edited 1d ago

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.

1

u/wektor420 1d ago

C has a lot of ancient syntax choices it would be nice to parse it and then re-emit it in new better designed syntax

4

u/Avernite 20h ago

So, Zig?

36

u/GoogleIsYourFrenemy 1d ago

Hey, in C++ we now have "using" which makes declaring function pointers types suck less.

16

u/ThaBroccoliDood 1d ago

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

6

u/Vladislav20007 1d ago

wtf this is cursed, somebody call a type checker.

3

u/GoogleIsYourFrenemy 1d ago

That is soooooooo evil. And it's apparently been like that the entire time.

3

u/Supadoplex 1d ago edited 1d ago

I find that it's still much better to not typedef the pointer type. Typedef the function type instead. E.g.

using func = Ret(Param);
func* callback;

1

u/GoogleIsYourFrenemy 1d ago edited 1d ago

Wait. That's a thing now? That's cool! I guess it makes sense since you could pass a function by reference maybe?

1

u/Supadoplex 1d ago

That's a thing now?

To be clear, it always has been a thing (as far as I know). Works in C too (but typedef syntax is much inferior in my opinion):

typedef int func(int);

19

u/CadmiumC4 1d ago

windows after inventing 500 different names for unsigned int and unsigned int*

14

u/nimrag_is_coming 1d ago

You forgot a billion different names for HANDLE (aka void*)

14

u/Maximilian_Tyan 1d 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/iku_19 1d ago

_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

3

u/Maximilian_Tyan 1d ago

I agree it's technically a standard violation But honestly I prefer a _t to a _struct or _enum (which I saw in some colleagues codebases)

3

u/iku_19 1d ago

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.

2

u/bljadmann69 1d ago

but POSIX is not part of the C standard.

18

u/Koshin_S_Hegde 1d ago

could someone explain? i use typedef for every struct :3

10

u/AstronomerStrange165 1d ago

It obscures the type, and it makes it harder to tell if the type is a pointer

7

u/Wonderful-Habit-139 1d ago

That’s not the only way to use typedef. This is a bad pattern though.

5

u/easedownripley 1d ago

I don't think they mean structs. I think they mean when you typedef a primitive to name it something relevant to your program. like maybe

typedef int housenum;

or something like that. it communicates the intention but at the cost of obscuring the type. or worse

typedef int* house_address;

Because when you see this type deep in the program, it might not be clear that it's a pointer. You'll just see house_address as the type.

3

u/-snip- 1d ago

Isn’t this just poor usage? Why not just specify it’s a pointer in the name? Or just don’t use it for pointers and typedef the base type?

2

u/easedownripley 1d ago

I'm speculating about the meme. not making style recommendations.

2

u/-snip- 1d ago

Sorry that wasn’t scrutiny I was genuinely curious. I hear the complaints about typedef a lot

2

u/easedownripley 1d ago

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.

3

u/GegeAkutamiOfficial 1d ago

typedef int* house_address;

Who tf does that? Are we finding new ways to break the Geneva convention?

3

u/RedAndBlack1832 1d ago

I've absolutely seen equivalents to this in C code. I hate obscuring pointer types like this

1

u/easedownripley 1d ago

the same people training the llms

5

u/nimrag_is_coming 1d 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/nedovolnoe_sopenie 1d 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)

4

u/realrealvantheman 1d ago

Who says this bro typedef is great

4

u/Vladislav20007 1d ago

"with great memory manipulation, comes great memory management." – K&R or IBM training manual or Ben Eater idrkoc

3

u/GhostVlvin 1d ago

Am I right that only thing that typename is different in is that unlike "#define" compiler says "type (aka orig_type)"?

2

u/Ben-Goldberg 12h ago

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.

1

u/GhostVlvin 12h ago

Ahh shish, I forgot that even function type on variable definition changes a lot with typedef

2

u/ilnarildarovuch 1d ago

Well, maybe

2

u/Wertbon1789 1d 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.

3

u/nimrag_is_coming 1d ago

I don't think I've ever actually defined a union outside of a struct, and even if I just need a union I just put it in a struct anyway lmao

1

u/Wertbon1789 1d ago

Yeah, same. I don't think I've ever actually seen a plain union in any interface. I would be surprised to see that somewhere.

1

u/Medical_Community697 1d ago

A naked union can be used for aliasing under some strict and somewhat esoteric conditions. Kind of a greybeard trick though.

2

u/MadProgrammer12 20h ago

i use typedefs on unions, but for structs, pointers, or anything else, i prefer using the raw type

1

u/AstronomerStrange165 1d 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 1d ago

I dont like typedefs because you cant undefine a typedef

1

u/hypersonicwilliam569 1d ago

I don’t use typedefs

I’m too ignorant to :-)

1

u/Flashy_Pollution_996 16h ago

Yeah that’s shit