Two % for show % in C

Hello,

For show % we need %% but for * we don't need **
I don't understand well.


1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main()
{
    const int value = 5;
    int a;

    printf("Modulus %d:\n", value);
    for (a = 0; a < 30; a++)
        printf("%d %% %d = %d\n", a, value, a % value); // We need two %
    return(0);
}
%% escapes % in printf. * isn't a character that needs to be escaped.
for the show ' we used \' but why we cannot use \% for show %?
Because that's just the way it is.

But the more proper reason is that things like \", \', \n, etc. are escape characters built into the language. % is not a character that needs to be escaped in a string literal, so \% is not seen as a valid escape sequence by the language itself.

"%%" is literally two percent signs as far as C/C++ syntax goes, but the logic within printf treats it uniquely.
Last edited on
Topic archived. No new replies allowed.