r/programminghumor • • 1d ago

The c community

Post image
336 Upvotes

54 comments sorted by

77

u/RedAndBlack1832 23h ago

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

38

u/creeper6530 21h ago

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

17

u/Wertbon1789 16h ago

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

18

u/Right_Ear_2230 15h ago

GLDEBUGPROC is much better than

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

2

u/EatingSolidBricks 7h ago

You can always just

typedef void foo(int);

bar(foo *fnptr)

53

u/MissinqLink 1d ago

“Most” C devs

This fucking sucks actually 👉 C🛹

3

u/bljadmann69 12h ago

Well, it does.

3

u/thussy-obliterator 11h ago edited 11h 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 10h 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

1

u/Avernite 2h ago

So, Zig?

33

u/GoogleIsYourFrenemy 1d ago

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

12

u/ThaBroccoliDood 17h 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

4

u/Vladislav20007 16h ago

wtf this is cursed, somebody call a type checker.

1

u/GoogleIsYourFrenemy 11h ago

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

3

u/Supadoplex 17h ago edited 17h 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 11h ago edited 11h 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 10h 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);

16

u/CadmiumC4 18h ago

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

12

u/nimrag_is_coming 15h ago

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

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)

7

u/iku_19 17h 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 16h 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)

2

u/iku_19 16h 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.

1

u/bljadmann69 12h 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 21h ago

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

7

u/Wonderful-Habit-139 20h ago

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

5

u/easedownripley 17h 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- 15h 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 15h ago

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

2

u/-snip- 15h ago

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

2

u/easedownripley 15h 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 12h ago

typedef int* house_address;

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

3

u/RedAndBlack1832 12h ago

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

1

u/easedownripley 11h ago

the same people training the llms

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/nimrag_is_coming 12h 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 12h 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 12h ago

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

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