Declaration and Initialisation of Strings

Strings in C are group of characters ,digits and symbols enclosed in quotation marks .The end of the string is marked with a special character,the'\0'(null),which has the decimal value 0.There is a difference between a character stored in memory and a single character string stored in a memory.The character requires only one byte whereas the single character string requires two bytes (one byte for the character aand other byte for the delimiter).

Declaration of Strings

To declare a string,specify the data type as char and place the  number of characters in an array in square brackets after the string name.Syntax is as follows:

Char string-name[size];
for example -
char city[24];
char studname[50];

Initialization of strings

String can be initialized as follows:

String can be initialized as follows:
char name[8]={'P','R','O','G','R','A','M','\0'};

Each character of strings occupies 1 byte of memory(on 16 bit computing) .The size of character is machine dependent,and varies from 16 bit computers to 64 bit computers .The character of strings are stored in the contiguous(adjacent) memory location.

1 Byte 1 Byte 1 Byte 1 Byte 1 Byte 1 Byte 1 Byte 1 Byte
P R O G R A M \0
2001 2002 2003 2004 2005 2006 2007 \2008

The C compiler inserts the null (\0) character automatically at the end of the string.So initialization of the Null character is not essential.

You can set the initial value of a character array when you declare it by specifying a string literal .If the array is too small for the literal ,the literal will be truncated .If the literal  is smaller than the array ,then the final characters in the array will be undefined .If you don't specify the size of  the array,but do specify a literal,then C will set the array to the size of the literal ,including the null terminator.

char str[4]={'u','n','i','x'};
char str[5]={'u','n','i','x','\0'};
char str[3];
char str[]="UNIX";
char str[4]="unix";
char str[9]="unix";

All of the above declarations are legal.The first is valid declaration but it will cause major major problem because null is not-terminated.The fifth example suffers the size problem and fourth example is legal because the compiler will determine the length of string automatically and initialize the  last character to null-terminator .

String Constants

String constants have double quote marks around him them,and can be assigned to char pointers.Alternatively,you can assign a string constant to char array -either with no size specified ,or you can specify a size ,but don't forget to leave a for the null character !Suppose you create the following two code fragments and  run them:
/*fragment 1*/
 {
char *s;
s="hello" ;
printf("%s\n",s);
 }

/*fragment 2*/
{
 char s[100];
strcpy(s,"hello");
printf("%s\n",s);
}

These two fragments produce the same output ,but their internal behavior is different.In fragment 2 you can say s="hello"; . To understand the differences,you have to understand how the string constant table work in C.When your program is compiled ,the compiler forms the object code file ,which contain your machine code and a table of ll the string constant declared in a program.In fragment 1 ,the statement s="hello"; causes s to  point to the address of the string hello in the string constant table .Since this string is in the string constant table ,and therefore technically a part of executable code,you cannot modify it .You can only point to it and use it in a read-only manner.In fragment 2,the string hello also exists in the constant table ,so you can copy it into the array of characters  named s .Since s is not an address,the statement s="hello"; will not work n fragment 2.It will not compile.


EXAMPLE

Write  a program to read a name from the keyboard and display message Hello onto the monitor.


#include<stdio.h>
main()
{
char name[10];
printf("\n Enter your name:");
Scanf("%s",name);
printf("Hello %s\n",name);
}

OUTPUT
Enter your name : Pankaj
Hello Pankaj

0 comments:

Post a Comment