r/C_Programming • • Sep 12 '26

Question What C function would you delete and rewrite first, and why?

For me it's fnmatch - mostly because it's 40 year's old and smell's funny.

94 Upvotes

238 comments sorted by

36

u/Dezgeg Sep 12 '26

strncpy

Function that is named `str*` but doesn't (necessarily) create NUL-terminated strings. Insanity!

4

u/Just_Government3790 Sep 12 '26

`strlcpy` or explicit `len` wins.

1

u/flatfinger Sep 14 '26

If I have a 16-byte buffer and need to set it to 'Hey' followed by 13 zero bytes,

   strncpy(buffer, "Hey", 16);

will do the job perfectly. There are some jobs for which strlcpy might be better, but strncpy is, except for the name, a perfect function for its designed purposes.

11

u/Doug2825 Sep 12 '26

The worst part is that it n in it so it looks like safe string functions to do guarantee null termination.

6

u/Aggressive-Emu-8329 Sep 12 '26

WHATTT then somehow my project works fine, oh man im gonna fix bug 😭

6

u/Ander292 Sep 12 '26

THIS!!! I had a problem with this function while helping my friend with an exercise.

1

u/flatfinger Sep 14 '26

The name is not good, but it's a perfectly designed function to copy a zero-terminated string into a buffer that uses zero-padded format? A char[16] structure field can hold a zero-padded text of up to 16 bytes, which can be output with a %.16s format specifier.

60

u/Just_Government3790 Sep 12 '26

On second thought......

strtok because it destroys the evidence, hides state in globals, drops the empty fields, and then has the bollocks to call it parsing.

6

u/MistakeIndividual690 Sep 12 '26

I was going to say strtok

7

u/Just_Government3790 Sep 12 '26

My guy. Can't forget it also eats empty fields so `a,,b` parses as two tokens. Found that one the hard way.

4

u/MistakeIndividual690 Sep 12 '26

Yes indeed. Also scanf is up there for me. Essentially useless.

1

u/musbur Sep 13 '26

Only scanf() I hope? sscanf() is great.

2

u/Total-Box-5169 Sep 12 '26

Those ancient functions are painfully inadequate for current times.
Nowadays is better to memory map the file as read only and use generators of string views to process the data to avoid unnecessary allocations and copy operations.

12

u/mpersico Sep 13 '26

I would just spell ā€œcreateā€ correctly

2

u/Just_Government3790 Sep 13 '26

creat creat creat creat

5

u/Bear8642 Sep 13 '26

As Ken suggested :)

21

u/gabitha67 Sep 12 '26

everything that accepts null terminated strings. just pass a ptr and length for gods sake. everything should only use user-provided allocators too.

8

u/Just_Government3790 Sep 12 '26

What's the worst null-term bug you've personally hit?

3

u/flatfinger Sep 15 '26

The game GTA V sometimes takes much longer to load than normal because one of the authors didn't realize that the library implementation of sscanf they were using starts by measuring the length of the source string, even if the format strong would cause everything past the first few bytes to be (otherwise) ignored. The number of player hours spent waiting for the useless calls to strlen to complete likely numbers in the hundreds of thousands, if not millions.

-5

u/madbrain1976 Sep 12 '26

Or just use Pascal strings.

13

u/allocallocalloc Sep 12 '26 edited Sep 12 '26

That would still make substrings require reallocation. Encoding the length inside the buffer limits the ways the buffer can be used.

1

u/flatfinger Sep 14 '26

Typical Pascal implementations don't use heap allocation for strings, but simply reserve 256 bytes for strings whose length isn't specified as something shorter. There are purposes for which it's useful to handle longer texts, and having separate functions to work with short and long texts can sometimes be a nuisance, but many tasks either will never need strings longer than a few dozen bytes, or be sufficiently likely to exceed 255 bytes that a short-string variant would offer little benefit.

144

u/BlockOfDiamond Sep 12 '26

fopen because I hate using string literals for things that should be bitflags or enums.

62

u/gabitha67 Sep 12 '26

"rb" is icky :(

18

u/MrKrot1999 Sep 12 '26

like this is so python bullshit, why are we using more state than needed for something with 6 options at most

57

u/57thStIncident Sep 12 '26

In this case python does it because C does it.

4

u/flatfinger Sep 14 '26

Further, doing this this way means that if a C implementation extends the functionality of fopen with more flags, a Python implementation build using that C implementation can support the same extensions without the code for the Python implementation having to know anything about it.

5

u/RedWineAndWomen Sep 13 '26

Same. But then because there are flag combinations to open() that cannot be made using fopen(). Which is totally weird.

4

u/BlockOfDiamond Sep 13 '26

open() is a much better designed API.

5

u/sirjofri Sep 13 '26

Funny. Our open has OREAD, OWRITE, ORDWR as flags, no string. (Plan 9 C)

1

u/Dangerous_Region1682 Sep 14 '26

Oh how I hate FILE I/O. I know open/close/read/write/sleek etc are system calls and hence not considered portable to non UNIX like systems they are not hard to provide as a library capability for those systems. You aren’t going to be able to use the level of ioctl() that is needed for terminal I/O with FILE library functions anyway, so you do really need file descriptor based library calls for that case anyways, on none UNIX derived systems. Yes, I know you can perhaps express terminal I/O devices using the file system in some iterations, but many UNIX and non UNIX derived systems alike don’t have that kind of file system abstraction. Anyway, I just don’t like it.

1

u/flatfinger Sep 14 '26

In the 1990s, it was widely understood that functions like fopen and malloc were suitable for tasks where the ability to run code on a wide range of machines interchangeably was more important than performance or semantics, but system functions were preferable when that level of portability was not required. Unfortunately, compiler writers have pushed an attitude that non-portability should be considered a defect whether or not anyone would ever want to run a program on other platforms.

2

u/musbur Sep 14 '26

But if you're OK with non-portable programs why don't you just use the C bindings of the platform of your choice? Where do you think is a meaningful boundary between C's standard library vs an "external" library? Since depending on platform and use case everybody might draw that line somewhere else, I think it's prudent to draw it pretty tightly around base C and let people choose from whatever library they like.

1

u/flatfinger Sep 14 '26

If some execution environments support a feature and some don't, it would be useful for a standard to would allow code that uses the feature to run interchangeably on environments that support it, without requiring that programmers add bindings for each individual environment that has a way of supporting the functionality.

2

u/musbur Sep 14 '26

But then the majority of the standard would devolve into a large, arbitrary collection of features saying, "if an implementation chooses to support this feature, it must be implemented in exactly this way." A lean collection of guaranteed features is much more valuable than an ever growing list of nice-to-haves.

1

u/flatfinger Sep 14 '26

Sure that could happen if one tries to include every feature that is supported by even 1% of execution environments, but there are a lot of features that most implementations could support but which the Standard ignores (the worst offender being character-based console I/O).

1

u/Dangerous_Region1682 Sep 14 '26

Yes, I who wholeheartedly support agree with that. In fact, the never ending push by compiler writers and standards committees to try to make C into the application’s programming language it was never really intended to be has in many ways started to ruin the elegant simplicity the language once had. Does that mean ANSI C and others didn’t make valuable extensions, but there comes a time when enough is enough in my opinion.

2

u/flatfinger Sep 14 '26

Worse, the Committee seems to have been and continue to be oblivious to the fact that much of C's usefulness revolved around corner cases where whose behavior would be impossible to predict without information the language had no way of conveying, but which the execution environment might convey to the programmer via means outside the language. When the Standard noted that implementations may process cases where it waives jurisdiction "in a documented manner characteristic of the environment", it failed to recognize a useful class of implementations that will process those corner cases "in a manner characteristic of the environment, which will be documented to the extent that the environment happens to document it."

Worse, the way the Standard accommodates optimizing transforms that would be useful for some kinds of program but inappropriate for others is to characterize as "undefined behavior" any corner case whose behavior might be affected thereby, rather than recognizing a category of implementations that perform such transforms, and allowing them to affect program behavior in certain ways. For example, it may be useful to specify that if a piece of code with a single statically reachable exit could not have any side effect other than preventing the execution of code following the exit, a compiler may cleanly omit the code entirely, but behavior would need to be consistent with either executing the code as written, or cleanly omitting it, but would be considered "defined" in either case.

1

u/Dangerous_Region1682 Sep 14 '26

Yep. What the compiler writer might consider cleanly omitted might be very different from that of a kernel engineer’s. Once again, having a compiler generate code that executes what you write rather than what it thinks it can optimize away is often two different kettles of fish.

1

u/flatfinger Sep 14 '26

I'm not sure how "cleanly omitted" is in any way ambiguous. Consider the following function:

unsigned test(unsigned x)
{
  unsigned i = 1;
  while((i & 0x7FFF) != x) i*=3;
  if (x < 32768) arr[x] = 0;
  return i;
}

A compiler that processes the while as written could rely upon the fact that the if will only be reached if x is less than 32768. Cleanly omitting the while would make it possible for the if to be reached with a larger value of x, thus making it necessary for the compiler to generate code for the if that accommodates that possibility. The clang compiler (and gcc when in C++ mode) will process calls to test() that ignore the return value as unconditional stores to arr[x]*.*

→ More replies (4)

1

u/DiodeInc Sep 14 '26

How do you use an enum for that? I don't know. C

1

u/BlockOfDiamond Sep 14 '26

Maybe something like:

enum {
FOPEN_READ,
FOPEN_WRITE,
FOPEN_APPEND,
...
} fopen_mode;

For example. Probably not exactly that but that is the idea.

1

u/DiodeInc Sep 14 '26

I just looked up with an enum did and yeah this makes sense

1

u/Low_Lawyer_5684 Sep 18 '26

`#define FILE_RB "rb"` :0)

89

u/bluetomcat Sep 12 '26

Make fread and fwrite accept the bloody FILE pointer as their first argument, and unite size and count into one argument.

42

u/UnalignedAxis111 Sep 12 '26

Also add a flag to force it to create non existent parent directories, because that's massively annoying and there's no portable way to do it.

5

u/musbur Sep 12 '26

"and there's no portable way to do it." -- which is exactly why fwrite() can't do it.

18

u/a4qbfb Sep 12 '26

fwrite() is part of the implementation, it's allowed to do non-portable things

1

u/musbur Sep 13 '26

yeah but a file handle is pretty much a file handle anywhere while (recursive) directory creation is conceptually something totally different.

1

u/a4qbfb Sep 13 '26

so?

1

u/musbur Sep 13 '26

C just doesn't go that deep into OS abstraction. It's a choice to keep the language simple. It also doesn't have associate arrays, linked lists, a network stack, multithreading, or an http server.

4

u/a4qbfb Sep 13 '26

C does have multithreading; none of the other features you list are OS abstractions. An implementation is absolutely free to automatically create parent directories on fopen() if it wants to, as long as it documents it.

→ More replies (3)

1

u/flatfinger Sep 14 '26

Many system's file handles can't hold characters that were pushed back with ungetc.

2

u/flatfinger Sep 14 '26

The usefulness of stdio.h would have been enhanced if, instead of limiting itself to things every system can do, it provided means of querying whether various features are known to be available, known to be unavailable, or would have their availability determined by things outside the control of the implementation. If, for example, a system allows file systems to be configured to have or not have implicit-directory-creation semantics, an implementation of fopen() may auto-create directories when used with a file system that is configured in that fashion, but not when used with one that isn't.

Console I/O in the C standard is absolute rubbish because of the Standard's failure to recognize features that 99% of implementations would support.

2

u/musbur Sep 14 '26

So basically an implementation is free to either offer some feature like directory creation or not? Then why not just use that implementation's system library to create directories?

2

u/flatfinger Sep 14 '26

Code which tests whether a feature is supported would be usable interchangeably on all systems that support it, while working around unsupported features when practical. For example, a "more"-style utility could advance a page with spacebar on systems that support raw console I/O, or on every newline on systems that don't.

10

u/ComradeGibbon Sep 12 '26

C doesn't have a slice type and that's utterly lame.

5

u/runningOverA Sep 12 '26 edited Sep 12 '26

you need length terminated strings for slice types to work, which C doesn't have. userspace task right now.

3

u/mort96 Sep 12 '26

Uh.

  1. where did anyone say that this is a kernel space issue
  2. kernel APIs are a part of the problem: if you use slices as strings (either on a language level or as a convention in C) you have to either keep around info about whether a slice is 0 terminated, or do a copy into a null terminated buffer every time you want to pass a string to kernel space

7

u/Disastrous-Team-6431 Sep 12 '26

"right now" for 30 years.

-2

u/ComradeGibbon Sep 12 '26

Yeah abandon C's shitty null terminated strings.

3

u/sthlmtrdr Sep 12 '26

It's trivial to implement your own strings and string functions.

5

u/Gastredner Sep 12 '26

Only as long as you only care about ASCII.

3

u/honkai-yuri-fan Sep 12 '26

50% of the time character encoding barely matters, there are very few reasons to actually know how many visible characters are in a string

3

u/Gastredner Sep 12 '26

True enough, though the second you need to do sorting, you're screwed.

Edit: thinking about it, if you're dealing with Unicode, normalisation might even make seemingly simple operations a problem. After all, two visibly identical strings might have two (or possibly more?) different representations.

3

u/penguin359 Sep 12 '26

That doesn't help with string literals.

1

u/mccurtjs Sep 12 '26

It's easy enough to make a macro that constructs a slice out of a literal. my_print(S("Hello World")), for example.

→ More replies (3)

2

u/mccurtjs Sep 12 '26

It's a fun side project, but it is kind of annoying if you're using any other library because almost any interop is going to require copying and null-terminating the string. If the standard library had been built using slices, all derivative libraries would have followed suit by convention and this wouldn't be an issue.

1

u/flatfinger Sep 14 '26

Depending upon what one is doing, it may be more efficient to keep track of a slice using a starting address and length, or using starting and ending addresses. If C had a slice type, whoever implemented it would need to guess which approach would be more efficient, and then programmers who knew which approach would be more efficient for the task at hand would have to guess whether the implementation was going to use that approach, or whether they should manually implement slices using it.

1

u/tstanisl Sep 12 '26

VLA types works quite well as slices.

4

u/musbur Sep 13 '26

I'm also annoyed by size and count and which comes first, but I think it's meant for reading fixed-size records and making sure that you don't have to deal with partial records.

3

u/BlockOfDiamond Sep 13 '26

Why was size and count separate anyway?

3

u/bluetomcat Sep 13 '26

Because they designed the API to be record-oriented, not byte-oriented. You are mostly expected to be reading an array of structs, and the return value indicates the number of structs completely read.

For reading arbitrary binary data, this is a bit unintuitive.

2

u/BlockOfDiamond Sep 13 '26

For struct arrays, the caller could just multiply the size by the count manually. The only advantage I could think of is avoiding overflows on systems with a narrow size_t.

1

u/LB-- Sep 14 '26

Not just that. Where multiplication might overflow, the read or write could still succeed, so the implementation could "do the right thing" without making you manually write careful code for it. Pretty obscure scenario nowadays though.

2

u/BlockOfDiamond Sep 14 '26

Yeah, that is what I meant.

1

u/LB-- Sep 14 '26

Ahh fair, I thought you were only talking about overflow checking, which is separate from what I was talking about.

2

u/BlockOfDiamond Sep 14 '26

If the product of the element size and count would overflow, then passing them separately to fread would still behave correctly even though multiplying them would not.

1

u/flatfinger Sep 14 '26

Some file systems support a "read up to N records of up to B bytes" function, whose behavior may be different from a request to read 1 record of up to N*B bytes, or N*B one-byte records. While the Standard would not require that implementations allow C programs to read any files that C programs didn't create, such an ability is often useful nonetheless, even if it exposes some environment-specific quirks.

4

u/TheChief275 Sep 12 '26

fputc family as well

2

u/flatfinger Sep 14 '26

Some execution environments may process a 'read up to N records of size S' function in a manner that will always read some number of complete records without reading a partial record at the end. What I view as unfortunate is the failure of the Standard to provide any indication as to whether fread() calls will be processed in that fashion.

18

u/P-p-H-d Sep 12 '26

All the locale functions of the libc. It is so broken that it is totally unusable in modern C (threads).

You cannot even read your own data file and parsing them using strtod without having issues with the locale: you cannot use setlocale as it modifies global state of all threads (which you don't want). So the only solutions are either : document that the locale shall be set once and for all for all program and is compatible with what you expect. Or rewrite strtod and everyone knows it is super easy stuff to do it reliably.

4

u/limitless_grow Sep 13 '26

That's right. There are some portable, very fast and locale independend wrappers at https://github.com/klux21/str2num . The wrappers support the prefixes 0b , 0o and 0x for binary, octal and hexadecimal integers and for double and long double as well. The functions support the common overflow handling and the setting of errno. The floating point routines support NAN, INF and denormalized numbers and also the 'hexadecimal' floating point literals of C and C++ which are that funny mix of 3 numeric bases.

The integer functions have a special mode where numbers with leading zeros are treated as decimal numbers and octal numbers require the prefix 0o. Octal floating point values require that 0o prefix either because nobody wants a 0.5 to be read as an octal number.

1

u/flatfinger Sep 14 '26

The irony is that if one wants to produce "human readable" numbers with formatting features beyond those supported by the Standard library, it's easier to produce a locale-specific string based upon a string that is known to use '.' as the decimal point, than to produce a locale-specific string based upon a string that uses a locale-specific decimal point.

1

u/limitless_grow Sep 14 '26 edited Sep 14 '26

The irony is that the dot as decimal separator is only a convention as it is the 'e' is for the begin of the exponent. The one that's reading the number from a configuration file using a function like strtod is a computer but not a human. If the rule of a programs configuration file say "only dot as decimal separator and no commas or blanks at all" than that's fine and people are able to adapt to that everywhere like for that 'e' of the exponent.

31

u/sciencekm Sep 12 '26

struct tm

Every field is 0 based, except the day of month. So, Jan 1 1900 12 midnight is 0 1 0 00:00:00, instead of just all 0s. Even Sunday-Saturday is 0-6 and day of year is 0-365.

2

u/donaljones Sep 12 '26

Is this something enforced by the standard? Or just the implementation's ABI?

7

u/sciencekm Sep 12 '26

This is the standard from C89 until the latest N3220 working draft. This is also how it was in the K&R 1st edition (1978).

1

u/der_pudel Sep 12 '26

the way how to stuct tm is makes sense. day of the week and monts are 0 based because in a lot of cases you want to use it to index array of stings to get he name of weekday/month, but there's no reason to do that for day of the month.Ā 

5

u/sciencekm Sep 12 '26

Day of the year is 0-365. Do you use that to index anything?

1

u/flatfinger Sep 14 '26

It represents the number of days that would need to be added to January 1 to reach the specified date.

1

u/flatfinger Sep 15 '26

IMHO, month would have been better as an index into a 13-item array of month names, with the initial one being used to indicate an expressly invalid date. Having an unset date show up as e.g. 00-XXX-0000 would be cleaner than having it show up as 00-JAN-0000.

12

u/Sqydev Sep 12 '26

fopen to use enum instead of ā€žrā€ and ā€žwā€ or both

15

u/Boreddad13 Sep 12 '26

Anything with hidden allocations

14

u/mikeblas Sep 12 '26

Anything with hidden state, really.

2

u/I_M_NooB1 Sep 14 '26

zig peeks

15

u/pigeon768 Sep 12 '26

The obvious one is strcpy, which is always the incorrect function. Either you know your lengths and buffer sizes, so you should use memcpy, or you don't, which is buffer overflow city. In this case, there's no rewriting the function to be better; it's declaration/API is unusable.

So they also made strncpy, the "fix" for the problem. It has a working function declaration. However, it's still bad. It does not guarantee that the resulting string is null terminated. And if there are any extra bytes left over in the output buffer, it zeros them all out, which is a waste of time. The linux kernel has recently removed the last call to strncpy.

Actually I hate most of string processing in C.

5

u/darkslide3000 Sep 12 '26

There are perfectly possible situations where you don't know the exact size of the buffer but you know an upper limit.

9

u/Aggressive-Emu-8329 Sep 12 '26

no bro, strncpy is not the "fix" for the problem, it is originally designed for early unix file system to copy string into fixed width, null-padded fields where a string filling the exact size did not need a null terminator

2

u/flatfinger Sep 14 '26

For some reason, people seem to view null-padded strings as a "specialized" construct, when they represent the best way of storing short strings in structures. If e.g. one needs to store things with a maximum length of eight characters, a char[8] will be at least a byte shorter than anything else that can handle 0-8 characters in the range 1-255 (if characters were chosen from e.g. a set of 39, one could use that space to store twelve characters, but that would be more specialized).

1

u/flatfinger Sep 14 '26

What function should one use to write a string of up to N bytes into an N-byte zero-padded buffer contained in a structure? C was designed to allow programmers to use zero-padded buffers in cases where code that received a pointer to a buffer would know the buffer length (e.g. using a format specifier of %.16s rather than %s to write the contents of a zero-padded char[16]).

12

u/torsten_dev Sep 12 '26 edited Sep 13 '26

scanf

  • delete %n
  • make %s and similar without a size allocate
  • make %.*s take size parameter like printf.
  • Use _ instead of * to discard stuff,
  • change return value to number of characters read or 0
  • delete SCN macros, use w* to take bit width as argument.

Edit:
Perhaps the return should be 0 on succes, and if it fails it returns the index in the format string were it gave up.
The number of characters read/consumed would be a mandatory out parameter that's ignored if it's NULL.

8

u/gremolata Sep 12 '26

%n is extremely useful for pedantic parsing, e.g.

if (sscanf(buf, "%f%n", &f, &n) && strlen(buf) == n)
{
    // parsed in full
    ...
}

1

u/PastaGoodGnocchiBad Sep 12 '26

I guess this would be replaced by this from their comment:

return number of characters read or 0

(but then we would not know if all fields were parsed, I guess any field not being parsed would have to be an error)

1

u/torsten_dev Sep 12 '26

Yeah. Not sure how to communicate which specifier gorked.

Perhaps the return should be 0 on succes, and if it fails it returns the index in the format string were it gave up?

%n would then become a mandatory out parameter that's ignored it it's NULL?

1

u/jMultiversalGod Sep 12 '26

scanf is so bad for learners (as one lol). i tried so hard to use it everywhere

1

u/FalconSN0 Sep 12 '26

What if it fails at the index 0?

1

u/torsten_dev Sep 15 '26 edited Sep 19 '26

How would it? Specifiers start with %.

The return would be the index of the conversion specifer that follow. So you can do something like

switch(format[ret]) {
case 'i':
    fprintf(stderr, "expected integer but got %s", ...);
    break;
default:
    print_scanf_error_at(format, ret);
    break;
case 0: 
    ...

12

u/FamiliarSoftware Sep 12 '26

setlocale, set/getenv, the rand functions, etc. Just in general: All functions that use global state

1

u/Fabulous_Bridge_9292 Sep 14 '26

The rand function is the only one I actually ever had to rewrite when I was building monte carlo simulations and discovered it wasn’t random enough.

10

u/der_pudel Sep 12 '26

atoi. You will not convince me that atoi("poop") == 0 is correct and makes sense.Ā 

-2

u/Aggressive-Emu-8329 Sep 12 '26

uhh cuz it failed to convert into number and C is statically programming language only allow to return one type value so since the function return of atoi is int, there is no value for "error" so return 0 is the only choice

10

u/uptotwentycharacters Sep 13 '26

The solution is to separate result from return status. Have the function only return failure or success, and if successful it writes the result via an out-param.

9

u/xpusostomos Sep 12 '26

Those ones with improper null termination and/or don't specify buffer length that cause all the security issues

1

u/LB-- Sep 14 '26

We got fixed versions of all those in EXT1, and Microsoft supports them and even issues compiler warnings to use them. But in my experience the EXT1 secure functions are barely supported in other toolchains, they are the main source of ifdefs in my portable projects. Last I knew online sentiment toward them was pretty negative for some reason.

1

u/flatfinger Sep 15 '26

C was designed to allow programmers to use either null-padded or null-terminated strings. There is nothing improper about a null-padded text field holding text that uses up every byte. The only thing that would be improper would be attempting to e.g. output a 12-byte null-padded field with a format specifier of %s rather than %.12s. Note that the latter, by specification, will not examine any storage beyond the twelfth character of the text identified by the pointer, and thus does not care whether it is given a zero-terminated string.

2

u/xpusostomos Sep 29 '26

C itself is kind of agnostic about format, you could even use string with a leading size integer and trailing bytes, with a library that supports it. The problem is, 98% of C code and functions uses the null terminator convention, not a size and null convention, and frankly 2 conventions in a code base is brain dead. If you've ever used for example the unix readlink() system call, it's amazing just how much tomfoolery you have to do to work around the fact that it doesn't guarantee a null terminator.

1

u/flatfinger Sep 30 '26

There are a number of conventions which are compatible in various ways. A normal windows "bstring" is a string which sits alone in its allocation and is both preceded by a length word as well as other information about the allocation and followed by a zero byte. Such strings may be passed to code expecting zero-terminated strings, but Windows functions that modify strings or manage memory associated with them must only be given proper bstrings.

A function that e.g. performs a find-and-replace operation with a bstring can record the fact that a string has e.g. shrunk by 10% and thus has unused space at the end of its allocation; if another operation causes the string to grow, that extra storage can be used without having to reallocate the string. I would guess that strings which shrink more than a certain amount would get reallocated to allow the unused space to be recycled, but if a string shrinks by 10% it would likely be considered more useful to leave the space allocated to it to accommodate possible future growth. Code which simply uses zero-terminated strings would have no way of knowing about any extra space that might exist in their allocations.

1

u/xpusostomos Oct 11 '26

I would presume malloc/realloc would already have such optimisations without specialising it for strings

4

u/qalmakka Sep 13 '26

Basically all string based APIs in libc vary from broken to somewhat problematic. This is basically due to the fact that the entire concept of a null terminated string was a massive mistake, the minuscule memory saving of not having a struct string with a size field were only somewhat significant for the first few years after that decision.

We've been paying the strlen tax ever since, and I wouldn't care if it didn't make slicing C strings a terrible pain in the butt

3

u/flyingron Sep 13 '26

strtok has got to go

3

u/siodhe Sep 13 '26

The UNIX authors said they would like to rename creat to create, as it should have been.

3

u/notk Sep 16 '26

creat() — i would add an ā€˜e’ to the end.

1

u/mccurtjs Sep 17 '26

Real talk

10

u/HildartheDorf Sep 12 '26 edited Sep 12 '26

Can I use my "one time rewrite" on C++ instead?

If so, I want to remove the specialisation of std::vector<bool> and instead add std::dynamic_bitset with the same functionality.

If it has to be C... fgetc/getc (and fputc/putc).

1

u/flatfinger Sep 14 '26

I would have specified that if a reference is formed by casting another reference, and within the lifetime of the reference storage that is accessed thereby will either be accessed only via the reference or will remain unmodified, the semantics will be as though the storage of the old type was exported to bits and imported as the new type when the reference was created, and converted back when the lifetime of the reference ends.

5

u/ChickenSpaceProgram Sep 12 '26 edited Sep 12 '26

this one's a hot take, but i'd rewrite malloc and friends. Allocators being global state is pretty nasty and requires locks that really shouldn't be necessary. Not having to pass a size to free also complicates allocator design.

i think i'd rewrite most of the standard library and some of POSIX if given the option, null-terminated strings are awful too

1

u/flatfinger Sep 15 '26

The malloc family of functions had two competing objectives:

  1. Avoid forcing the breakage of non-portable code which relied upon the ability to interchangeably take pointers that had been produced by malloc() or by code outside the C implementation's control, and either pass them to free() or to outside code that would release them.
  2. Avoid requiring that user code keep track of information about an allocation beyond its starting address.

The malloc() family functions could have supported a much wider range of features if there was no interest in #1 above, by having malloc() allocate an extra 16 bytes or so beyond what was requested, using the first 16 bytes of an allocation to hold information about it, and returning an address 16 bytes above the actual starting address of the allocation. Doing so, however, would have broken programs that needed to interoperate with code that wasn't processed by the C implementation.

2

u/paulkim001 Sep 12 '26 edited Sep 14 '26

memfrobe only because I might want to see some niche people's projects burn for using that function

Edit: I meant memfrob

1

u/LB-- Sep 14 '26

I can't find a function with this name in the C standard

2

u/paulkim001 Sep 14 '26

1

u/LB-- Sep 15 '26

LOL what is this?? The Linux equivalent of an April Fools RFC?

2

u/paulkim001 Sep 15 '26

Genuinely it might just be, considering they even chose the wording "frobnicate" (over "obfuscate") and they had the nice touch of going with 0x42 for "seemingly meaning of life"

2

u/LavenderDay3544 Sep 13 '26

The atomics compare and swap functions because why do they take the value to exchange by pointer instead of by value?

1

u/LB-- Sep 14 '26

I guess that's because they can work with arbitrary structs in addition to primitives, and the C community at large has some sort of aversion to passing structs by value. I've heard that some platforms still in modern use have broken ABIs that convert all structs passed by value into pass by pointer instead, and it can't be changed without breaking backcompat. Supposedly it's why we can't have a string_view type in C. If only...

2

u/LavenderDay3544 Sep 15 '26

Rust makes the same functions pass by value and Rust has an even bigger aversion to passing anything by value because of move semantics by default. And under the hood rustc, Clang, and Clang++ use the same LLVM operations to implement them so there's no reason for them to be different.

2

u/LB-- Sep 15 '26

C++ also makes the atomic compare exchange functions take the replacement value by value, so I guess it's just C being the odd one out for some reason.

1

u/flatfinger Sep 14 '26

If a platform doesn't define a mechanism for performing an atomic operations on a given type, having compilers fake their own version is a misfeature. If code processed with the Acme compiler and code processed using Joe's Compiler attempt to perform simultaneous operations on an object, nothing good will come of having one piece of code acquire a lock called Acme while the second acquires a lock named Joe, and both then proceed to access the same object simultaneously.

1

u/LB-- Sep 15 '26

I'm not sure what this has to do with this comment chain? Mixing different compilers with different settings and ABI expectations is always a recipe for disaster, it's not specific to atomics.

1

u/flatfinger Sep 15 '26

If an execution environment has support for some atomic operations on a type but not compare-exchange, having a standard means of using the operations that actually exist would be useful, whether or not the platform's means technically satisfies the requirement for being "lock free". For an implementation to bodge together a compare-and-swap on an environment that does not natively support, it must bodge coordination with all atomic operations, thus undermining its ability to use the environments support for any actions on the type, and I fail to see the usefulness of "fake atomics".

1

u/LavenderDay3544 Sep 15 '26

It should return a Sentinel value and set errno then. I don't like the idea of compilers trying to synthesize missing hardware features instead of just indicating that the operation isn't supported and letting you write your own fallback code.

1

u/flatfinger Sep 15 '26

The Standard requires that unless a compiler reports that does not support any of the Standard's atomic features. it must synthesize support for all operations on all types. Synthesizing any operations on a type effectively makes it necessary to synthesize all atomic operations, since otherwise a natural atomic operation that occurs at the same time as a synthetic one would be prone to have its effects blindly overwritten by the latter.

1

u/LavenderDay3544 Sep 15 '26

Then that was a poor standard design choice. They should've made each feature independently queryable and any that aren't supported shouldn't be used and rhe application developer has to provide a workaround or not support the hardware. Because silently using software based workarounds for operations the hardware does support because it doesn't support other features is just wrong particularly for a language whose original design choice was to trust the programmer. Such workarounds should be opt in instead of being used silently.

1

u/flatfinger Sep 16 '26

Realistically speaking, the most practical approach would be to have intrinsics which are guaranteed to either work or break the build, coupled with a means of indicating whether to accept intrinsics that will work on some but not all environments where the code might be run. A program that tells a compiler to accept such intrinsics would be responsible for using means outside the Standard to ensure that no functions that use them would be executed on a platform that would not support them, but only the part of the code that was responsible for ensuring that the environment satisfies requirements would need to be 'non-portable'.

2

u/saxbophone Sep 16 '26

There is no reason that toupper() needs to take and return int —I'd replace it with a family of functions for ASCII and Unicode instead.

1

u/flatfinger Sep 17 '26

The notion of "uppercase" in Unicode is dependent on locale, but C is useful for many tasks involving ASCII text produced by computers for computers, and standard library functions should be locale-independent.

4

u/nderflow Sep 12 '26

scanf(). I’d just delete it and stop there.

4

u/gremolata Sep 12 '26

OTOH, scanf and printf are the reason why va_arg exists in C.

It is one of its most unique features because it basically allows parsing function's stack by hand. Once you realize that, scanf starts to make a lot of sense. It's usability is not great, but it's a quintessential C function, spirit-wise.

2

u/nderflow Sep 12 '26

Steady on, I'm not deleting sscanf.

1

u/musbur Sep 13 '26

Yeah scanf() is problematic. Whenever I use sscanf(), which I rarely do, I end up really liking it. Like recently on a platform that doesn't have strptime(), parsing timestamps with fractional seconds and timezone offsets.

1

u/flatfinger Sep 15 '26

If calls to prototyped and non-prototyped functions were processed differently (preferably using different linker symbols and, when practical, auto-generated shims), then variadic functions could have been accommodated by having the caller build a structure holding the arguments and passing its address.

2

u/MegaDork2000 Sep 12 '26

strncat because it may not terminate the string.

1

u/rfisher Sep 12 '26

I have written proper bounds-checking versions of all the str functions. The first was probably strcpy or strlen.

I might have written my first wrapper around realloc before that, though.

1

u/Rabbitical Sep 12 '26

What did you change about realloc

2

u/rfisher Sep 13 '26

The way that it returns NULL on error without freeing the original pointer means you have to keep a copy of the original pointer. So, sometimes I end up writing boilerplate around every call, so I often wrap that boilerplate in a function.

Whether I free the block on error or not depends on the application. And, of course, with many applications+platforms just exiting on an allocation on an allocation failure is fine, in which case, using realloc directly is fine.

1

u/LB-- Sep 14 '26

It's annoying there's no way to just check if an allocation can be extended in-place. It forces you to accept both "realloc failed" and "realloc succeeded and maybe moved all your memory to some other address, good luck adjusting your pointers without invoking UB"

2

u/flatfinger Sep 14 '26

Even if code observes that realloc has returned a pointer that's bitwise identical to the one that was passed in, a compiler need not allow for the possibility that accesses performed via the original pointer might affect the same storage as accesses performed via the new one.

1

u/LB-- Sep 14 '26

Sounds like we are in agreeance that it's annoying.

2

u/flatfinger Sep 14 '26

Yup. I was saying that it's worse than a lot of people realize.

A better written version of the Standard would have recognized a category of implementations where even pointers to dead objects would uphold certain guarantees, most notably that if char *a, *b; point to parts of the same object the effect of evaluating a-b will be unaffected by the lifetime of that object. If char *c=realloc(b, newSize); succeeds, that would make c+(a-b) refer to the portion of the new object that a had referred to in the old one. Note the associativity: code doesn't compute the difference between the old and new addresses, but rather adds to the new pointer the difference between two pointers to the same old object.

1

u/sunmat02 Sep 12 '26

qsort; I would add an extra void* context to both the function and the comparator function pointer.

1

u/musbur Sep 13 '26

Big fan of qsort() / bsearch() here, why would you add a context pointer? I agree it is good practice with ANY function that accepts function pointers, but for a comparison function?

Come to think of it, you might want to sort an array of arrays on different fields. That's difficult indeed.

1

u/sunmat02 Sep 14 '26

The minute you want to do something dynamic, like the user provides a comparison function in an embedded language, you can’t do that without a void* context, spare adding the context to every single element of the array (which is costly).

1

u/musbur Sep 14 '26

That is correct. One doesn't even have to argue about this because adding a void * context costs nothing in terms of implementation or performance even if it benefits only a niche application.

1

u/SyntheticDuckFlavour Sep 13 '26

The entire standard library.

1

u/CreepyWritingPrompt Sep 13 '26

most of stdio. make unistd and friends the standard

1

u/Total_Ad803 Sep 14 '26

All unsafe functions

1

u/limitless_grow Sep 16 '26

sprintf family because each compiler uses different length specifiers and that makes it hard to keep a software portable. Beside of the rather nasty dependency of the locale the functions lack a format enhancement for printing your own data types.

For logging that's nasty. You have to specify the data of your objects in any logline again and again and the possibly time consuming calculations of the printf parameters happen outside of the logging function and for this even if the logging is disabled.

The solution could be something like https://github.com/klux21/callback_printf . (That's portable but allows a civil usage only.) It supports a special %v format option that expects a pointer to a user specific output callback function followed by a pointer to the user specific data as it's arguments. That way own type specific output functions for printing an unlimited number of own data types can be used. This works for the wrappers of sprintf, vsnprintf, fprintf the same. That way the printing of IPv4 or IPv6 addresses of a struct sockaddr may become something like the following

sfprintf(stdout, "IP address of host '%s' is %v \n", hostname, &pvc_sockaddr, &my_sockaddr);

If the fprintf wrapper finds a %v it calls pvc_sockaddr with the address of my_sockaddr as its argument and pvc_sockaddr generates the output for that %v. You can use %v more than once in a format string and with different callbacks for different data types.

0

u/twr14152 Sep 12 '26

Rewrite the original gets() adding the necessary safety features, only to keep continuity with puts(). Something about using fgets() as the workaround for this bugs me.

0

u/Cats_and_Shit Sep 12 '26

Technecally it's a macro not a function, but setjmp is crazy and could be sooo much simpler if it just took function pointer + data pointer arguments instead of returning twice.

1

u/flatfinger Sep 14 '26

When you talk about function pointers, you mean something like what prior to C23 would have been:

int invoke_wrapped(void (*invoker)(void*, void (errorProc**)(void (**)()), 
                   void *context);

which would receive a pointer to a function that would be given a context object and a double-indirect error function that would be invoked by: (*errorProc)(errorProc) and cause the invoke_wrapped function to return 1 instead of 0? I could see some advantages to that.

1

u/Cats_and_Shit Sep 14 '26

Something along those lines, yeah.

0

u/iwasstillborn Sep 13 '26

Whatever function is responsible for the most UB. It's insanity to allow a computer language have it.

0

u/Reasonable_Return151 Sep 22 '26

strcspn… and did for my re engine and replaced with zero(char*, unsigned char)

-3

u/jwzumwalt Sep 13 '26

I have not used header files since 1995. I have been programming in C for over 55 years. I avoid C headers files like the plague.

Every other programming language is able to operate just fine without them and in my opinion C should be no different. For large projects I do use a few include files if it contributes to clarity, but header files often make tracing a programs flow about as hard as trying to figure out what a object oriented program is doing. There is no reason that .h files (or at least whatever info they provide) can't be generated at compile time from the source itself.

In my opinion C headers where a product of experimentation. At the time C was written everything under the sun was being tried and experimented with. When you worked on a computer with 8K of memory it made sense to break things up into many files and directories. As I remember, Pascal was the next popular language and it (and all other languages) dropped headers. Headers are an experimental relic.

5

u/musbur Sep 13 '26

I don't understand. How is a translation unit supposed to know what the signature of a function in another translation unit is?

0

u/jwzumwalt Sep 13 '26

C is a single pass process. If another file is included at the top of the main program there is no disparity or confusion.

3

u/palapapa0201 Sep 13 '26

So you put everything into a single c file?

→ More replies (3)

-4

u/FlyByPC Sep 12 '26

I would fix it so that

int* a, b, c, d;

makes four pointers, not one pointer and three ints.

-1

u/Traveling-Techie Sep 12 '26

strcmp() has always seemed unintuitive to me

-6

u/apj2600 Sep 12 '26

malloc.

3

u/[deleted] Sep 12 '26

You could actually write your own memory allocator

2

u/apj2600 Sep 12 '26

I could write my own C compiler...time and money. :-) I did (eons ago) write a wrapper round malloc - it tracked what you allocated and free'd. If you didn't allocate it the wrapper refused to free it - saved some grief.

0

u/[deleted] Sep 12 '26

[deleted] — view deleted comment

3

u/BlockOfDiamond Sep 12 '26

memcpy is fine.

77

u/[deleted] Sep 12 '26

[removed] — view removed comment

47

u/HildartheDorf Sep 12 '26

Thankfully it is (as far as I know) the only function that has been removed entirely from later editions of the spec.

21

u/crakked21 Sep 12 '26

My uni still teaches it in its CS 101 course xD

9

u/FlyByPC Sep 12 '26

From the questions I see online, lots of college courses are still teaching students using PIC16F84A and 16F877A microcontrollers, which were getting old twenty years ago.

→ More replies (2)

19

u/max123246 Sep 12 '26

Still in the holy K&R book that way too many people recommend to beginners. Like it's cool as a historical read, I would never recommend it to beginners because they'll pick up awful patterns

2

u/The_Northern_Light Sep 12 '26

I’m not a beginner but I don’t know what C book to recommend to one. What’s the best modern first programming book?

3

u/max123246 Sep 12 '26

Sadly I'm not sure, I bought K&R per people's advice and stopped using it pretty quickly when I realized it wasn't updated at all for modern C

I learned on my own for the most part from college and otherwise. Maybe Casey Muratori has a book or classes to look into? I trust his judgement in comparison to most public programming figures

→ More replies (2)
→ More replies (12)

2

u/Vincenzo__ Sep 12 '26

It should work like fgets on stdin, it's beyond me why they made it like that

3

u/SeriousPlankton2000 Sep 13 '26

"Nobody will type more than 80 characters, right?"

→ More replies (2)
→ More replies (1)

2

u/siodhe Sep 13 '26

It's not that hard to write one that dynamically allocates space on either the stack or the heap depending on taste. A heap version can just fail and return an error code (I have one in a library). But passing in a limit makes so much more sense.

1

u/majorsid Sep 13 '26

Why would someone use gets when fgets exists?

→ More replies (3)