In C language, single and double quotes have different uses, and are used for character and string representations respectively.
1. Single quotes ('):
Single quotes are used to indicateSingle character, i.e. character constant. Each character constant is aint
A value of type, corresponding to the integer value of the character in ASCII (or other character encoding table).
grammar:
'character'
Example of usage:
char ch = 'A'; // 'A' is a character constant, its ASCII value is 65int value = 'A'; // valueThe value is65
Things to note:
- Only single quotes can be includedOne character, cannot contain multiple characters or empty strings.
- Can contain escaped characters, such as:
\n
、\t
、\r
wait.
Error Example:
// Error: Single quotes cannot contain multiple characterschar ch = 'AB'; // Compilation error
2. Double quotes ("):
Double quotes are used to indicateString, i.e. character array. A string is a null character in C language ('\0'
) ending character array.
grammar:
"String"
Example of usage:
char str[] = "Hello"; // stris a included5Character array of characters,'H', 'e', 'l', 'l', 'o' and '\0' End symbol
Things to note:
- A string can contain multiple characters and will automatically add one at the end.
'\0'
Character (null terminator). - The type of string is
char[]
(character array), but can also be used as a pointer type to a character.
Example:
const char *str = "Hello, World!";
Summary of the difference between single quotes and double quotes:
characteristic | Single quotes' '
|
Double quotes" "
|
---|---|---|
use | Represents a single character constant | Represents a string (character array) |
type |
char Type (actuallyint ) |
char[] orchar* type |
Number of characters allowed | Only one character | Can contain multiple characters |
End symbol | No automatic ending character | Automatically add'\0' End symbol |
Escape characters | Escape characters can be used, such as'\n'
|
Can contain escaped characters, such as"\n"
|
Example comparison:
#include <> int main() { char ch = 'A'; // Character constant char str[] = "Hello, World!"; // String constants printf("ch: %c\n", ch); // Output a single character 'A' printf("str: %s\n", str); // Output string "Hello, World!" return 0; }
Summarize:
-
Single quotes: Used to represent a single character (
char
)。 - Double quotes: Used to represent a string (character array).
This is the introduction to this article about the detailed explanation of the usage of single and double quotes in C language. For more related contents of single and double quotes in C language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!