
In this code:
#include
char* greet() {
return "Hello";
}
int main() {
printf("%s Mia\n", greet());
printf("Next statement");
return 0;
}
%s
is a format specifier used for strings. That means when the program runs, %s
is replaced by whatever the greet()
function returns.
Since greet()
returns the text "Hello"
, that text takes the place of %s
, so the output becomes:
Hello Mia
If you've seen format specifiers like %d
(for integers) or %f
(for floating-point numbers), %s
works the same way—it's just specifically used to print strings.
Here's a closer look at what this line is doing:
printf("%s Mia\n", greet());
This tells the program: Print a string (%s
), followed by the word “Mia”
and a newline. Replace %s
with the return value from the greet()
function.
So effectively, it behaves like:
printf("Hello Mia\n");
Also, the greet()
function is written as:
char* greet() {
return "Hello";
}
It returns a string (specifically, a pointer to the string "Hello"
), which is exactly what %s
expects in a printf
call.