I wrote a simple C program which reads space separated integers from stdin
.
#include<stdio.h>
int main(){
int k, n, a[5], i = -1;
printf("Enter K and N: \n");
scanf("%d %d", &k, &n);
printf("Enter the values: \n");
scanf("%d %d %d %d %d", &a[++i], &a[++i], &a[++i], &a[++i], &a[++i]);
for (int i = 0; i < 5; ++i)
{
printf("%d ", a[i]);
}
return 0;
}
If you enter 1, 2, 3, 4, 5
, the output given is 5, 4, 3, 2, 1
. If you don't use i
while taking the input and use the values 0, 1, 2, 3, 4
instead, the inputs are taken in normal way. I want to understand why is the program behaving so.
Screenshot of output: