Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will be the output, if we print a string that contains "%s" in it?

I'd like to know what the result would be if we print a string that contains "%s" in its content?

I tried printing it as "hi%s".

char *ptr="hi%s";
printf(ptr);

I expected the output to be as "hi". but i got it as "hi hi%s".

"hi hi%s"

like image 518
chetan vamshi Avatar asked Jul 16 '19 14:07

chetan vamshi


2 Answers

As specified in other answered, a undefined behavior will occur.

What the undefined behavior means in this context: When printf receives a string with n number of format specifiers (such as %s) it is going to expect n number of parameters to be passed to the function in addition to the string. So, when you have a statement like this printf("hi%s"), the function will behave as if you passed a parameter (in this case the second parameter should be a char *) even though there is not. The function is just going to get the next value on the stack which is some junk value in this case. Then the function will deference this junk value and will treat it as a buffer of characters. The reasons why this behavour is undefined is there is no telling what the junk value on the stack could be.

Possible Outcomes

  1. The junk value on the stack is 0 -> Segmentation fault when junk value is dereferenced.

  2. The junk value on the stack happens to be a valid memory address -> The bytes at the memory location will keep getting inserted into the string (appended to "hi" in this case) until either a byte of value 0 is encountered or a segmentation fault occurs.

  3. The junk value on the stack is not 0 but is not a valid memory location -> Segmentation fault when junk value is dereferenced.

Producing the same situation The below portion of code is a very similar situation to printf("hi%s")

void foo() {
   char * myJunkValue;  // Since not a global/static variable, this is not guaranteed to be 0
   printf(myJunkValue); // Undefined behavior
}
like image 173
Cabbage Champion Avatar answered Nov 09 '22 02:11

Cabbage Champion


Your program invokes undefined behaviour.

Your code is equivalent to

 printf("hi%s");

where %s is a conversion specifier and it expects an argument to be supplied.

Quoting C11, chapter §7.21.6.1

[...] If there are insufficient arguments for the format, the behavior is undefined. [....]

Suggestion: If you just have to print a string, without a need of any conversion (formatting), you can use puts().

like image 27
Sourav Ghosh Avatar answered Nov 09 '22 04:11

Sourav Ghosh