int main(void)
{
char s[4] = "heloo"; // The character array is initialized with more data than its size
printf("%s",s);
}
The output is: helo�[�G�
.
Why is the output in this format?
int main(void)
{
char s[4] = "heloo"; // The character array is initialized with more data than its size
printf("%s",s);
}
The output is: helo�[�G�
.
Why is the output in this format?
The compiler shall issue an error because there are more initializers than the number of elements of the array and the redundant initializer is not the terminating zero of the string literal.
From the C Standard (6.7.9 Initialization)
2 No initializer shall attempt to provide a value for an object not contained within the entity being initialized.
The one exclusion of this rule is regards character arrays when the terminating zero of a string literal can be excluded from initializers if the character array does not have a corresponding element.
14 An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
If the program was run then it has undefined behavior. The character array does not contain a string that is required when the conversion specifier %s is used.