FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
731 views
in Tutorial & Interview questions by Goeduhub's Expert (9.3k 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 (9.3k 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.

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

 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

...