Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
in Tutorial & Interview questions by Goeduhub's Expert (8.3k points)

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

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-


Course Name



Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon


Natural Language Processing-NLP with Deep Learning in Python Apply Coupon


Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.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.

3.3k questions

7.1k answers


4.6k users


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