简体   繁体   中英

printf defined as void

I figured out a way to silence all printf s at once, while reading C-FAQs .

Could you tell me how this is working. Program:

#include<stdio.h>

//How to silence all printfs at once!!!

#define printf (void) 

#define ab a b c d e f
#define xstr(a) str(a)
#define str(a) #a

#define string "The politicians do not even know the" xstr(ab) "of politics"

main(){

char *all=str(a b c d e f);

printf("%s\n",all);
printf(string);

}

Output of the program is blank. I mean it does not print anything at all. If i #define printf as:

/ #define printf  

This too behaves the same way. I am not understanding how GCC is compiling the calls in the two cases. No errors and no warnings absolutely.

  • 1st case printf becomes: (void)("%s\\n",all);
  • 2nd case printf becomes: ("%s\\n",all);

C有一个逗号运算符 ,这意味着两个表达式都是有效的。

预处理器会将函数替换为空格或void,这就是为什么在运行时不调用它的原因。

When it happens, both time it compiles to a bracket expression, which simply does nothing and returns the value of its last element (separated by commas - this is C's Comma Operator which you can see sometimes in loop conditions). Even if you don't cast it to void, the default setting in most compilers is not to warn about ignored non-void return values, so it compiles fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM