字符串文字是一个以零结尾的字符序列。例如,
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。
立即学习“C语言免费学习笔记(深入)”;
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";
以上就是C语言中的字符串字面值是什么?的详细内容,更多请关注php中文网其它相关文章!
C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号