Friday, 9 June 2023

Strings in C language

What is Strings in C language?

There is no unrelated or separate data type for strings in C. They are use or treated as arrays of type char. A character array is a string or typically character if it ends with a null character (‘\0’). This null character is an run off or escape sequence with ASCII value 0. Strings are generally or normally used to store and manipulate or operate data in text form like words or sentences.

Strings in c, c program, string constant
String in C language image-1

1.1   String Constant or String Literal

A string constant or sustained is a sequence of characters enclosed or surrounded in double quotes. It is sometimes from time to time called a literal. The double quotes or repeat are not a part of the string.

Whenever a string constant or sustained is written anywhere in a program, it is stored somewhere or to some place in memory as an array of characters terminated or end by a null character (‘\0’). The string constant or sustained itself becomes a pointer to the first character in the array.

 Each character live in or occupies one byte and compiler automatically or no direct human control inserts the null character at the end. The string sustained or constant “The Mahal” is actually a pointer to the character ’T’. So whenever a string constant or sustained is used in the program it is replaced by a pointer pointing to the string.

 If we have a pointer variable or changing of type char*, then we can assign or allot the address of this, string constant to it as –

Char *p=”The mahal”;

Similarly when we write –

Printf(“computer stuff kit\n”);

Then actually or literally a pointer to character (char*) is passed to the printf() function.

If identical or similar string constants or sustained are used in a program, they will be stored separately or one by one at different memory locations. For example if a string sustained or constant “India” appears three times in a project or program then it will be stored thrice in memory.

We have studied that a string constant or sustained gives the address of first character in it, but there is an exception or deviation to this rule; when the string constant is used as an initialize or set to the value for a character array then it does not represent or appear for any address and it is not stored anywhere in memory. For example –

 char arr [5]=”Keep”

Here the string sustained or constant “Keep” is not stored in memory and hence does not represent or appear for any address. Note or record that the ‘b’ and “b” are different. ‘b’ is a character constant which represents or appear for the ASCII value of character ‘b’ while “b” is a string sustained or constant which consists of character ‘b’ and null character ‘\0’.


Computer stuff kit tricks of Topics 53.