-1

Code:

#include <stdio.h>
#define puts "%s C preprocessor"
int main()
{
    printf(puts, puts);
    return 0;
}

Output:

%s C preprocessor C preprocessor

See also...

Can anyone explain me the logic behind this output?

I tried to solve it,but I didn't get the proper explanation through my friends. If anyone can explain me ,it'll be very helpful.

5
  • 2
    Post code as text here instead of an external link of an image. Tell us what you have done so far to solve your problem. Macros are covered in any textbook on c. Don't shout (use all upper case). Commented Nov 27, 2022 at 9:06
  • What is your closest explanation you can come up with? What did you find out how the macro is replaced by preprocessor? Which part is missing then to get the output? Commented Nov 27, 2022 at 9:17
  • @AllanWind you don't shout Commented Nov 29, 2022 at 8:14
  • @Gerhardh , no part is missing. closest explanation would be % s is taking next argument as a string Commented Nov 29, 2022 at 8:17
  • 2
    Welcome to Stack Overflow! Please edit your post to add code and data as text (using code formatting), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and many more reasons. Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. See minimal reproducible example on what code is required. Commented Nov 29, 2022 at 8:47

1 Answer 1

0

The macro let to (code after linker):

int main
{
    printf("%s C preprocessor", "%s C preprocessor");
    return 0;
}

printf works like:

enter image description here

The output is then

%s C preprocessor C preprocessor
Sign up to request clarification or add additional context in comments.

13 Comments

what's the reason behind this output?
A, reason for this outpul? why is%s got printed?
Well, at first the linker changes the code because of the macros. Then printf has 2 identical strings. The first string is taken as formatstring, the second one as a simple string. Then printf replaces the "%s" in the formatstring by the second argument. There the "%s" will not be replaced, because its not in the formatstring.
Added an image of how the substitution in printf works
And now, be so kind an rewrite your question with lowercase letters. To write all uppercase classified as rude!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.