A character array has storage beyond its terminator

Draw all five elements of code, distinguishing the null character from the digit character zero. Trace length and sum, give the exact output, and explain why code[4] exists but does not appear in the %s field. Justify code[length]-'0' and '0'+sum%10 without assuming ASCII. All characters examined by the loop before its terminator are decimal digits.

#include <stdio.h>
int main(void) {
    char code[5] = {'5', '0', '8', '\0', '9'};
    int sum = 0, length = 0;
    while (length < 5 && code[length] != '\0') {
        sum += code[length] - '0';
        ++length;
    }
    code[1] = (char)('0' + sum % 10);
    printf("length=%d sum=%d code=%s tail=%c\n", length, sum, code, code[4]);
    return 0;
}

