Good news in the article, apparently asprintf is scheduled for the next POSIX standard. I wonder how one sees what is in the next POSIX standard, it would inform my choices of what I use. I'll use something that works on all my platforms if it is headed to a standard, but usually not if it may head to oblivion.<p>Also open_memstream is POSIX 2008, now if it would just get into OS X…
Should we not be using wchar_t strings in modern C?<p><pre><code> int main(int argc, char *argv[]) {
wchar_t buf[100];
wprintf(L"Hello, world!\ntype something>");
if (fgetws(buf, 100, stdin))
wprintf(L"You typed '%ls'\n", buf);
if (argv[1]) {
char *s = argv[1];
/* Convert char string to wchar_t string */
size_t len = mbsrtowcs(buf, &s, 99, NULL);
if (len != (size_t)-1) {
buf[len] = 0;
wprintf(L"argv[1] is '%ls'\n", buf);
}
return 0;
}
</code></pre>
It's a pain, but the advantage is access to iswalpha() and friends.
This is an interesting article, but the "Portability" comments could be a lot more useful: strndup and open_memstream are both "POSIX 2008", but strndup can be used on OS X while open_memstream cannot.
This is an advice to use C in a way that resembles higher level languages. But it you want to do so, why not simply use a higher level language?<p>The power of C -- which distinguishes it from most other languages -- is the ability to allocate almost everything statically.<p>In fact, the older I get the more I appreciate pre-Algol60 way to allocating stack frames statically.
I see too much damage caused by strncpy() to ever recommend it for use. Code that is blissfully unaware of the non-guaranteed NUL or that repetitively does extra work to guarantee a NUL. Use strlcpy() if available or reimplement it.