C Programming

How to Print a Char Array in C Through printf

A general-purpose programming language C provides low-level access to computing machines. Arrays are the set of elements enclosed inside the brackets. Usually, arrays are a little difficult to understand for beginners as they need to be printed using loops.

In this tutorial, we are going to discuss the method to print the character array in C through printf.

Printing Char Array by Using printf in C

The complete process to print a character type array in C language using printf command is discussed below:

Step 1: To print a character array on C using printf first include the required header files, which are <studio.h> and <stdlib.h>.

#include <stdio.h>
#include <stdlib.h>

Step 2: Then define the main() function. The main() function is a function which contains all of the main code except headers:

int main(void) {


}

Step 3: Then inside the main() function define the array type as “character”. We will use 2 arrays here, but by following the same method, you can define as many arrays as you want:

 char arr1[] = {'w','e','l','c','o','m','e'};
 char arr2[] = {'L','I','N','U','X','H','I','N','T'};

Step 4: Now to print the array there are two methods:

Method 1: Print a Char Array in C Using for Loop

To print the first array using for loop and printf command. The %c in printf command is used to define that the character is required to print, if you add %d instead of %c, it will display the internal numeric representation of the characters inside the array:

  for (int i = 0; i < 7; i++) {
       printf("%c", arr1[i]);
   }

Then print the second array using the same method, but remember that the incrementing variable “i” in the loop is set according to the number of elements in the array. For instance, in this example the arr2[ ] has 9 elements so “i” is starting from 0 and the limit is set i < 9, which means the for loop will repeat 9 times i.e. from 0-8:

 printf("\n");

   for (int i = 0; i < 9; i++) {
       printf("%c", arr2[i]);
   }

Note: The printf(“\n”) is just to move the output pointer to the next line.

The overall code will look like as shown below:

#include <stdio.h>
#include <stdlib.h>
int main(void) {
 
   char arr1[] = { 'w','e','l','c','o','m','e'};
   char arr2[] = { 'L','I','N','U','X','H','I','N','T'};
   
   for (int i = 0; i < 7; i++) {       
       printf("%c", arr1[i]);        
   }

   printf("\n");

   for (int i = 0; i < 9; i++) {       
       printf("%c", arr2[i]);        
   }
}

Then Run the code and the output will print the arrays.

Method 2: Print a Char Array in C Using While Loop

Let’s print the same character array by using a while() loop instead of for loop. Then the printf command will be used to print each element one by one. For that use the below-written code:

#include <stdio.h>
#include <stdlib.h>
int main(void) {
  int i=0, j =0;
   char arr1[] = { 'w','e','l','c','o','m','e'};
   char arr2[] = { 'L','I','N','U','X','H','I','N','T'};
   
   while (i < 7) {     
       printf("%c", arr1[i]);
       i++;      
   }

   printf("\n");

   while ( j < 9) {    
       printf("%c", arr2[j]);        
       j++;
 }
}

Remember that for using while() loop the loop variables need to be initialized before, whereas in for() loop the variable increment and initializing both can be done in the single step.

Output

Conclusion

To print the char arrays using printf in C language, firstly add the required headers. Then inside the main() function define the array type as char (character type). After that to print the array, you can use the for or while loop that are useful in printing the elements of the array one by one through printf.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.