liveBook · Manning

String_literal

As explained in Modern C, Third Edition

string_literal is a book-specific function-like macro that accepts a string literal by requiring adjacent empty string literals in its replacement.

`string_literal("hello")` is accepted, but `string_literal(word)` is rejected when `word` is a character array. The function-like macro places empty string literals on either side of its argument: `"" X ""`. With `"hello"`, the result is a sequence of adjacent string literals. With `word`, the tokens cannot be joined that way. section 17.2 section 5.3

string_literal("hello");  // "" "hello" ""
char word[25] = "hello";
...
string_literal(word);     // "" word ""     // Error

Compare the expansion for a literal argument with the error-producing expansion for a character-array argument. section 17.2

TAKEAWAY: Adjacent string literals are concatenated. The empty literals add no text; they make that rule a constraint on what can appear as the macro argument. A character array initialized from a literal is not itself a string literal. section 5.3 section 17.2

This check is useful at interfaces that should receive fixed text, especially output formats. String literals are read-only, although their type is not `const`-qualified. A function parameter alone cannot express all the desired constraints—character pointer, non-null, immutable, and zero-terminated—so the macro supplies a targeted check rather than a general replacement for those interface guarantees. section 5.6.1 section 17.2

How Modern C, Third Edition covers it

How deep it goes
Works through the `"" X ""` expansion and a rejected character array; stops short of a complete macro definition.
Examples
Contrasts `string_literal("hello")` with `string_literal(word)`, where `word` is a character array initialized to `"hello"`.
What it assumes
Builds on C string literals, character arrays, and function-like macro expansion.
What you can do after
Evaluate/understand why empty adjacent literals reject a character-array argument and why that check helps protect format-string interfaces.
How it connects
Connects string-literal concatenation and function-like macros to character arrays, read-only objects, and format strings.

Related concepts

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