

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are string literals in C language?
A string literal is a sequence of chars, terminated by zero. For example,
Char * str = "hi, hello"; /* string literal */
String literals are used to initialize arrays.
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' */
String literals are not modifiable if you try to alter their values, which leads to undefined behavior.
char* s = "welcome"; s[0] = 'W'; /* undefined behaviour */
Always try to denote string literals as such, by using const.
char const* s1 = "welcome"; s1[0] = 'W'; /* compiler error! */
String literals also called as character constants, support different character sets.
/* 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";
- Related Questions & Answers
- What are string literals in C#?
- What are Perl String Literals?
- What are literals in C++?
- What are integer literals in C#?
- What are Boolean Literals in C++?
- What are Character Literals in C++?
- What are string searching functions in C language?
- What are floating point literals in C#?
- What are JSP literals?
- Formatted string literals in C#
- What are literals in Java?
- What is the type of string literals in C/ C++?
- What are Perl Numerical Literals?
- What are Boolean literals in Java?
- What is the type of string literals in C and C++?
Advertisements