
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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 Articles
- What are string literals in C#?
- What are Perl String Literals?
- What are literals in C++?
- What are string searching functions in C language?
- What are Boolean Literals in C++?
- What are Character Literals in C++?
- What are integer literals in C#?
- Formatted string literals in C#
- What is the type of string literals in C/ C++?
- What are floating point literals in C#?
- What is the type of string literals in C and C++?
- What happen if we concatenate two string literals in C++?
- What does the @ prefix do on string literals in C#?
- Character constants vs String literals in C#
- What is the difference between character literals and string literals in Java?

Advertisements