Paul Gilmartin wrote:
On Tue, 4 Sep 2012 06:22:43 -0700, Charles Mills wrote:
char[] delimits strings with '\0' in every implementation in the world, I
think.
It depends. With gcc:
char a6[ 6 ] = { 'w', 'o', 'm', 'b', 'a', 't' };
char s6[ 6 ] = "wombat";
both compile with no error message, whereas:
char s5[ 5 ] = "wombat";
gives:
wombat.c:6: warning: initializer-string for array of chars is too long
so, apparently the \0 terminator is not mandatory.
-- gil
That is how it is defined the C language. In the first instance
you are defining an array `a6' of 6 character values initialized with
6 individual characters.
In the second instance, you are defining an array of 6 character
values initialized with a string value. It is a convenience in the C
specification that allows for this (because the constant "wombat"
is a 6-character array consisting of ['w', 'o', 'm', 'b', 'a', 't', '\0'].)
In the third instance - the warning is generated because you
are trying to initialize an array of 5 characters from a constant-string
constant containing 6 characters.
Regarding the '\0' terminater... it is not mandatory at all.
The language defines a character string constant (one in double-quotes)
as having it. The runtime library provides functions that depend on it.
That's about the extent of it.. it's not exactly an "inherent" component of
C.
By the way - your example with the Dignus compiler generates:
cc: t.c line 6:Warning #2197: initializer string is too long, truncated
char s5[ 5 ] = "wombat";
indicating that the string constant "wombat" is too long (the trailing
'\0' char)
to fit in the given array. It just happens to be the '\0'-char that is
truncated,
it could be any others that were too long to fit in the target.
- Dave Rivers -
--
riv...@dignus.com Work: (919) 676-0847
Get your mainframe programming tools at http://www.dignus.com
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN