Tutorialspoint.dev

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.

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;
}

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

#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;
}

Output:

10


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter