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

CServer Side ProgrammingProgramming

Here we will see how to change the output of the printf() function from main(). Here we will define a function that will change all of the printf() statements with the given type to another type.

We will use the #define macro to do this task. This macro will be defined inside the function. We can directly put the #define line without using it in the function, but in that case always the printf() will be changed. To control it using main, we have to call the function first.

Example

#include <stdio.h>
void changePrintf() { //always any printf will print 50
   #define printf(x, y) printf(x, 50);
}
main() {
   int x = 40;
   changePrintf();
   printf("%d\n", x);
   x = 60;
   printf("%d", x);
}

Output

50
50
raja
Published on 02-May-2019 07:37:25

Advertisements