-3

Please explain the output of the following program, why the array is printing till 10th array index, if I do not terminate the string with a null character it always print the size of the array and then the loop terminates which means it now found the null character. I checked with different size of array every time I found same result the size at the last and then

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    char name[10];

    for(i=0;i<10;i++)
    {
        name[i]='A';
    }
    for(i=0;name[i]!='\0';i++)
    {
        printf("%d %d\n",i,name[i]);
    }
}

OUTPUT is
0 65
1 65
2 65
3 65
4 65
5 65
6 65
7 65
8 65
9 65
10 10
Vivek Singh
  • 11
  • 1
  • 4
  • 1
    This is just luck. You code reads whatever garbage it finds present in memory, and that until it finds a null byte which happens to be there. – Ra'Jiska Mar 07 '18 at 16:45
  • UB. You're just getting "lucky" (some may say "unlucky"). There happens to be a 0 right after the array in memory. Ide produces different results: https://ideone.com/nhbS4r – 001 Mar 07 '18 at 16:45
  • 1
    Referencing `name[10]` produces undefined behavior, so anything can happen. What is likely happening in your case is that name + 10 addresses the least significant byte of the integer next to `name` on the stack, which happens to be 10. The next significant byte is 0, which is addressed by name + 11. – William Pursell Mar 07 '18 at 16:46
  • Hi Rajiska it may be luck but I was just experimenting I found every time it prints the size of the array then it terminates means finds a null. – Vivek Singh Mar 14 '18 at 06:18
  • I am using gcc compile, I know the program could have crashed it is just luck, but I am interested in why it always print size of array and then immediately finds a null in next memory block I have checked hundreds of times every time it is same anyway I always terminate my strings with a null. thanks – Vivek Singh Mar 14 '18 at 06:30

2 Answers2

2

Array access out of bound is undefined behavior. You have accessed array index out of bound. You have gained undefined behavior.

The program could have crashed, stopped or worked perfectly towards some random index or simply could have stopped at index 9. There is no guarantee what will happen. You will be suggested to refrain from introducing undefined behavior in your code. Don't do this. C doesn't provide checking for array index out of bound. You have to be careful on this case.

You can't call it string - it is a char array. Not a string. We can't say it string unless it ends with \0.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • @DavidRTribble.: And `strlen` etc won't work with unterminated string. Unterminated string and char array has no difference. And most probably you have used string in the sense that sequence of character. I just went with the convention of ours to call nul terminated char array to be string. That is all there is. – user2736738 Mar 07 '18 at 16:52
1

You're reading past the end of your array, which results in undefined behaviour.

In this case, you're running into the memory used to store the value of i, which is probably stored as four bytes 0a 00 00 00 (i.e., the number 10 in little-endian byte order).

But literally anything could happen when you invite UB. So don't do it!

r3mainer
  • 23,981
  • 3
  • 51
  • 88