code to print out entire array in c
#include <stdio.h>
#define SIZE 5
int main() {
int arr[SIZE] = {1, 2, 3, 4, 5};
int i;
printf("Elements of the array are: ");
for (i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation:
#include <stdio.h>
: This line includes the standard input-output header file in the program, allowing the use of functions likeprintf()
andscanf()
.#define SIZE 5
: This line defines a constant namedSIZE
with a value of 5. It helps in setting the size of the array.int main() { ... }
: This is the main function where the program execution begins.int arr[SIZE] = {1, 2, 3, 4, 5};
: This line declares an integer array namedarr
with a size ofSIZE
(which is 5 in this case) and initializes it with values 1, 2, 3, 4, and 5.int i;
: This declares an integer variablei
which will be used as a loop counter.printf("Elements of the array are: ");
: This line prints a message to indicate that the following output will be the elements of the array.for (i = 0; i < SIZE; i++) { ... }
: This is afor
loop that iterates through each element of the array from index 0 toSIZE - 1
.printf("%d ", arr[i]);
: Inside the loop, this line prints each element of the array using the%d
format specifier for integers andarr[i]
to access each element of the array.return 0;
: This statement signifies the end of themain()
function and returns an exit status of 0, indicating successful execution of the program.