String in C programming-medium

                                    STRINGS


  •        WHAT ARE STRING 

                      The way a group of integers can be stored in an integers array,similarly a group of character can be stored in character array,character arrays are many a time also called string.many languages internally treat strings as character arrays, but some how conceal this fact from the programmer .Character arrays
 or strings are used but programming   languages to manipulate text such as word and sentences.

The string constant is a one-dimensional array of character terminated by a null('/0')for example,

char name[] {'H' , 'A' , 'E' , 'S' , 'E' , 'R' , '\0'} :

Each character n the array occupies one byte of memory and the lats character is always '\0' .What character is this ? . It looks like two character but it is actually only one character , with the \ indicating that what follow it is something special. '\0' is called "null character". Note that '\0' and ' 0 ' are not same. ASCII value ( https://en.wikipedia.org/wiki/ASCII ) of  '0' is 48. Note that the elements of the character array are stored in contiguous memory locations. 

                 The terminating null is important , because t is the only way the function that work with a string can know where the string ends. In fact , a string  not terminated by a '\0' is not  really  a string , but merely a collection of characters. 


 value
 H A\0 
 address65518 65519 65520 65521 65522 65523 65524 65525 

C concedes the fact that you would use strings very often and hence provides a short cut  for initializing strings. For example , the string used above can also be initialized as ,

char name[] = "HAESLER";

Note that , in this declaration '\0'  is not necessary. C inserts the null character automatically. 

While entering the string using scanf() , we must be cautious about two things :
a) The length of the string should not exceed the dimension of the character array. This is the because the C compiler does not perform bounds checking on character arrays. Hence , if you carelessly exceed the bounds, there is always a danger of overwriting something important , and   in that event , you would have nobody to blame but yourselves. 

b) scanf() is not capable of receiving multi-world strings. Therefore , names such as 'Debashish Roy' would be unacceptable . The way to get around this  limitation  is by using the function gets() . The usage of functions gets() and its counterpart puts() is shown below . 

#include<stdo.h>
int main()
{
char name [25] ;
printf("Enter your full name");
gets(name) ;
puts("Hello") ;
puts(name) ;
return 0 ;
}


And here s the output...

Enter your full name : Donald trump
Hello!
Donald trump


The  program and the output are self- explanatory  except for the fact that , puts()  can display only one string at a time (hence the use of to puts() in the program above). Also , on displaying a string , unlike printf() , puts()  places the cursor on the next line. Through gets() is capable of receiving only one string at a time , the plus point with gets() is that it can receive a multi-word string. 

If we are prepared to take the trouble , we can make scanf() accept multi-word strings by writing it in this manner :

char name[25] ;
printf("Enter your full name") ;
scanf("%s", name) ;


Here indicates that scanf() will keep receiving  characters into name[] until a \n is encountered . Through workable, this is not the best of the ways to call a function .
Previous
Next Post »
}; //]]>