How To Print Char Array In C

Print Char Array in C

We will learn how to print char array in C using for loops and built-in methods. Each way has one relatively straightforward, and you can choose whichever you want. Let’s read this article now.

How to print char array in C?

Use the for loop

C programming language is one of the most basic languages ​​to become a developer. Printing char array in C is a simple and common problem, and the fastest way to do it is to use a for loop to iterate over the array.

Syntax:

for (start; proviso; increment) {
   statement(s);
}

Parameters:

  • start: Starting value.
  • proviso: Condition to repeat.
  • increment: Value increments after each iteration.

To better understand how to do this, see the following example:

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

int main(void) {
   // Init two arrays
   char learnshareit[] = { 'l','e','a','r','n','s','h','a','r','e','I','T'};
   char welcome[] = { 'w','e','l','c','o','m','e'};

   // Print the first array with for loop
   for (int i = 0; i < 12; i++) {    	
       printf("%c", learnshareit[i]);        
   }

   printf("\n");
    
   // Print the second array with for loop
   for (int i = 0; i < 7; i++) {    	
       printf("%c", welcome[i]);        
   }
}

First, we declare two arrays “study” and “hello”. Then we proceed to traverse the array using a for loop. The starting value is i = 0. So, the first value of the array will be printed. The loop condition is still satisfied. After each iteration, the variable i increases by one unit. Like that, the array will be printed gradually. For the second for loop, it’s the same.

Output:

learnshareIT
welcome

This is a straightforward and effective method. However, if we use this method when we need to traverse many arrays, it will consume memory and not perform well. So, I will show a shorter and less memory-intensive method below.

Use %s Specifier with printf

%s Specifier in C is a format that describes that the variable to be printed is a string value. When combining this format with the printf function to print an array, just calling the array name will print all the values ​​in that array.

Syntax:

printf("%s\n", arr);

Parameters:

  • arr: is an array to traverse and print the value.

Example:

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

int main(void) {
   // Init two arrays
    char apple[] = { 'a', 'p', 'p', 'l', 'e'};
    char horse[] = { 'h', 'o', 'r', 's', 'e'};

   // Show the result
    printf("%s\n", apple);
    printf("%s\n", horse);
}

Here I have two arrays, “apple” and “horse”. I want to print these arrays with the above method. I need to reformat the temporary data type and print it as a usual string like the example above.

Output:

apple
horse

Summary

Print Char Array in C is a straightforward problem that every programmer can do. I have shown you two simple ways to do it in this article. If you have any questions, please comment below. I will help you answer them. Thank you for reading!

Maybe you are interested:

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *