How to print char array in c
An array of characters is a string in C as long as it ends in a null. If not, loop through the number of characters and print them one by one or copy them to a null-terminated character array.
The procedure used to print a char array in C is as follows:
- Create a character array.
- Put the characters you wish to print in the char array's first value.
- The char array can be printed by using the printf() function.
The for loop function is demonstrated in the following example, where we iterate over the array of six letters exactly six times.
The syntax for the printf() function is:
printf("%s", <char_array_name>);
Overview
- Use the For Loop and while loop to print char Array in C
- Use printf with %s specifier to print char Array in C
Thus, there are two ways to print the array:
- Using For loop
- Using while loop
Method 1: Usefor loop to print a Char array in C
If we wish to structure the output more precisely and display each element of the array separately, the for loop is the most apparent way to do so. We must have prior knowledge of the array length in order to use this method. The collection of elements contained in brackets is called an array. Because arrays require loops to print them, they are confusing for novices to understand.
We'll talk about how to use printf to output the character array in C in this article.
Though we can use alternative iteration techniques, such as the while loop, we must first determine the value at which the iteration should end in order to prevent it from going out of bounds and generating a segmentation fault. The printf command and a for loop are used to print the first array. The printf command uses %c to indicate that a character is necessary for printing; if %d is added in place of %c, the internal numeric representation of the characters inside the array will be displayed:
Code:
#include <stdio.h> #include <stdlib.h> #define STR(num) #num int main(void) { char arr1[] = { 'f', 'g', 'h', 'i', 'j' }; printf(STR(arr1)": "); for (int i = 0; i < 5; ++i) { printf("%c, ", arr1[i]); } printf("\b\b\n"); exit(EXIT_SUCCESS); }
Output:
Method 2: Use a While Loop to Print a Char Array in C
Instead of using a for loop, let's use a while() loop to output a similar character array. Next, each element will be printed one at a time using the printf command. Use the code described below for that:
#include <stdio.h> #include <stdlib.h> int main(void) { int i=0, j =0; char arr1[] = { 'H','E','L','L','O'}; char arr2[] = { 'D','A','D','D','Y'}; while (i < 5) { printf("%c", arr1[i]); i++; } printf("\n"); while ( j < 5) { printf("%c", arr2[j]); j++; } }
Output:
Method 3: Use printf with %s specifier to print char array in C
An effective function for formatted output is printf. It can work with both process variables and input variables that have type specifiers.
Internally, the char array is structurally identical to the C-style string; the only difference is that the characters in the C-style string always terminate with a \0 byte, which indicates the end of the string. A single printf call can print the entire array if we append the null byte to the end of our char array.
A segmentation error may likely occur if the software tries to access memory areas when printf is invoked without specifying the terminating null byte.
#include <stdio.h> #include <stdlib.h> #define STR(num) #num int main(void) { char arr1[] = { 'a', 'v', 'i', 'k', 'f' }; char arr2[] = { '1', '6', 'g', 'h', 'b', '\0' }; printf("%s\n", arr1); printf("%s\n", arr2); exit(EXIT_SUCCESS); }
Output:
As you can see, we shall print more characters until the iteration reaches one null terminator, \0, when we print the arr1 that lacks the null terminator.
Putting the string's character count inside the %s specifier is another way to customize the printf function. Using an integer between the symbols % and s to statically hard-code the string length is one method of accomplishing this; alternatively, the * symbol can be used to accept an additional integer argument from the printf arguments. Keep in mind that the character appears before the number or asterisk in the specifier in both approaches.
#include <stdio.h> #include <stdlib.h> #define STR(num) #num int main(void) { char arr1[] = { 'a', 'v', 'i', 'k', 'f' }; char arr2[] = { '1', '6', 'g', 'h', 'b', '\0' }; printf("%.5s\n", arr1); printf("%.*s\n", (int)sizeof arr1, arr2); exit(EXIT_SUCCESS); }
Output:
Add the necessary headers before using printf to print the char arrays in C. Next, declare the array type as char (character type) inside the main() method. The array can then be printed using the printf function and the for or while loop, which are helpful for printing the array's elements one at a time.