Program to Read and Print array in C language

Program-to-Read-and-Print-array-in-C-language

Program Description:

Write a program to Read and Print array in C programming language. The program should accept the integer numbers from the user and store them as the array elements and then print it on the standard output (console)

Example Input and Output:

Input:

Output:

Prerequisites:

As we are going to use the arrays in this program, It is recommended to know the basics of the C Arrays. Please go through the following articles to learn more about Arrays in C

Read and Print array in C Program Explanation:

  1. Start the program by declaring an array with the size of SIZE global constant – int array[SIZE];
    • We created the SIZE global constant and initialized with 10. So the array size is 10. We also use the macro instead of the global constant – const int SIZE = 10;
  2. Read Array: We need to take the input from the user and update the elements of the array. Use a for loop to iterate over the array and prompt the user for input values for each element of the array(  array). Then read the values using scanf and store them in the respective array element.
  3. Print Array: Use for loop to print the elements of the array to the console. The for loop will start from and goes till the SIZE

Program to Read and Print array in C language:

Let’s write the program.

In the above program, We no need to use the size as the extra parameter to the function, As we already have SIZE global constant, We can use it directly.

Program Output:

Let’s compile the program using GCC (Any compiler)

gcc read-print-arr.c

The above command creates an executable file a.out.

Run the program.

read-and-print-array-in-c-program-output

As we can see from the above program, The program is reading the input from the user and printing it back to the console.

Alternative Way: Read and Print the Array in C:

Instead of printing the array element by element, let’s print it in a single line.

Program Output:

Compile and run the program

📢 Related Program: Compile and Run the program in Linux OS.

read-and-print-array-example-2

Read and Print array using functions in C programming language:

Read and Print array using Function Program Explanation:

  1. Create a constant named ARRAY_SIZE with the size of the array.
  2. In the main() function, declare an integer array arr with ARRAY_SIZE elements.
  3. Call the read() function from the main() function and pass the arr and its size as arguments. The read() function will prompt the user for input values for each element of the array and update the array.
    1. The read() function takes an integer pointer arr and its size as formal arguments – void read(int *arr, int size)
    2. It then prompts the user for input values for each element of the array( arr).
    3. The values are read using scanf() function and stored in the corresponding array( arr) element.
  4. Call the print() function and pass the arr and its size as arguments. The print() function will print the elements of the array to
    1. The print function also takes an integer pointer arr and size as formal arguments – void print(int arr[], int size)
    2. Then it uses a for loop to iterate over each element of the arr , it prints the element’s index and its corresponding value.

Read and Print array using Function Program:

📢In the above program, We no need to pass the size as the extra parameter to the function, As we already have SIZE global constant.

Program Output:

Compile and run the program

Related Programs:

  1. C Program to Print First n Fibonacci numbers
  2. C Program to get Nth Fibonacci number
  3. C Program to Check number is Prime Number or not?
  4. C Program to Check Prime Number using Square Root (Sqrt) Method
  5. C Program to Print Prime Numbers between Two numbers
  6. C Program to print First N Prime numbers
  7. C Program to generate Prime numbers up to N
  8. C Program to Calculate Nth Prime Number
  9. C Program Check Prime Number [Mutliple Methods]
  10. C Program to Convert Decimal Number to Binary Number
  11. C Program to Convert Binary Number to Decimal Number
  12. C Program to Convert Octal Number to Decimal Number
  13. C Program to Calculate the Factorial of Number

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Define a read() function to update the array elements with user-provided values. […]

  2. […] C Program to Read and Print Arrays […]

Leave a Reply