In my struggle to learn C, I came across a situation where I can assign values to indices outside of array scope, or it seems to me I can. The code looks like this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned char c;
char aoeui[1];
srand (time(NULL));
int i = 0;
while (i < 10)
{
c = (rand() % 255);
if (isalnum(c))
{
printf("%c\n", c);
aoeui[i] = c;
++i;
}
}
printf("----\n");
printf("%s\n", aoeui);
return 0;
}
I tried to write the simplest possible random string generator. I declare an array of size 1:
But when it's printed,
Code:
printf("%s\n", aoeui);
10 characters are printed on the screen, for example:
Code:
j
O
b
j
L
5
a
q
J
N
----
jObjL5aqJN
I don't understand why it happens.