[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C Strings and String Literals. (Was: Pascal rides again)
From: |
Larry McVoy |
Subject: |
Re: C Strings and String Literals. (Was: Pascal rides again) |
Date: |
Sun, 13 Nov 2022 12:45:19 -0800 |
User-agent: |
Mutt/1.5.24 (2015-08-30) |
None of this surprises me but it reminds me of how I described C to
my older son who is learning computer science.
C is sort of like driving a sports car on a narrow mountain road with
no guard rails. If you like to text while you drive, C is not for you.
If you can't keep your eyes on the road, C is not for you. If you are
a bad driver (programmer, duh), C is not for you. But if you are an
expert driver, like to go fast, pay attention, C is just fine, great
in fact.
Source: been a C programmer for more than 40 years, have written
commercially used products over and over again in C. I love C, I get
that it is not for everyone but it is an excellent language in the hands
of expert programmers.
On Sun, Nov 13, 2022 at 08:08:58PM +0000, Ralph Corderoy wrote:
> Hi Branden,
>
> > C doesn't _really_ have strings, except at the library level.
> > It has character arrays and one grain of syntactic sugar for encoding
> > "string literals", which should not have been called that because
> > whether they get the null terminator is context-dependent.
> >
> > char a[5] = "fooba";
> > char *b = "bazqux";
> >
> > I see some Internet sources claim that C is absolutely reliable about
> > null-terminating such literals, but I can't agree. The assignment to
> > `b` above adds a null terminator, and the one to `a` does not. This
> > is the opposite of absolute reliability. Since I foresee someone
> > calling me a liar for saying that, I'll grant that if you carry a long
> > enough list of exceptional cases for the syntax in your head, both are
> > predictable. But it's simply a land mine for the everyday programmer.
>
> - C defines both string literals and strings at the language level,
> e.g. main()'s argv[] is defined to contain strings.
>
> - In C, "foo" is a string literal. That is the correct name as it is
> not a C string because a string literal may contain explicit NUL bytes
> within it which a string may not: "foo\0bar".
>
> - A string literal has an implicit NUL added at its end thus "foo" fills
> four bytes.
>
> - A character array may be initialised by a string literal. Successive
> elements of the array are set to the string literal's characters,
> including the implicit NUL if there is room.
>
> char two[2] = "foo"; // 'f' 'o'
> char three[3] = "foo"; // 'f' 'o' 'o'
> char four[4] = "foo"; // 'f' 'o' 'o' '\0'
> char five[5] = "foo"; // 'f' 'o' 'o' '\0' '\0'
> char implicit[] = "foo"; // 'f' 'o' 'o' '\0'
>
> That's it.
>
> - The string literal is reliably terminating by a NUL.
> - It is not context dependent whether a string literal has a terminating
> NUL.
> - It is absolutely reliable and clearly stated in the C standard and in
> any other C reference worth its salt.
> - There is no need to ???carry a long enough list of exceptional cases for
> the syntax in your head???.
> - An ???everyday C programmer??? will know this simple behaviour by dint of
> being a C programmer who writes it every day; there is no landmine
> upon which to step. :-)
>
> Hope that helps clear up this corner of C.
>
> --
> Cheers, Ralph.
--
---
Larry McVoy Retired to fishing http://www.mcvoy.com/lm/boat
- Re: Specifying dependencies more clearly, (continued)
- Pascal rides again (was: Specifying dependencies more clearly), G. Branden Robinson, 2022/11/10
- Re: Pascal rides again (was: Specifying dependencies more clearly), Alejandro Colomar, 2022/11/10
- Re: Pascal rides again (was: Specifying dependencies more clearly), Alejandro Colomar, 2022/11/10
- Re: Pascal rides again (was: Specifying dependencies more clearly), G. Branden Robinson, 2022/11/10
- Re: Pascal rides again (was: Specifying dependencies more clearly), Dave Kemper, 2022/11/11
- Re: Pascal rides again (was: Specifying dependencies more clearly), Alejandro Colomar, 2022/11/12
- C Strings and String Literals. (Was: Pascal rides again), Ralph Corderoy, 2022/11/13
- Re: C Strings and String Literals. (Was: Pascal rides again),
Larry McVoy <=
- Re: C Strings and String Literals. (Was: Pascal rides again), Alejandro Colomar, 2022/11/13
- Re: C Strings and String Literals. (Was: Pascal rides again), Alejandro Colomar, 2022/11/13
- Re: C Strings and String Literals. (Was: Pascal rides again), Larry McVoy, 2022/11/13
- Re: C Strings and String Literals. (Was: Pascal rides again), Alejandro Colomar, 2022/11/13
- Re: C Strings and String Literals., Ralph Corderoy, 2022/11/14
- Re: C Strings and String Literals., Alejandro Colomar, 2022/11/15
- Re: C Strings and String Literals., Alejandro Colomar, 2022/11/15