Maison  >  Article  >  développement back-end  >  Qu’est-ce qu’une chaîne littérale en langage C ?

Qu’est-ce qu’une chaîne littérale en langage C ?

PHPz
PHPzavant
2023-09-02 12:53:061425parcourir

Qu’est-ce qu’une chaîne littérale en langage C ?

字符串文字是一个以零结尾的字符序列。例如,

Char * str = "hi, hello"; /* string literal */

字符串字面量用于初始化数组。

char a1[] = "xyz"; /* a1 is char[4] holding {'x','y','z','\0'} */
char a2[4] = "xyz"; /* same as a1 */
char a3[3] = "xyz"; /* a1 is char[3] holding {'x,'y','z'}, missing the '\0' */

如果您尝试更改字符串文字的值,则它们是不可修改的,这会导致未定义的行为。

char* s = "welcome";
s[0] = 'W'; /* undefined behaviour */

始终尝试将字符串文字标记为常量,使用const。

char const* s1 = "welcome";
s1[0] = 'W'; /* compiler error! */

字符串文字也称为字符常量,支持不同的字符集。

/* normal string literal, of type char[] */
   char* s1 = "abc";
/* UTF-8 string literal, of type char[] */
   char* s3 = u8"abc";
/* 16-bit wide string literal, of type char16x[] */
   char16x* s4 = u"abc";
/* 32-bit wide string literal, of type char32x[] */
   char32x* s5 = U"abc";

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer