C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
String in cin the previous sessions we learn about float,int,char etc data types, in this session we learn about string. There is no any separate data type for the string, The string is used or string is treat as Array of type char. A character array is string if it ends with the null character(‘\0’). This null character is the escape sequence with the corresponding. ASCII value 0. And it is different from the ASCII character ‘o’. so The string are used to stored or manipulate(change) data in text form like words or sentences. String constant or String literal There are some example of string constants are
The string constant store some where in memory as an Array of characters and terminated or end by a null character(‘\0’). The string constant also itself a pointer of the first character of the Array. Lets us take example to understand for example RED FORT will be stored in memory as— in the above example each character take one byte and at the end the compiler automatically place null character. in the above example the string There is no any separate data type for the string,“RED FORT” actually the first character is the pointer of this string so here R character is the pointer. so whenever a string constant is used in the program it is replace by pointer point to the string if we have any pointer variable of char * type, we can assign the address of this string constant to pointer as
ok let us see to clear concept of string see what the expression “Engineering”[6] represent. let us understand according to the equivalence of pointer, This expression is equivalence to *(“Engineering”+6), as we know that Engineering represent the address of first character ‘E’ then on adding 4 to this address we get the address of character ‘e’ and on perform dereferencing operation we will get the character ‘e’. There is situation where exception is occurs, When the string constant is used for initializing for a character array then this does not represent any address and also it can not store any where in memory. take the example to understand this
in this example the string “Cisbestprogramming” is not stored any where in the memory or complete string “Cisbestprogramming” is not stored anywhere in memory but this declaration statement reserve space in memory for array arr and is equivalent to-
as in the previous if we have statement like this
in this example allocate the pointer variable ptr, The string constant is seprately stored in the memory and initialize pointer variable ptr by starting address of “Cisbestprogramming”. we should understand here some important things such as note that ‘C’ and “C” are different. ‘C’ is character constant which represent the ASCII value of the character ‘C’ but while “C” is the string constant which represent of character C and null character ‘\0’. String variable A string variable is a 1-D array of characters terminated by a null character. when we want to declare string variable we need to declare a character array size to hold all the characters of the string including null character take the example to understand
we can also initialization this as
This initialization is same as the previous one but in this case the compiler automatic add null character at the end. but note here this string constant does not represent any address. This array is represent in memory as In the above example we not fixed or not specify the size of array so this array can hold large enough elements including null character
The output of this program is similar as that of previous program let us understand the concept of above example here p is pointer variable which hold the the base address of array ptr[]. increment this pointer every time by 1 so give the next address of array ptr[] every time. here the loop will terminated when the character ‘\0’ encountered which indicates the end of string we can control string using %s specification also or we can enter and print the string using %s specification with the control string of printf() and scanf()
In the above example the printf() takes the base Address of string and continues display the characters untill the ‘\0’ not encounter. when we enter the string using the %s specification compiler automatic add the ‘\0’ or null character at the end of array. we also don’t need to use & sign in the scanf because the name of the array is address of the array. but in the second run when enter a string with the space we could not get correct result we could get only first string, not get string after space because of scaf() stop reading as soon as it encounters a whitespace. to get correct result or for entering string with the whitespaces we can use function gets(), it is stop reading the string when it get new line or encounters new line and replace this newline with the null character There is another function puts() which display the string and replaces the null character by a newline
|