FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
255 views
in Tutorial & Interview questions by Goeduhub's Expert (9.3k points)

A string literal in C is a sequence of chars, terminated by a literal zero.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (9.3k points)
 
Best answer

A string literal in C is a sequence of chars, terminated by a literal zero.

char* str = "hello, world"; /* string literal */ 

/* string literals can be used to initialize arrays */

char a1[] = "abc"; /* a1 is char[4] holding {'a','b','c','\0'} */

char a2[4] = "abc"; /* same as a1 */

char a3[3] = "abc"; /* a1 is char[3] holding {'a','b','c'}, missing the '\0' */

String literals are not modifiable (and in fact may be placed in read-only memory such as .rodata). Attempting to alter their values results in undefined behaviour.

char* s = "foobar";

s[0] = 'F'; /* undefined behaviour */ 

/* it's good practice to denote string literals as such, by using `const` */

char const* s1 = "foobar";

s1[0] = 'F'; /* compiler error! */

Multiple string literals are concatenated at compile time, which means you can write construct like these. 

Version < C99

/* only two narrow or two wide string literals may be concatenated */

char* s = "Hello, " "World";

Version ≥ C99

/* since C99, more than two can be concatenated */

/* concatenation is implementation defined */

char* s1 = "Hello" ", " "World"; 

/* common usages are concatenations of format strings */

char* fmt = "%" PRId16; /* PRId16 macro since C99 */

String literals, same as character constants, support different character sets.

/* normal string literal, of type char[] */

char* s1 = "abc"; 

/* wide character string literal, of type wchar_t[] */

wchar_t* s2 = L"abc";

Version ≥ C11

/* UTF-8 string literal, of type char[] */

char* s3 = u8"abc"; 

/* 16-bit wide string literal, of type char16_t[] */

char16_t* s4 = u"abc"; 

/* 32-bit wide string literal, of type char32_t[] */

char32_t* s5 = U"abc";

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
1 answer 319 views
0 like 0 dislike
1 answer 471 views
0 like 0 dislike
1 answer 287 views
0 like 0 dislike
1 answer 280 views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...