Live data from Hacker News

My personal C coding style as of late 2023

nullprogram.com

381–390 of 466 posts

Re: My personal C coding style as of late 2023

#381
post #142

Earlier quoted context omitted.

Yeah but not for basic types. Also, most code mingles sooner or later with other code. Than this is just ugly.

It’s better than dealing with needlessly long type names like uint32_t though

uint32_t isn't that long, and is quite clear what type it means.

Re: My personal C coding style as of late 2023

#382

Earlier quoted context omitted.

Long type name?? uint32_t is literally 8 characters long. There's not much to cut here.

It’s longer than it needs to be when you’re typing it out so many times

What are we trying to optimize, the number of characters to type or clarity/readbility?

Re: My personal C coding style as of late 2023

#383

Earlier quoted context omitted.

Everything around the rules of destruction in derived classes from a base class with/without a virtual destructor. How about capture of values in a lambda within a loop? How to prevent a template expansion from killing you build process? When are move semantics sufficient for the std container classes and when are they not? What's the order of construction of multiple objects at file scope? When should a copy constru…

> I'm typing on a phone But you have an uncontrollable urge to write here? Someone's holding a gun to your head? > You are not disagreeing with me when you make that claim, you're disagreeing with one of the world's foremost experts on C++. I guess that settles everything then. Never mind that you're misquoting him. Look, if you hadn't written the last two paragraphs, I'd have replied to your points, but they strongl…

> I guess that settles everything then. Never mind that you're misquoting him.

I'm not misquoting him - you are free to provide a link to the context in which he said what he said.

The worlds foremost expert in C++, author of dozens of books on C++, disagrees with you. I'm merely agreeing with him.

> Look, if you hadn't written the last two paragraphs, I'd have replied to your points, but they strongly indicate it would fall into deaf ears. You're clearly more interested in entertaining the peanut gallery more than actual discussion.

The fact that you entered a thread about C practices, then got all salty when you tried to go with the "but why not use C++?" argument, then devolved into personal attacks is ... well "classy" is not the word I'd use.

EDIT: You can't respond to those points - those are all well-known footguns that are present in C++ but not in C. What were you going to respond with? "No, C++ doesn't have those!"?

Re: My personal C coding style as of late 2023

#384

Earlier quoted context omitted.

> carrying bit length in the name is critical I beg to disagree. In D: byte - 8 bits short - 16 bits int - 32 bits long - 64 bits absolutely nobody is confused about this.

When we move into the 128-bit CPU era, will we call 128-bit integers "super long"? Maybe "elongated". Maybe "huge"? Or, you know, we could just name them all by bit length and completely future-proof this system.

We call them "cent" and "ucent" in D :-)

Re: My personal C coding style as of late 2023

#385
post #232

Earlier quoted context omitted.

> carrying bit length in the name is critical I beg to disagree. In D: byte - 8 bits short - 16 bits int - 32 bits long - 64 bits absolutely nobody is confused about this.

Of course plenty of people are confused, the overhead of "short/long" just makes no sense, but yet another bad design from the past carefully preserved

Haven't run into a confused one yet, and D has been around 20 years.

Re: My personal C coding style as of late 2023

#386

Earlier quoted context omitted.

it has defined the type for very long: it's cent and ucent. It hasn't implemented it completely though, but named and defined it is already forever.

So will the 256bit one be dollar or euro?

We'll think of something. Perhaps `bright`?

Re: My personal C coding style as of late 2023

#387

IMO, defining your own types is one step too far. Now everyone who is already familiar with C types has to learn your own quirky system to understand one program. I think it does probably make sense to be specific about the sizes though e.g. using uint32_t over just uint (and expecting to receive some architecture-dependent size you might not get with uint.) These types should be defined in the right header (I think…

The u32/i64/... naming system is pretty common.

Assuming you can trust those types to be what they look like, the code is readable.

I've worked with C for well over 30 years; custom typedefs are par for the course. Work with OpenMAX libs? You have OMX_U32. On Windows? You have DWORD. Using Glib? guint32 ...

Re: My personal C coding style as of late 2023

#388

Earlier quoted context omitted.

The reality is that C `int` is 32 bits in size. Sure, that's not true for 16 bit targets. But are you really going to port a 5Mb program to 16 bits? It's not worth worrying about. Your code is highly unlikely to be portable to 16 bits anyway. The problem is with `long`, which is 32 bits on some machines and 64 bits on others. This is just madness. Fortunately, `long long` is always 64 bits, so it makes sense to just…

Yet another issue is that `char` is signed on some platforms but unsigned on others. It is signed on x86 but unsigned on RISC-V. On ARM it could be either (ARM standard is unsigned, Apple does signed). I therefore use typedefs called `byte` and `ubyte` wherever the data is 8-bit but not character data. I also use the aliases `ushort`, `uint` and `ulong` to cut down on typing. On the other hand, the types in are often…

Yes, the optional sign on char is also madness. C had a chance in 1989 to make it unsigned, and muffed it. (When C86 decided between value-preserving and sign-preserving semantics, they could have also said char was unsigned, and saved generations of programmers from grief.)

D's `char` type is unsigned. Done. No more problems.

Re: My personal C coding style as of late 2023

#389

Earlier quoted context omitted.

Yet another issue is that `char` is signed on some platforms but unsigned on others. It is signed on x86 but unsigned on RISC-V. On ARM it could be either (ARM standard is unsigned, Apple does signed). I therefore use typedefs called `byte` and `ubyte` wherever the data is 8-bit but not character data. I also use the aliases `ushort`, `uint` and `ulong` to cut down on typing. On the other hand, the types in are often…

Default signed/unsigned is not a platform convention but a compiler one. You can change it with a compiler switch, in the makefile.

Um, it was dependent on how the CPU handled it back in those days. That problem went away, though.

Re: My personal C coding style as of late 2023

#390

IMO, defining your own types is one step too far. Now everyone who is already familiar with C types has to learn your own quirky system to understand one program. I think it does probably make sense to be specific about the sizes though e.g. using uint32_t over just uint (and expecting to receive some architecture-dependent size you might not get with uint.) These types should be defined in the right header (I think…

The reality is that C `int` is 32 bits in size. Sure, that's not true for 16 bit targets. But are you really going to port a 5Mb program to 16 bits? It's not worth worrying about. Your code is highly unlikely to be portable to 16 bits anyway. The problem is with `long`, which is 32 bits on some machines and 64 bits on others. This is just madness. Fortunately, `long long` is always 64 bits, so it makes sense to just…

The Motorola 68K family has been targeted by C compilers configured with 16 bit int.
Post reply on HN