How to print % using printf()?



Generally, printf() function is used to print the text along with the values. If you want to print % as a string or text, you will have to use ‘%%’. Neither single % will print anything nor it will show any error or warning.

Here is an example to print % in printf() in C language,

Example

 Live Demo

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

Output

%

There are some other ways to print % in the text message as in the following example,

Example

 Live Demo

#include<stdio.h>
#include<string.h>
int main() {
   printf("welcome%
");    printf("%%
");    printf("%c
",'%');    printf("%s
","%");    char a[5];    strcpy(a, "%%");    printf("This is a's value: %s
", a);    return 0; }

Output

welcome%
%
37
%
%
This is a's value: %%
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements