I am assuming you have a basic knowledge about pointer. If not please read about it first.
"str1" in your code is a pointer to char array and it points to the first element of the array ('H').
%c modifier is designed to take input a char value and print the ASCII representation of that value. In this case it is 'H'.
On the other hand, %s modifier is designed to work similar to the following way:
- take input a pointer to char
- make a copy of the pointer. let it be cpointer.
- if the value of the memory location stored in the cpointer is NULL('\0') goto 7
- print the value of the memory location stored in the cpointer.
- increment cpointer so that it point to the next element of the char array.
- goto 3.
- end printing char array.
As a result, it will print the whole string. If you are wondering about how did it found '\0' in the end of the string. it can because, when we assign some value while declaring a char array (ex: char str[] = "abc") compiler automatically adds a null character in the end. For this reason, we must specify char array size to be 1 greater than the length of the string it will store. So you declaration should have been:
char str[13] = "Hello world!";
Also when we assign a value while declaring char array, we need not specify the size of the array. It is automatically determined by the comiler. So following code will work too.
char str[] = "Hello world!";