How to change the output of printf() in main() ?

Consider the following program. Change the program so that the output of printf is always 10. It is not allowed to change main(). Only fun() can be changed.

filter_none

edit
close

play_arrow

link
brightness_4
code

void fun()
{
    // Add something here so that the printf in main prints 10
}
  
int main()
{
    int i = 10;
    fun();
    i = 20;
    printf("%d", i);
    return 0;
}

chevron_right


We can use Macro Arguments to change the output of printf.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
void fun()
{
   #define printf(x, y) printf(x, 10);
}
  
int main()
{
    int i = 10;
    fun();
    i = 20;
    printf("%d", i);
    return 0;
}

chevron_right


Output:



10

This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


12


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.