-1
#include<stdio.h>
void main()
{
char str[4] ="HELLO";
printf("%s",str);
}

What actually is happening here ??

The output is : HELL?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Notice that it did not happen quite like that, your string was *too long* for being an array initializer and the compiler output a diagnostics message. It would not have done so for `char str[5] = "HELLO";` (but the null terminator would have been missing anyway). – Antti Haapala -- Слава Україні Sep 18 '19 at 07:23
  • From [this initalization from string reference](https://en.cppreference.com/w/c/language/array_initialization#Initialization_from_strings): "If the size of the array is known, it may be one less than the size of the string literal, in which case the terminating null character is ignored" So as mentioned you can have `char str[5] = "HELLO";` but any array-size smaller than `5` will be invalid. – Some programmer dude Sep 18 '19 at 07:44

1 Answers1

1

You can´t assign 5 values to a 4 size array, when you put Hello it is interpreted as {'h', 'e', 'l', 'l', 'o'}, so you need at least 5 size array.

Taking into account that C needs also a last character '/0', i recommend you to put it as 6 size array