3 ms·
In well-written C, you don't work with strings the way you do in other HLLs. For example, extracting and copying substrings is something unnecessary, unless you
by bluetomcat 4y ago
In well-written C, you don't work with strings the way you do in other HLLs. For example, extracting and copying substrings is something unnecessary, unless you want to modify the parent string. Otherwise, a substring is represented by a pointer and a size_t length, and can easily be printed that way via the "%.*s" printf specifier:
const char *s = "Hello World!";
const char *world = s + 6;
size_t world_len = 5;
printf("%.*s\n", world_len, world);
- gpderetta 4y agoOn other HLLs it is easy to have subviews on other strings. C makes is needlessly hard by requiring null termination in half the APIs.
- tom_ 4y ago* consumes an int, not a size_t: https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p5 https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p5
- TremendousJudge 4y agoI love how in every C code snippet on every comment on this thread, somebody got something wrong. I take it as a sign that it's probably best to avoid C as much as possible.
- giantrobot 4y ago! multithreaded it's not Hey, at least code
- avar 4y agoAny C compiler that isn't a trivial toy implementation will warn about that, so it's hardly a C gotcha.
- tom_ 4y agoclang and VC++ for x64 warn about this out of the box, but gcc seems to need -Wall.
- lolcatuser 4y agoMaybe my least favorite "feature" of C. I can manage most aspects of zero-terminated strings well enough, but when I have to specify the length of them, is it an 'int', 'size_t', 'ssize_t', or something else? (Answer: All of the above!)