I'm asking this for general knowledge purposes.
Can I define printf in a header file and use pf to replace printf throughout the whole program?
Is it wrong based on any standards?
#include<stdio.h>
#define pf printf //printf defined here
int main(){
int x=10;
pf("x=%d",x); //pf is used instead of printf
}
PS: Learning new things just to get ahead in my class ;)
That's perfectly legal -- and horribly bad style.
The compiler will replace each occurrence of the identifier pf by printf.
But writing printf in full each time you use it makes your code much more readable to a human reader. If I see a call to printf, I know exactly what it means. If I see a call to something called pf, my first assumption will be that you have a function by that name, and I need to find out what it does. I have to hunt for you macro definition before I can understand what the call means. It wastes far more time than the 4 keystrokes you save by using a macro.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With