Delicious Digg Facebook Favorites More Stumbleupon Twitter

Monday 8 October 2012

strings in c-language

                                              STRINGS
 
    
String :-

A string is a collection of characters. Strings are always enlosed in double quotes as "string_constant".
 


  • In C language Strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL '\0' character:
  • It is important to preserve the NULL terminating character as it is how C defines and manages variable length strings. All the C standard library functions require this for successful operation.
  • All the string handling functions are prototyped in: string.h or stdio.h standard header file. So while using any string related function, don't forget to include either stdio.h or string.h. May be your compiler differs so please check before going ahead.
  • If you were to have an array of characters WITHOUT the null character as the last element, you'd have an ordinary character array, rather than a string constant.
  • String constants have double quote marks around them, and can be assigned to char pointers as shown below. Alternatively, you can assign a string constant to a char array - either with no size specified, or you can specify a size, but don't forget to leave a space for the null character!

  • Declaration of Strings :-

         datatype variable name array length=value;

    example :-  datatype is char, converted into string by using the
                       systemcode "%s",

                       char  a[10]={"hello"};
                                    (or)
                       char a[10]={'h','e','l','l','o'};
                                    (or)
                       char a[10]={{'h'},{'e'},{'l'},{'l'},{'o'}};
                    
                       in strings space( ) , .  is also counted.


     

    0 comments:

    Post a Comment