I'm new to C, and I'm trying to print an array consisting of whole numbers upto a certain number(excluding this number) received as input. Here's my code:
#include <stdio.h>
int main(void) {
int n; int* k;
k = &n;
printf("Enter number");
scanf("%d", &n);
int set[*k];
for(int i=0; i<=n;i ){
set[i]= i;
}
}
I used printf
in the last line with different inputs to check if it was working properly. I expected the output to be 0 1 2 3....n-1
but instead I got the output 1 2 3 4 ....n
. Doesn't the index of an array begin with a zero? What am I missing here?
CodePudding user response:
Array indices in C do start at 0 but your loop is written incorrectly, and will let i
iterate all the way up to n
. Replace i <= n
with i < n
so the loop stops after n - 1
.