Ben Ben - 2 months ago 12
C Question

How can I combine these two printf statements in C?

I'm trying to print "Hello the number 5 is correct!" in C.

The way I'm doing it now is with two printf statements:

printf("Hello the number %d", number);
printf(" is correct!\n");


How can I do this in one statement like in Java:

System.out.println("Hello the number "+number+" is correct!");


I've tried doing it this way in C:

printf("Hello the number %d", number, " is correct!");


but the "is correct!" doesn't show up.

is there a way to do this in one statement?
I'm sorry I'm very new to C.

jxh jxh
Answer

You can embed the format specifier into the middle of the string like so:

printf("Hello the number %d is correct!\n", number);

Alternatively, you can use another format specifier for the rest of the string:

printf("Hello the number %d%s\n", number, " is correct!");
Comments