Table of Contents
Char Array in C
Char array in C is used for the string type data. Char array in c means character array in c programing. In previous tutorial we have learned some basic concepts of array. In this tutorial we will mainly learn about the character array.
Questions based on string and character array are generally asked in GATE(CS/IT) and UGC NET exam every year. This tutorial will be useful for the computer science students.
Let’s start with introduction of char array.
What is Char Array in C ?
When we talk about character arrays in c, we basically talk about strings.
Strings are a group of characters and names, sentences etc. are strings.
We use character arrays to store strings and then we can apply multiple operations on strings like modifying a string, copying a string, concatenation of a string to another, finding properties of strings like length of the string etc.
To understand the concepts of Char Array in C students are suggested to read the Array Tutorial ( if you dont have knowledge about array )
Use of Char Array in C
Since C Programming Language does not has Data Types. So Char array in C programming is used to store and manipulate the strings.
To be able to store strings in character arrays, it should fulfill some requirements.
The character array should be large enough to accommodate the string. i.e. If we have a string “Rahul” then to store the string we need a character array of size 6. To understand why we need the extra space in the character let us visualize a character array.
For example
Consider the array as shown in following figure. In the array C of size 8 below, if we try to store the string “Rahul”, we’ll have-
Here, C[0]=’R’, C[1]=’a’, C[2]=’h’, C[3]=’u’, C[4]=’l’. But how will we know that character ‘l’ is the last character in the string?
The string ends at index 4 and we have to store this information in the character array. Hence the next position of the end of the string (here at 5th position) we add a null character which is ‘\0’ which has ASCII value 0. Hence,C[5]=’\0’.
All the functions for string manipulation in c language, expect strings to be null terminated. To store this extra null character, we need this extra space.
Importance of Char Array
- Character Array is used to display the sequence of characters or numbers.
- Using char array we can store the variable in a memory to corresponding memory address.
- Various application such as Word processors, text editors, dictionaries all use strings or character arrays.
- Char array is also used in Pattern Matching like DNA matching and genome matching.
- Char array is also used in Language processing software such as Text-to-speech software.
- Modern databases like mongoDB and JSON also used Char Array for data manipulation.
Program for Char Array in C
Here in this section we will two program in C for the use of Char Array in C.
In first program we will just see how to use char array in c to print a string.
In Second program we will see the use of char array in c for string manipulation.
Program 1 – Display a given string through use of char array in C Programming.
#include <stdio.h>
int main()
{
char C[8];
C[0]=’R’;
C[1]=’a’;
C[2]=’h’;
C[3]=’u’;
C[4]=’l’;
printf(“%s”,C);
}
Output
Rahul
Program 2 – Program in C to Calculate the Length and size of a given string.
#include <stdio.h>#include<string.h>
int main()
{
char C[]=”Rahul”;
int len_of_c=strlen(C);
printf(“Length of C=%d\n”,len_of_c);
// gives the length of the string stored
printf(“Size in bytes of C =%d\n”,sizeof(C));
//gives the size of the character array which also stores the null character
}
Output
Length of C=5
Size in bytes of C =6
Conclusion and Summary
Char array in C plays an important role in string manipulation. I hope this tutorial will be beneficial for computer science students.
After reading this tutorial students will be able to write programs using char array in c for different string manipulation.
If you have any query regarding char array in C then you may ask in comment section.