#include <stdio.h> // Include the standard I/O header file for using printf and other I/O functions
void main() {
int i; // Declare an integer variable 'i' for loop indexing
int array[4] = {10, 20, 30, 40}; // Initialize an array of 4 integers with values 10, 20, 30, and 40
// First loop: Increment each element of the array by 1
for (i = 0; i < 4; i++) {
array[i]++; // Increment the value at array[i] by 1
}
// Second loop: Print the updated values of the array
for (i = 0; i < 4; i++) {
printf("%d\t", array[i]); // Print each element of the array followed by a tab space
}
}
Explanation of the Code:
Header file inclusion:
#include <stdio.h>
: This line tells the compiler to include the Standard Input Output library. This library is required for functions likeprintf()
that we use for output.
Main function:
void main()
: Themain
function is the entry point of the program. In this case, it has a return type ofvoid
, meaning it does not return a value.
Array Initialization:
int array[4] = {10, 20, 30, 40};
: This line declares an integer array namedarray
with 4 elements. The array is initialized with values10, 20, 30, 40
.
First
for
loop:for (i = 0; i < 4; i++)
: This loop runs 4 times (i.e., fromi = 0
toi = 3
).- Inside the loop,
array[i]++
increments each element of the array by 1. The++
operator is a post-increment operator, meaning it adds 1 to the current value of the element.
Second
for
loop:for (i = 0; i < 4; i++)
: This loop also runs 4 times.printf("%d\t", array[i]);
: This prints each element of the updated array, followed by a tab (\t
) for spacing between elements.
Expected Output:
After the first loop, each element of the array is incremented by 1:
- The original array was:
[10, 20, 30, 40]
- After incrementing:
[11, 21, 31, 41]