Include double-quote (") in C-string
Categories:
Mastering Double Quotes in C-Strings

Learn the essential techniques for including double-quote characters within C-style strings, covering escape sequences, raw string literals, and practical examples.
Working with strings in C often involves handling special characters. One common challenge is embedding a double-quote character ("
) directly within a string literal, as the double quote itself serves to delimit the string. This article explores various methods to correctly include double quotes in your C-strings, ensuring your code compiles and behaves as expected.
The Backslash Escape Sequence
The most fundamental and widely used method to include a double quote within a C-string is by using the backslash escape sequence. The backslash (\
) character signals to the compiler that the character immediately following it should be interpreted specially, rather than literally. For a double quote, you use \"
.
#include <stdio.h>
int main() {
char str1[] = "This is a string with a \"quoted\" word.";
char str2[] = "He said, \"Hello, World!\"";
printf("%s\n", str1);
printf("%s\n", str2);
return 0;
}
Using the backslash escape sequence for double quotes.
\\
.Concatenation of String Literals
C compilers automatically concatenate adjacent string literals. This feature can be leveraged to include double quotes without using escape sequences, though it often results in less readable code for simple cases. You can break the string into parts, placing the double quote in its own literal.
#include <stdio.h>
int main() {
char str[] = "This string contains " "\"" " a double quote.";
printf("%s\n", str);
char another_str[] = "Another way to " "\"" "embed quotes.";
printf("%s\n", another_str);
return 0;
}
Concatenating string literals to include a double quote.
\"
is generally preferred for clarity.Illustrating String Processing with Double Quotes
Understanding how the compiler processes string literals with escape sequences is crucial. The following diagram illustrates the transformation from source code to the in-memory representation of a string containing double quotes.
flowchart TD A["Source Code: char str[] = \"Hello \\"World\\"\";"] B["Compiler Preprocessing (Escape Sequence Resolution)"] C["In-Memory Representation: 'H' 'e' 'l' 'l' 'o' ' ' '"' 'W' 'o' 'r' 'l' 'd' '"' '\\0'"] A --> B B --> C
Flowchart illustrating how the C compiler processes escaped double quotes in a string literal.
As shown in the diagram, the compiler interprets \"
as a single double-quote character in the final string. This process is fundamental to how C handles special characters within string literals.
Practical Application: Generating JSON Strings
A common scenario where you need to embed double quotes is when constructing JSON strings manually. JSON syntax requires string values to be enclosed in double quotes, and if those values themselves contain double quotes, they must be escaped.
#include <stdio.h>
#include <string.h>
int main() {
char json_key[] = "item_name";
char json_value[] = "Product with \"special\" features";
// Constructing a simple JSON object string
char json_output[256];
sprintf(json_output, "{\"%s\": \"%s\"}", json_key, json_value);
printf("Generated JSON: %s\n", json_output);
// Expected output: {"item_name": "Product with "special" features"}
return 0;
}
Constructing a JSON string with embedded double quotes.
cJSON
) instead of manual string formatting to avoid common escaping errors and improve robustness.