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

CServer Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

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 22:30:26