Reputation: 46778
void func(char a, int num)
{
printf("%c",a);
}
int main()
{
func("a", 6); //not func('a',6);
printf("\n");
func("b", 2); //not func('b',6);
}
I understand I am passing a char array of a and b with a null character \0
.
Could someone how it ends up printing the characters $
and &
?
Upvotes: 1
Views: 257
Reputation: 98118
You are passing pointer to a literal string but the func
expects a character. Change it to receive an array:
void func(char *a, int num)
{
printf("%c",a[0]); // also note that to print a char you need to
// 'select' a char from the array
}
Otherwise you'll end up printing to a char that is the ascii representation of whatever the address of a
has as the first byte.
Upvotes: 2
Reputation: 137432
It could end up printing anything pretty much, probably part of the adresses of "a"
and "b"
match the ascii code of $
and &
.
Upvotes: 4