C Programming Tutorial
Computer Tutorial

Strings



What are Strings

A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ). Character arrays or strings are used by programming languages to manipulate text such as words and sentences.
char name[ ] = { 'G', 'E', 'N', 'E', 'R', 'A', 'L', '\0' } ;
Each character in the array occupies one byte of memory and the last character is always ‘\0’. ‘\0’ is called null character.
The terminating null (‘\0’) is important, because it is the only way the functions 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.
The string used above can also be initialized as :
char name[ ] = "GENERAL" ;
In this declaration ‘\0’ is not necessary. C inserts the null character automatically.

Program to demonstrate printing of a string

\* C Program to demonstrate printing of a string *\
# include < stdio.h >
int   main( )
{

char   name[ ] = "Generalnote"  ;
int   i = 0  ;
while (  i < = 10 )
{

for (  j = 0 ; j < c ; i++ )
printf(" %c ",  name[i]) ;
i++ ;

}
return ( 0 ) ;

}

Output :

Generalnote