Defining and Initializing Character Arrays
We have discussed integer arrays in the previous tutorials. Now we will be looking at how character arrays can be used to store strings. For example, we initialize a char array string which has 20 elements as follows
char string1[20];
Although array string1 is defined to have 20 elements, actually it can hold up to 19 characters only. In C, a special string-termination character '\0' called null character is added at the end of a string. The null-character is there to indicate where a string end. Therefore when declaring a char array, we should always remember to define it large enough to hold the number of characters and the null character. The following figure illustrates a char array s holding a string "hello". The null character is added as the last character in the string.
Now lets look at how to initialize a char array. For example
char string2[] = "Good Morning";
Each element of the array is initialized to contain one character of the string. Note that we haven't specified the size of the array. We let the compiler assigns the size of the array based on the length of the string. Character arrays also can be initialize as follows.
char string3[20] = "The end";
The first seven elements will contain the specified string and the null character will be added to the seventh element. The rest still can be used because the memory locations are already allocated for string3.
I/O Character Arrays
A string can be inputted into a character array using scanf with conversion specifier %s. For example,
char string1[10]; scanf("%s", string1);
Note that the normally used & is omitted in the statement. This is because the name of the array is already an address which is corresponding to the address of the first element of the array. Function scanf will read characters until a space, tab or newline is encountered. Another important matter that we have to consider is the length of the string inputted. It should be no longer than 9 characters because the last element is reserved for the null character. Therefore it is better to use the conversion specifier 9%s to make sure only 9 characters will be read during the process of input.
A character array is printed using printf with conversion specifier %s. For example,
printf("The string is %s.\n", string1);
Function printf will print the characters in the array until null character is encountered.
An example of using character array.
Enter your name: Haris
Good Morning! Haris
Haris
Explanation:
Next: Two-dimensional Arrays
We have discussed integer arrays in the previous tutorials. Now we will be looking at how character arrays can be used to store strings. For example, we initialize a char array string which has 20 elements as follows
char string1[20];
Although array string1 is defined to have 20 elements, actually it can hold up to 19 characters only. In C, a special string-termination character '\0' called null character is added at the end of a string. The null-character is there to indicate where a string end. Therefore when declaring a char array, we should always remember to define it large enough to hold the number of characters and the null character. The following figure illustrates a char array s holding a string "hello". The null character is added as the last character in the string.
Now lets look at how to initialize a char array. For example
char string2[] = "Good Morning";
Each element of the array is initialized to contain one character of the string. Note that we haven't specified the size of the array. We let the compiler assigns the size of the array based on the length of the string. Character arrays also can be initialize as follows.
char string3[20] = "The end";
The first seven elements will contain the specified string and the null character will be added to the seventh element. The rest still can be used because the memory locations are already allocated for string3.
I/O Character Arrays
A string can be inputted into a character array using scanf with conversion specifier %s. For example,
char string1[10]; scanf("%s", string1);
Note that the normally used & is omitted in the statement. This is because the name of the array is already an address which is corresponding to the address of the first element of the array. Function scanf will read characters until a space, tab or newline is encountered. Another important matter that we have to consider is the length of the string inputted. It should be no longer than 9 characters because the last element is reserved for the null character. Therefore it is better to use the conversion specifier 9%s to make sure only 9 characters will be read during the process of input.
A character array is printed using printf with conversion specifier %s. For example,
printf("The string is %s.\n", string1);
Function printf will print the characters in the array until null character is encountered.
An example of using character array.
#include <stdio.h> int main() { int i; char string1[20]; char string2[] = "Good Morning!"; printf("Enter your name: "); scanf("%s", string1) printf("%s %s\n", string2, string1); /* print char array using for statement */ for(i = 0; string1[i] != '\0'; i++) { printf("%c", string1[i]); } printf("\n"); return 0; }Sample output:
Enter your name: Haris
Good Morning! Haris
Haris
Explanation:
int i; char string1[20]; char string2[] = "Good Morning!";Here we have declared integer variable i and two character arrays namely string1 and string2. string1's size is set to 20 while string2 is initialized to hold a string "Good Morning!".
printf("Enter your name: "); scanf("%s", string1) printf("%s %s\n", string2, string1);A prompt message is printed to ask the user to enter his/her name. The entered name is stored in array string1. Then both arrays' contents are printed using function printf.
for(i = 0; string1[i] != '\0'; i++) { printf("%c", string1[i]); }The for repetition statement prints the content of string1 element-by-element until the null character is encountered. This is achieved by specifying the loop condition to string1[i] != '\0'. Note that conversion specifier %c is used since we are printing a character in each iteration.
Next: Two-dimensional Arrays
4 comments:
Very Useful information that i have found. don't stop sharing and Please keep updating us..... Thanks
Thank your valuable content.we are very thankful to you.one of the recommended blog.which is very useful to new learners and professionals.content is very useful for hadoop learners
Best ASP.NET MVC Online Training Institute
Best Spring Online Training Institute
Best Devops Online Training Institute
Best Datascience Online Training Institute
Best Oracle Online Training Institute
Best AWS Online Training Institute
Best AngularJS Online Training Institute
Best C-language Online Training Institute
blog commenting : Thanks for sharing this information. I really Like Very Much.
angular js online training
Post a Comment