r/programminghumor • • 6d ago

The c community

Post image
671 Upvotes

66 comments sorted by

127

u/RedAndBlack1832 6d ago

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

68

u/creeper6530 6d ago

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

6

u/Informal_Branch1065 5d ago

Goto for retry loops

7

u/creeper6530 5d 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.

2

u/RedAndBlack1832 4d ago

I'd rather leave them only for end of function cleanup bc I'm scared lmao

1

u/Informal_Branch1065 4d ago

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.

21

u/Wertbon1789 6d ago

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

21

u/Right_Ear_2230 6d ago

GLDEBUGPROC is much better than

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

4

u/EatingSolidBricks 5d ago

You can always just

typedef void foo(int);

bar(foo *fnptr)

1

u/_yrlf 3d ago

this this this!

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.

80

u/MissinqLink 6d ago

“Most” C devs

This fucking sucks actually 👉 C🛹

5

u/thussy-obliterator 5d ago edited 5d 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.

2

u/bljadmann69 5d ago

Well, it does.

1

u/wektor420 5d 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

5

u/Avernite 5d ago

So, Zig?

42

u/GoogleIsYourFrenemy 6d ago

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

20

u/ThaBroccoliDood 6d 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

9

u/Vladislav20007 6d ago

wtf this is cursed, somebody call a type checker.

4

u/GoogleIsYourFrenemy 5d ago

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

3

u/Supadoplex 6d ago edited 6d 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 5d ago edited 5d 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 5d 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);

27

u/CadmiumC4 6d ago

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

18

u/nimrag_is_coming 6d ago

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

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)

9

u/iku_19 6d 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

4

u/Maximilian_Tyan 6d 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)

4

u/iku_19 6d 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 5d ago

but POSIX is not part of the C standard.

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/easedownripley 6d 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- 6d 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 6d ago

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

2

u/-snip- 6d ago

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

2

u/easedownripley 6d 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 5d ago

typedef int* house_address;

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

3

u/RedAndBlack1832 5d ago

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

1

u/easedownripley 5d ago

the same people training the llms

11

u/AstronomerStrange165 6d ago

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

7

u/Wonderful-Habit-139 6d ago

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

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)

1

u/Particular_Main5354 18h ago

what will you do about constexpr ?

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)"?

2

u/Ben-Goldberg 5d 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 5d ago

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

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.

3

u/nimrag_is_coming 5d 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 5d 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 5d ago

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

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?