GOEDUHUB Online Courses || Last Batch Student's Projects || COVID-19 Projects(AI-ML) || Universities  ||  Placement Preparation  Subscribe our youtube channel
+91-7976731765 Free Online Tutorials || AI || ML || NLP ||  OpenCV || Python || DBMS || OOPs || DSA || Java || Linux/Unix ||  C Programming
0 like 0 dislike
in Tutorial & Interview questions by Goeduhub's Expert (8.1k points)

In C, a string is a sequence of characters that is terminated by a null character ('\0').

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.1k points)
Best answer
In C, a string is a sequence of characters that is terminated by a null character ('\0').

We can create strings using string literals, which are sequences of characters surrounded by double quotation marks; for example, take the string literal "hello world". String literals are automatically null-terminated.

We can create strings using several methods. For instance, we can declare a char * and initialize it to point to the first character of a string:

char * string = "hello world";

When initializing a char * to a string constant as above, the string itself is usually allocated in read-only data; string is a pointer to the first element of the array, which is the character 'h'.

Since the string literal is allocated in read-only memory, it is non-modifiable1. Any attempt to modify it will lead to undefined behaviour, so it's better to add const to get a compile-time error like this

char const * string = "hello world";

It has similar effect2 as

char const string_arr[] = "hello world";

To create a modifiable string, you can declare a character array and initialize its contents using a string literal, like so:

char modifiable_string[] = "hello world";

This is equivalent to the following:

char modifiable_string[] = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0'};

Since the second version uses brace-enclosed initializer, the string is not automatically null-terminated unless a '\0' character is included explicitly in the character array usually as its last element.

1 Non-modifiable implies that the characters in the string literal can't be modified, but remember that the pointer string can be modified (can point somewhere else or can be incremented or decremented).

2 Both strings have similar effect in a sense that characters of both strings can't be modified. It should be noted that string is a pointer to char and it is a modifiable l-value so it can be incremented or point to some other location while the array string_arr is a non-modifiable l-value, it can't be modified.


About Us | Contact Us  © goeduhub.com Social::   |  | 
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO