I'm doing Harvard CS50, and in the second class on C, the instructor says %s would automatically print each extra argument in succession.
Okay, that's cool. But what if I want to print the first string multiple times?
#include <stdio.h>
int main(void) {
char firstname[5] = "Bruce";
char lastname[5] = "Wayne";
printf("Hi, %s!\nI am your virtual butler. I will call you Master %s from now on.\nWelcome to %s Manor!", firstname, lastname);
}
However, It outputs:
Hi, BruceWayne!
I am your virtual butler. I will call you Master Wayne from now on.
Welcome to q��U Manor!
Instead of:
Hi, Bruce!
I am your virtual butler. I will call you Master Wayne from now on.
Welcome to Wayne Manor!
I've tried searching on Google and did not find relevant information to fix the issue myself.
Update #1:
As per suggestions, I made changes.
#include <stdio.h>
int main(void) {
char firstname[] = "Bruce";
char lastname[] = "Wayne";
printf("Hi, %s!\nI am your virtual butler. I will call you Master %s from now on.\nWelcome to %s Manor!", firstname, lastname);
}
It outputs:
Hi, Bruce!
I am your virtual butler. I will call you Master Wayne from now on.
Welcome to ���(V Manor!
It's still having trouble with Wayne Manor.
Update #2
printf("Hi, %s!\nI am your virtual butler. I will call you Master %s from now on.\nWelcome to %s Manor!", firstname, lastname, lastname);
Works as intended!