C Special Characters
Strings - Special Characters
C programming utilizes a variety of special characters that serve specific purposes beyond simply representing alphanumeric characters. These characters hold distinctive meanings and functionalities within the language.
Example
char txt[] = "We are the so-called "Vikings" from the north.";
The backslash (\) acts as an escape character in many programming languages, including Python, C, Java, and more. It has the special ability to alter the meaning of the character that follows it within string literals.
Example
Escape character | Result | Description |
---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
The sequence " (backslash followed by a double quote) is indeed an escape sequence used in many programming languages, including C, to insert a double quote character within a string literal :
Example
#include <stdio.h>
int main() {
char txt[] = "We are the so-called \"Vikings\" from the north.";
printf("%s", txt);
return 0;
}
Output
We are the so-called "CodeLines" from the north.
A single quote in a string" is absolutely correct! In C programming, the escape sequence \' is used to represent a single quote (') within a string literal:
Example
#include <stdio.h>
int main() {
char txt[] = "It\'s alright.";
printf("%s", txt);
return 0;
}
Output
It's alright.
The sequence \\ inserts a single backslash in a string:
Example
#include <stdio.h>
int main() {
char txt[] = "The character \\ is called backslash.";
printf("%s", txt);
return 0;
}
Output
The character \ is called backslash.
Other popular escape characters in C are: