liveBook · Manning

String

As explained in Modern C

A string is a programming-language value representing an ordered sequence of characters or character units as text.

In C, a string is a zero-terminated array of char: the final element has value 0 and is not visible text. A string literal can initialize an array, but arrays cannot be assigned. A char array without a terminating 0 is not a string. The string.h library distinguishes array functions beginning with mem from string functions beginning with str. section 6.1.5

   char jay0[] = "jay";
   char jay1[] = { "jay" };
   char jay2[] = { 'j', 'a', 'y', 0, };
   char jay3[4] = { 'j', 'a', 'y', }; 


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":3},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":3},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":3},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":3},{\"line\":3,\"ch\":7}],[{\"line\":0,\"ch\":3},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":3},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":3},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":3},{\"line\":3,\"ch\":7}],[{\"line\":0,\"ch\":3},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":3},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":3},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":3},{\"line\":3,\"ch\":7}],[{\"line\":0,\"ch\":3},{\"line\":0,\"ch\":7}],[{\"line\":1,\"ch\":3},{\"line\":1,\"ch\":7}],[{\"line\":2,\"ch\":3},{\"line\":2,\"ch\":7}],[{\"line\":3,\"ch\":3},{\"line\":3,\"ch\":7}]]"}
!@%STYLE%@!

Compare several array initializations of the string literal "jay"; each includes room for its terminating 0. section 6.1.5

char jay4[3] = { 'j', 'a', 'y', };
char jay5[3] = "jay";


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}]]"}
!@%STYLE%@!

These three-element arrays hold the visible letters but lack the terminating 0, so they are not strings. section 6.1.5

How Modern C covers it

How deep it goes
Defines zero termination and initialization, then distinguishes array-oriented mem functions from string-oriented str functions.
Examples
Uses several initializations of "jay" to contrast terminated strings with char arrays lacking a terminating 0.
What it assumes
Builds on previously encountered arrays and char, while defining the zero-termination rule for strings.
What you can do after
Apply strlen, memcpy, and strcmp to copy and compare a program-name string while explicitly adding its terminator.
How it connects
Connects strings to arrays, char, ASCII, and standard-library memory and string functions.

Also explained in

Compare how 24 books explain string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.