2

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!

cssExp
  • 43
  • 5
  • 2
    `char firstname[5] = "Bruce";` Too short. "Bruce" requires 6 characters. Remember the null terminator. – Avi Berger Mar 26 '23 at 22:11
  • 2
    C strings ALSO require the terminating `'\0'`... Don't count letters; let the compiler do it correctly: `char firstname[] = "Bruce";` (Less maintenance when "Bruce" is changed to "George"...) The compiler knows best... – Fe2O3 Mar 26 '23 at 22:11
  • Those are not C strings, but character arrays, which cannot be passed to functions expecting a C string. – Weather Vane Mar 26 '23 at 22:13
  • 4
    You need a parameter to match each format specifier. If you want a value to be used for more than one format specifier, provide it more than once. – Avi Berger Mar 26 '23 at 22:13
  • 1
    If you can use POSIX extensions, see https://stackoverflow.com/questions/19327441/gcc-dollar-sign-in-printf-format-string – Solomon Ucko Mar 26 '23 at 22:24

1 Answers1

7

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);
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335