Home > Backend Development > C++ > body text

What is a string literal in C language?

PHPz
Release: 2023-09-02 12:53:06
forward
1446 people have browsed it

What is a string literal in C language?

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

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

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

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' */
Copy after login

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

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

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

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

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

/* 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";
Copy after login

The above is the detailed content of What is a string literal in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!