Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Character array initialization
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Chris Torek, post: 2382342"] Back in the 1980s when the X3J11 committee was standardizing C for the first time, this *was* an error and *did* get a diagnostic from all existing (pre-ANSI) C compilers worthy of the name "C compiler" (there were some strange compilers back then :-) ). But then someone on the committee decided it would be nice to have a way to suppress the automatic '\0'-adding for certain special cases. Whoever it was, proposed that if the programmer manually counted up the bytes and put the size in an array definition, and then used a string literal to initialize an array of "char", that it would be OK to have the array be "just one too small" to hold the final '\0', in which case the '\0' would be suppressed. Of course, a MUCH BETTER suggestion was sent in during the review periods -- a new escape sequence, \z, could be used at the end of string literals to suppress the final zero byte, so that one could even write things like x = "\10\2\4\1\z"[i & 3], for instance, to make a four-byte literal array without the unnecessary '\0' at the end -- but it was "Not Invented Here" and rejected. Had \z been accepted, you would have been able to write: char s[] = "123456\z"; and get an array of size 6, without having to manually count -- or perhaps mis-count -- the bytes. Leaving out the \z would get a diagnostic, just as one would expect. The X3J11 committee folks went along with the dumb idea :-) , so now that is what we have. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Character array initialization
Top