-3
main()
{
int i;
int a[5];
for (i = 1; i <= 5; i++)
    a[i] = 0;
printf("Hello, how are you? ");
}

why is the program not coming out of the loop?

Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42
  • 3
    Arrays in C use zero-based indices. Valid indices for your array are 0, 1, 2, 3 and 4. – David Heffernan Dec 27 '12 at 12:52
  • 2
    possible duplicate of [Why is there an infinite loop in my program?](http://stackoverflow.com/questions/14020402/why-is-there-an-infinite-loop-in-my-program) – P.P Dec 27 '12 at 13:08
  • 1
    So, what was your answer in the interview, and did you get the job? ;) – Mats Petersson Dec 27 '12 at 13:41
  • 1
    I would also point out that there is ABSOLUTELY no guarantee that this will end in an infinite loop. It may do, but i could be held in a register for the duration of the entire execution. Or, if it's On a 64-bit system, since stack is always aligned to 8 bytes, 5 * 4 = 20 gets rounded to 24 bytes and thus the extra 0 gets written to a "unused" location. – Mats Petersson Dec 27 '12 at 13:43
  • 1
    possible duplicate of [What's happening here in this for loop](http://stackoverflow.com/questions/13010004/whats-happening-here-in-this-for-loop) – Bo Persson Dec 27 '12 at 13:52

6 Answers6

14

C arrays are zero-based so you should use indexes [0..4] rather than [1..5].

Writing to a[5] writes beyond the bounds of your array. This has undefined results; in your case, this happens to be the address of your loop counter i which is reset to 0.

You can fix this by using

for (i = 0; i < 5; i++)

or, better,

for (i = 0; i < sizeof(a)/sizeof(a[0]); i++)

as your loop.

simonc
  • 41,632
  • 12
  • 85
  • 103
  • 2
    Actually, he is writing 5 elements - but starting from 1 instead of zero. (This pedantic note does not change the correct main reasoning of this answer) (+1) – amit Dec 27 '12 at 12:52
  • 1
    To add to your answer, arrays in C and zero-based not 1-based: i.e. the valid index values for a[5] are 0 through 4, not 1 through 5. – ChrisW Dec 27 '12 at 12:52
  • +1 for overwriting the loop counter explanation. Where is that documented? – user1284631 Dec 27 '12 at 12:54
  • @axeoth It's not, it's UB. That's just the most likely explanation. – sepp2k Dec 27 '12 at 12:55
  • @amit, Thanks, I'd missed that the loop was starting from array index 1. I've updated my answer accordingly now – simonc Dec 27 '12 at 12:56
  • @ChrisW Thanks, I've updated my answer as per your suggestion – simonc Dec 27 '12 at 12:56
  • @sepp2k: Thanks. But... what the "UB" means? – user1284631 Dec 27 '12 at 12:57
  • @axeoth UB means Undefined Behaviour. Its what I meant by saying that writing beyond the end of the array had undefined results. Different compilers could behave differently here. – simonc Dec 27 '12 at 12:58
  • @axeoth It's undefined behavior - like simonc said in his answer. – sepp2k Dec 27 '12 at 12:58
  • @simonc: OK, OK... I know what undefined behavior is, I just did not linked the UB with... the undefined behavior. – user1284631 Dec 27 '12 at 12:59
  • 2
    @simonc And so could the same compiler at different optimization levels or with a slightly changed program. – sepp2k Dec 27 '12 at 12:59
  • 1
    .. or even better `int a[5] = {0};` if you're using a remotely modern C compiler (but +1 on a great explanation of 0-based indexing and UB). – WhozCraig Dec 27 '12 at 13:01
5

You're accessing the array out of bounds (the legal indices for an array of size 5 are indices 0 to 4, not 1 to 5). As far as the standard is concerned, this means your program invokes undefined behavior and can behave any way it pleases.

In terms of the actual implementation, what's actually happening is probably that your last array access (i.e. a[5] = 0;) write over variable that comes after a in memory, which happens to be i on your system. So i is reset to 0 and the loop continues forever.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
5
#include <stdio.h>
main()
{
int a[5];
int i;
printf("Memory location of i = %p\n",&i);
printf("Memory location of a[5] = %p\n",&a[5]);
for (i = 1; i <=5; i++)
{
    a[i] = 0;
    sleep (2);
   printf("Value of i=%d\n",i);
}
printf("Hello, how are you? \n");
}

See the o/p it is interesting .. [ will understood why value of i became 0(Zero)]

mnds@mind-AcerPower-Series:/tmp$ ./a.out 
Memory location of i = 3218951772
Memory location of a[5] = 3218951772
Value of i=1
Value of i=2
Value of i=3
Value of i=4
Value of i=0
Arun
  • 1,651
  • 4
  • 20
  • 31
  • +1 Even though this has already been stated twice in different answers, code to back it up is always a good thing. – Nocturno Dec 27 '12 at 13:12
  • except that the OP defined variable i *before* defining the array. Maybe the compiler optimized and placed i after. Anyway, it should be interesting to have a look at it. – user1284631 Dec 27 '12 at 13:13
  • 3
    @Arun: You should use %p for printing the address, not %u. – user1284631 Dec 27 '12 at 13:19
1

If you are writing a[5], that's outside the boundary.

Your loop should be:

for(i=0; i<5; i++)
user1284631
  • 4,446
  • 36
  • 61
1

You pass the size of the integer array, array size is 5 so maximum a[4] is accessible. Try this:

main()
{
    int i;
    int a[5];
    for (i = 0; i < 5; i++)
        a[i] = 0;
    printf("Hello, how are you? ");
}
Berkay Turancı
  • 3,373
  • 4
  • 32
  • 45
0

Because it invokes undefined behavior, so anything can happen (infinite loops included).

When your array has 5 elements, the indices of these elements range from 0 to 4. Accessing a[5] is an invalid operation.

Arrays in C are zero-indexed, so in order your program to work correctly, you should have written

for (i = 0; i < 5; i++)