How to Change the Output of printf() in main() in C?
Last Updated :
21 Jun, 2022
To change the output of printf() in main(), we can use Macro Arguments.
#define macro can be used for this task. This macro is defined inside the function. Although, #define can be used without declaring it in the function, in that case always the printf() will be changed. The function needs to be called first to change the output of printf() in main().
Consider the following program. Change the program so that the output of printf() is always 10.
C
#include <stdio.h>
void fun()
{
}
int main()
{
int i = 10;
fun();
i = 20;
printf ( "%d" , i);
return 0;
}
|
It is not allowed to change main(). Only fun() can be changed. Now, consider the following program using Macro Arguments,
C
#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
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...