Wrong output for C program in Clion (works in other compilers)

I wrote a program that creates an array with the size of 10, takes all elements and prints out the square numbers of those elements. The output should be:

1 4 9 16 25 36 49 64 81 100

But it is:

1 4 9 16 25 36 49 64 81 10

The last number is cut off. Since this works in other compilers this has to be an issue in CLion, what seems to be the issue? If I do all that without using an array, it correctly prints out all elements.

 

Here's my code:

 


#include <stdio.h>
#define nl printf("\n")

int main(){

int i;
int ar[10];

for (i = 1; i <= 10; i++)
ar[i] = i*i;
nl;

for (i = 1; i <= 10; i++)
printf("%d ", ar[i]);
nl;

return 0;
}
0
1 comment
Avatar
Permanently deleted user

Numeration of arrays in C starts with 0, thus if you create `int ar[10];`, you can't address 10th element.

 Code below works good. And Clion isn't compiler.

    #include <stdio.h>
#define nl printf("\n")

int main(){

int i;
int ar[10];

for (i = 0; i < 10; i++)
ar[i] = (i+1)*(i+1);

for (i = 0; i < 10; i++)
printf("%d ", ar[i]);
nl;

return 0;
}
0

Please sign in to leave a comment.