How to Print an Array in C

How to Print an Array in C | To print an array we need to use loops. The loop can be either for loop, while loop, or do-while loop. Let us see the C program to print an Array.

C Program to Print an Array using for loop

#include <stdio.h>
int main()
{
  int arr[] = {10, 20, 30, 40, 50};
  
  // display array
  for(int i=0; i<5; i++) {
     printf("%d ", arr[i]);
  }

  return 0;
}

Output:- 

10 20 30 40 50

In this program, since the array contains 5 elements, therefore, we write i<5. Instead of hardcoding the values, you can also calculate the length of the array. See:- How to find the length of the array in C

The for loop in C language is a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed. If the condition is true then the statements of the for loop executes. Looping continues as long as the condition is true. When the condition becomes false then the execution of for loop is terminated.

C Program to Print an Array using while loop

#include <stdio.h>
int main()
{
  int arr[] = {10, 20, 30, 40, 50};
  
  // display array
  int i = 0;
  while(i<5) {
     printf("%d ", arr[i]);
     i++;
  }

  return 0;
}

Output:-

10 20 30 40 50

While loop in C is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated. The statements of while loop may be a single statement or multiple statements terminated by a semicolon. To use the while statement, the test expression should contain a loop control variable. The initialization of the loop control variable has to be done before the loop starts and updating must be included in the body of the loop.

C Program to Print an Array using do-while loop

#include <stdio.h>
int main()
{
  int arr[] = {10, 20, 30, 40, 50};
  
  // display array
  printf("Using do-while loop: \n");
  int i = 0;
  do {
     printf("%d ", arr[i]);
     i++;
  } while(i<5);

  return 0;
}

Output:-

10 20 30 40 50

The do-while loop in C is very closely related to the while loop. The do keyword is placed on a line of code at the top of the loop. A block of statements follows it with a test expression after the keyword while, at the bottom of the loop. It is a post-test loop. The post-test loop executes at least, only then control expression is tested. If the expression is true, the loop repeats. When the expression is false then the loop terminates.

Among for loop, while loop and do-while loop the for loop is the best way to print an array in C. Therefore it is recommended to use for loop instead of while or do-while loop.

In all the above programs, we were declaring and initializing the array at the same time. Now, let us see an example to take array input from the end-user and display it to the console.

#include <stdio.h>
int main()
{
  // Array of 5 floating-point numbers
  float arr[5];

  //  take 5 number
  printf("Enter array elements:\n");
  for(int i=0;i<5;i++)
  {
    scanf("%f", &arr[i]);
  }
  
  // display array
  printf("The Array elements are:\n");
  for(int i=0; i<5; i++) {
     printf("%f ", arr[i]);
  }

  return 0;
}

Output:-

Enter array elements:
12.5 45.9 84.84 554.65 15.15
The Array elements are:
12.500000 45.900002 84.839996 554.650024 15.150000

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “How to Print an Array in C”

Leave a Comment

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