Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it wrong to define a macro for printf?

Tags:

c

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 ;)

like image 936
user3389315 Avatar asked Jun 27 '26 21:06

user3389315


1 Answers

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.

like image 168
Keith Thompson Avatar answered Jul 02 '26 22:07

Keith Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!