How to change output of printf() in main() function

Wednesday, January 11, 2017 Unknown 0 Comments

 Output of printf() inside main() function can be changed by defining a macro.

For example

#include<stdio.h>

void changeDef(){
#define printf(a,b) printf(a,'x');
}

int main(){
char c='k';
changeDef();
printf("character c=%c",c);
return 0;
}

output:
character c=x

explanation :-  changeDef() will change the definition of printf(). so, it will always print 'x' irrespective of the actual value of c inside main() function.

0 comments: