String Function

                 A group of characters defined between double quotation marks is a string. It is a constant string. In C Language, a string variable is nothing but an array of characters and terminated by a null character (\0).

Declaration:

                char    identifier[size];
Eg:    char st[20];

Initialization of string:

                At the time of declaring a string variable, we can store a constant string into that variable is called as initialization.

Syntax:                                       

                char    identifier[size]="string"
Eg:  
                char st1[10]="WELCOME";
                char st2[]="ABCD"; 
                The compiler assigns a constant string to the string variable. It automatically supplied a null character(\0) at the end of the string. Therefore the size should be equal to the maximum number of characters in the string plus(+) 1.

Note:  In C language, string initialization is possible, but string assigning is not possible.

Program:


#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    char st1[10]="WELCOME";
    char st2[]="STREDUTUTS";
    printf("st1 = %s", st1);
    printf("\nst2 = %s", st2);
    getch();
}

Output:

st1 = WELCOME
st2 = STREDUTUTS




No comments:

Post a Comment