In the first program the function parameter
void pn(char x);
has the type char
that is the function expects as an argument a single character.
However the function is called with the string literal "HHH"
as its argument
pn("HHH");
String literals in C have types of character arrays. For example the string literal "HHH"
has the type char[4]
because the string literal includes also an invisible terminating zero character.
Used in expressions as for example as function arguments arrays are implicitly converted to pointers to their first elements. So the string literal "HHH"
is converted to a temporary object of the type char *
that points to the first character 'H'
..
As a result the function has undefined behavior due to the incompatibility of the parameter and the corresponding argument.
In the second program the function parameter was correctly changed to the type char *
and the program produced the expected result.
If you wanted to output just one character in the first program then it should be changed at least the following way
#include <stdio.h>
void pn(char x);
void main()
{
pn("HHH"[0]);
}
void pn(char x)
{
printf("Hello %c\n",x);
}
Or it would be more clear to call the function like
pn( 'H' );
passing a character literal.