一直使用单引号和双引号,在Python中好像没有很强的使用限定,但是看到C++中两者的使用有些不同。
参考:
Single quotes vs. double quotes in C or C++
In C and in C++ single quotes identify a single character, while double quotes create a string literal. ‘a’ is a single a character literal, while “a” is a string literal containing an ‘a’ and a null terminator (that is a 2 char array).
In C++ the type of a character literal is char, but note that in C, the type of a character literal is int, that is sizeof ‘a’ is 4 in an architecture where ints are 32bit (and CHAR_BIT is 8), while sizeof(char) is 1 everywhere.
意思就是:
1)单引号:单个字符,双引号:一个字符串常量;
2)双引号表示的字符串常量包含结束字符'\0'
;
例如,"a"包含 a 和 \0 两个字符。
3)C++中单个字符是char型,1个字节
C中单个字符是int型,4个字节(32位)。
【注】:
1)C/C++中每个字符串都是以字符\0
作为结尾,每个字符串都有一个额外字符的开销,需要注意字符串的越界情况。
2)指针赋值给相同的常量字符串时,实际指向相同的内存地址;
但常量内存初始化数组,不是指向相同的地址。