-1

I was wondering if I could print % in c but when I use printf("%") It just shows this syntax in output.

    #include<stdio.h>
    int main()
   {
       printf("%");
   }

but the result is like this:

main.c:13:13: warning: spurious trailing ‘%’ in format [-Wformat=]

Does anybody know how I can literally print % as a character in c.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93

4 Answers4

1

You can do:

printf("%%");

or because % is a char, so you can use %c:

printf("%c", '%');

Overall, you have two ways.

#include<stdio.h>
int main()
{
   printf("%%\n");
   printf("%c\n", '%');
}

will prints:

%
%

1

To print a % character you need to follow this structure:

int main()
{
    printf("%%");
    return 0;
}

you have to use the character twice.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
0

The good old method would be to use the %c format specifier in printf() function

    #include<stdio.h>
    int main()
   {
       printf("%c",'%');
   }
Abhijit
  • 1
  • 2
0
#include <stdio.h>

int main()
{
    printf("%%");
    return 0;
}