cprintf

How to swallow a parameter in printf in C


Say I have a printf with many parameters:

printf("%d %d %d %d", A, B, C, D);

For some reason I would like one parameter to no longer be printed, but still be listed in the parameter list (for instance for visual reason, or maybe it's a function call with a necessary side effect, etc).

Can I replace %d with a conversion letter with no output ? I don't recall such a conversion letter. Or maybe playing with the flags...?

[Edit] I just noticed that scanf has something similar (but the reverse): an assignment suppression flag '*'. for instance sscanf("123 345 678", "%i %*i %i", &a, &b) will lead to a=123 b=678


Solution

  • You can suppress Strings (char*) arguments by specifying zero-width.

    However, I know of no way to suppress numeric (int and float) values.

    int main(void) {
        int a=1;
        char*  b="hello";  // Gets Suppressed
        int c=3;
    
        printf("%d %.0s %d\n", a, b, c); // .0 means "zero-width"
        return 0;
    }