Total Pageviews

Friday 4 May 2012

C Language Strings


In C Strings are defined as arrays of characters. For example, the following defines a string of 50 characters:    char name[50];

C has no string handling facilities built in and so the following are all illegal:

  char firstname[50],lastname[50],fullname[100];

        firstname= "Arnold"; /* Illegal */
        lastname= "Schwarznegger"; /* Illegal */
        fullname= "Mr"+firstname +lastname; /* Illegal */

However, there is a special library of string handling routines which we will come across later.

To print a string we use printf with a special %s control character:  printf(``%s'',name);

NOTE: We just need to give the name of the string.

In order to allow variable length strings the \0 character is used to indicate the end of a string.

So we if we have a string, char NAME[50]; and we store the ``DAVE'' in it its contents will look like:


D  A  V  E    \0    ………..
 
NAME                     
                               
              0                                                  49

No comments:

Post a Comment