Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to the same string multiple times in C via printf

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

The problem is that these arrays

char firstname[5] = "Bruce";
char lastname[5] = "Wayne";

do not contain strings. You need either to enlarge arrays to include the terminating zero character ‘\0’ of the string literals like

char firstname[6] = "Bruce";
char lastname[6]  = "Wayne";

or to allow the compiler itself to calculate sizes of the arrays like

char firstname[] = "Bruce";
char lastname[]  = "Wayne";

Another problem is that the format string contains three conversion specifiers s but you supplied only two corresponding arguments

printf("Hi, %s\nI am your virtual butler. I will call you Master %s from now on.\nWelcome to %s Manor!", firstname, lastname);

You need to write adding one more argument

printf("Hi, %s\nI am your virtual butler. I will call you Master "
       "%s from now on.\nWelcome to %s Manor!", 
       firstname, lastname, lastname);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading