#include<stdio.h>

int main()
{
int a[100], i=0;
while(1)

{

        a[i++]=5;
        printf("%d\n",i);

}
return 0;
}

on gcc this code doesn't give ny error. but after printing the value of i upto 100 , it restart writing value of i from 6 onward...
my question is how value of i is changing after reaching 100..

plz help

You answered it yourself.
You overran the array.
You then trashed i, and i got a new value.

There is no guarantee that the program will fail if you do anything wrong. If you want that kind of safety net, use another language.

but 'i' doesn't have any any connection with array...i is an independent value and i think it's value should increase infinitely....

Can u tell at what time ,i 's value is changed?

Once you're off in the land of undefined behavior, strange things can happen. In this case, I believe I need to reverse the declaration order to see which particular thing you may be seeing. That is,

int i=0, a[100];

...
100
101
102
103
6
7
8
...

This might indicate that the array and i are adjacent and that one of the things that gets trashed when the array is overrun is the value of i .

But trying to explain a particular undefined behavior is a fool's errand.

Perhaps simplify:

for(int i = 0; i < 100; i++)

Works out as:

int i  = 0;
while(i < 100)
  i++;

> i is an independent value and i think it's value should increase infinitely
It has to exist in memory SOMEWHERE.

The most likely scenarios are

+=========+
|    i    |
+=========+
| arr[99] |
+=========+
|   ...   |
+=========+
| arr[0]  |
+=========+

OR

+=========+
| arr[99] |
+=========+
|   ...   |
+=========+
| arr[0]  |
+=========+
|    i    |
+=========+

In one case, writing the mythical arr[100] in fact trashes i instead.
In the other case, you miss i, but probably go on to trash something else instead.

In either case, the code is trash.

commented: A picture is worth a thousand words. +24
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.