-1

In The C Programming Language, Second Edition, 1988, Section A2.6, page 194, Kernighan and Ritchie write:

A string has type “array of characters” and storage class static…

and I do not understand the second information well, can you explain it to me?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Mohammad
  • 65
  • 7
  • 1
    Does this answer your question? [What does "static" mean in C?](https://stackoverflow.com/questions/572547/what-does-static-mean-in-c) – user16217248 Sep 05 '22 at 00:56
  • Caveat: This is likely in relation to "compile time" strings defined in the source code. Code can define null terminated arrays of characters (also called strings) that only exist for the "scope" of the function (or code block) in which they are defined. Then, there are "string buffers" allocated in "heap memory", also not normally considered "static"... There's more to this than meets the eye on first glance... – Fe2O3 Sep 05 '22 at 01:14
  • When referring to a book, even a famous book like Kernighan and Ritchie’s book on C, give a proper bibliographic citation. There are two editions of *The C Programming Language*, so somebody with one might not be able to find a quotation from the other. Give all the information somebody needs to find the passage you cite: Authors, title, date of publication, chapter and section number, and page number. – Eric Postpischil Sep 05 '22 at 02:06
  • The sentence is actually wrong, at least in modern terminology. A string literal in source results results in an array of `char` with static storage duration, but there are other strings in C. You can have a string in an array that is automatically allocated or dynamically allocated; a “string” is any sequence of characters terminated by a null character, regardless of storage class. The sentence should say that a “string literal” creates an array of `char` with static storage duration. – Eric Postpischil Sep 05 '22 at 02:09

2 Answers2

0

It means that the memory allocated the string will remain for as long as the program runs, similar to a global or static variable. This is different from automatic local variables, which are deallocated once the lexical scope ends.

user16217248
  • 3,119
  • 19
  • 19
  • 37
0

The text should say that a string literal has type “array of char” and storage class static.

A string literal is text in C source code starting and ending with ", such as "Hello, world." (There are some embellishments to that form, not discussed in this answer.) A compiler builds the contents of that string literal into your program so that it exists in memory for the entire execution of the program. This is what we mean by static storage duration: It is static (unchanging) throughout the program. In contrast, variables declared inside functions, particularly inside blocks, are given memory only during execution of the block they are in, and objects given memory by malloc have that memory only until free is called (or certain related routines).

Kernighan and Ritchie should not have used “string” in this sentence instead of “string literal.” In C terminology, a string is any sequence of characters terminated by a null character (and not containing any earlier null characters). You can have a string in any memory, whether it is static, thread, automatic, or dynamically allocated.

Also, technically, a string literal is the source code that starts with " and ends with ". The array that results from it is a different thing. However, people often use the term “string literal” loosely to refer to that array.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312