Double quotation marks ("") are employed in the C programming language to delimit a string literal. The presence of double quotes signals the compiler that the string encompasses all characters within them.
As an example, the string "Hello world" in the code snippet provided is:
#include <stdio.h>
int main() {
// Print the string
printf("Hello world");
return 0;
}
Output:
Hello world
Explanation:
In this instance, the compiler may encounter confusion when attempting to display double quotation marks ("") within a text, such as in the phrases "Hello world" or "Hello world". It loses track of the rest of the string as it interprets the double quotation as the string's conclusion, leading to a compilation error.
As an example, the subsequent program will not compile due to the compiler's inability to find the end of the string.
Example:
#include <stdio.h>
int main() {
// Try to print the double quote(") directly
printf("Hello" world"); // Will throw error
return 0;
}
Output:
[Error] expected ')' before 'world'
What is the solution to it?
A backslash (/) needs to precede a double quotation in C programming to display it within a string. By using the backslash (/), the double quotation is interpreted as part of the string and not as its termination.
An escape sequence holds significance within C programming. Special characters or symbols like a newline, tab, or double quotations can be represented by a sequence of characters.
Example:
// C program to print the double quote
#include <stdio.h>
int main() {
// Print string with a double quote
printf("Hello\" world");
return 0;
}
Output:
Hello" world
A string can encompass a variety of escape sequences.
Example:
In this instance, we require two escape characters to display "Hello, world" in the C programming language:
// C program to print the double quotes
#include <stdio.h>
int main() {
// Print string with double quotes
printf("\"Hello\" world");
return 0;
}
Output:
"Hello" world
Escape sequences list:
The complete set of escape sequences commonly utilized in our C programs is presented here:
| Escape Sequence | character Represented |
|---|---|
a |
Alert |
b |
Backspace |
f |
New Page |
n |
New Line |
r |
Carriage Return |
t |
Horizontal Tab |
v |
Vertical Tab |
| \' | Single Quotation Mark |
| \" | Double Quotation Mark |