dprintf
for conditional printing
As you may know, printing something by a condition can be done with ?:
, &&
or ||
:
cond&&printf("%d\n",n);cond||printf("%d\n",n);cond?:printf("%d\n",n);
Another interesting idea is to use dprintf
, which is the same as printf
except that it takes an extra argument, specifying the output file descriptor. It will only output to STDOUT if said argument is equal to 1. This can be abused to potentially save a few bytes over the previously mentioned methods:
x-1||printf("%d\n",n);dprintf(x,"%d\n",n);