What are strings? C

Group of characters is a character array. A one-dimensional array of characters is called a string. The difference between any other type of array and string is that a character array ends with a ‘\0’ character.

E.g. char str_name[] = {‘S’, ‘E’, ‘T’ , ‘\0’};

char array str_name, is initialized as we had previously initialized integer array, but here we have added a ‘\0’ at the end. String can also be initialized as

char str_name = “SET”;

‘\0’ is automatically added in this case.
It is important to know the difference between a character and a string. String is a group of characters. Every element of a string is a character. A string has to end with a ‘\0’. Each character occupies 1 byte in memory and the characters of a string are stored in continuous memory locations. ‘X’ is a character whereas “X” is a string consisting of one character.

It is not necessary to mention the size of the array while initializing, but if you specify the size, you should not count ‘\0’ as character. ‘C’ compiler will take care of it. In our example, size of name is 3 and not 4. ‘\0’ being the terminating character of a string indicates end of string.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics