Thursday, July 29, 2010

character type array in C programming language


Like array of numeric types, it can be of character type in C Language. char type variable hold only one character at a time. But in some situations we have store a sequence of characters in a single variable (e.g. your name is a sequence of characters) and normal char type variable cannot serve the purpose. A sequence of characters is known as string. Here we have to declare a variable of char type.  C syntax of declaring is same as integer or float type array variable. So a character type array variable can be declared as char ch[20] in C Language, where char is the type, ch is the variable name with size 20. In case of character type array with size 20, the array variable can hold maximum 19 characters as at the end of the characters of a character type array there should be a NULL character, which is also known as string terminator. A special character can also denote string terminator ‘\0’.Whenever the char type array is initialized with a string then the NULL character is automatically set in the array but when the array is initialized character by character then the NULL character should be set by the user at the end of the characters.

Program on character type array in C Language.

   #include<stdio.h>
   void main()
{
char ch[]="This is string";
clrscr();
printf("The string is :-%s",ch);
getch();
}

Another program on character type array.

 #include<stdio.h>
 void main()
{
char ch[20];
clrscr();
printf("Enter a string:-");
scanf("%s",ch);
printf("The string is :-%s",ch);
getch();
}  
                                                            
In C Language, if we initialize any character array character wise then NULL character must be set at the end.

#include<stdio.h>

void main()
{
char ch1[]={'p','r','o','\0'};
clrscr();
printf("%s",ch1);
getch();
 }

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner