TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C String Creation

67 pointsby fcambusover 9 years ago

5 comments

jwsover 9 years ago
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&#x27;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…
评论 #11085242 未加载
评论 #11084375 未加载
jhallenworldover 9 years ago
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&quot;Hello, world!\ntype something&gt;&quot;); if (fgetws(buf, 100, stdin)) wprintf(L&quot;You typed &#x27;%ls&#x27;\n&quot;, buf); if (argv[1]) { char *s = argv[1]; &#x2F;* Convert char string to wchar_t string *&#x2F; size_t len = mbsrtowcs(buf, &amp;s, 99, NULL); if (len != (size_t)-1) { buf[len] = 0; wprintf(L&quot;argv[1] is &#x27;%ls&#x27;\n&quot;, buf); } return 0; } </code></pre> It&#x27;s a pain, but the advantage is access to iswalpha() and friends.
评论 #11084361 未加载
评论 #11084200 未加载
评论 #11085830 未加载
hthhover 9 years ago
This is an interesting article, but the &quot;Portability&quot; comments could be a lot more useful: strndup and open_memstream are both &quot;POSIX 2008&quot;, but strndup can be used on OS X while open_memstream cannot.
评论 #11085862 未加载
评论 #11084393 未加载
评论 #11084834 未加载
rumcajzover 9 years ago
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.
评论 #11085808 未加载
kevin_thibedeauover 9 years ago
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.
评论 #11085856 未加载