How to print % using printf()?

CProgrammingServer Side Programming

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%\n");
   printf("%%\n");
   printf("%c\n",'%');
   printf("%s\n","%");
   char a[5];
   strcpy(a, "%%");
   printf("This is a's value: %s\n", a);
   return 0;
}

Output

welcome%
%
37
%
%
This is a's value: %%
raja
Published on 08-Oct-2018 06:30:00

Advertisements