Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to print % using printf()?
In C, the printf() function uses the percent symbol (%) as a format specifier. To print the actual % character as text, you need to use a double percent (%%) because a single % has special meaning in printf() and will not display anything.
Syntax
printf("%%"); // Prints a single % character
Example 1: Basic % Printing
Here's how to print the % symbol using printf() −
#include <stdio.h>
int main() {
printf("Single percent: %%<br>");
printf("Percentage: 85%%<br>");
printf("Multiple: %% %% %%<br>");
return 0;
}
Single percent: % Percentage: 85% Multiple: % % %
Example 2: Alternative Methods
You can also print % using character and string format specifiers −
#include <stdio.h>
int main() {
printf("Method 1 (double %%): %%<br>");
printf("Method 2 (char): %c<br>", '%');
printf("Method 3 (string): %s<br>", "%");
printf("Method 4 (ASCII): %c<br>", 37);
return 0;
}
Method 1 (double %): % Method 2 (char): % Method 3 (string): % Method 4 (ASCII): %
Key Points
- Use %% to print a single % character in printf()
- A single % without a format specifier will not print anything
- The % character has ASCII value 37
- Alternative methods include using %c with '%' or %s with "%"
Conclusion
To print the % symbol in printf(), always use %% (double percent). This escapes the special meaning of % and displays it as literal text.
Advertisements
