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


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
", x);    x = 60;    printf("%d", x); }

Output

50
50

Updated on: 30-Jul-2019

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements