Question

I have tried escaping it using a backslash.

#include <stdio.h>

int main(void) {
    printf("\%");
    return 1;
}

But it doesn't work. What is the correct syntax?

Was it helpful?

Solution 2

 printf("\%");  

will print % if you compile it in C89 mode (in K&R C, it is behavior defined).
In C99/11 this is not valid.

C11: 6.4.4.4 Character constants:

The double-quote " and question-mark ? are representable either by themselves or by the escape sequences \" and \?, respectively, but the single-quote ' and the backslash \ shall be represented, respectively, by the escape sequences \' and \\.

confirms that there is no escape sequence like \%.
The preferred way is

  printf("%%");   

Almost similar question is answered here .

OTHER TIPS

Two percentage signs:

printf("%%");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top