×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

How to escape the % (percent) sign in C's printf

Learn how can I escape the % (percent) sign in printf() statement in C language? By Shubh Pachori Last updated : April 20, 2023

Escape the % (percent) sign

To print the % (percent) sign in the message by using the printf() function in C language, then we have to use it twice (%%). Because if we use it once like any other message it will return some random value in the message which will be printed.

Example: Using a single % (percent) sign

#include <stdio.h>

int main()
{
    printf("% this is the modulus operator.");
    printf("\n modulus sign is % in c.");
    
    return 0;
}

Output:

% his is the modulus operator.
 modulus sign is  1204043788n c.

In the above code, we can see that the "%" sign is creating some problem in the code that is visible in the output. The compiler is giving a warning for the use of the "%" sign.

So, if we want to print the "%" sign in the printf() function, use the percent sign twice (%%).

Example: Using double %% (percent) sign to print it

#include <stdio.h>

int main()
{
    printf("%% this is the modulus operator.");
    printf("\nmodulus sign is %% in c.");
    
    return 0;
}

Output:

% this is the modulus operator.
modulus sign is % in c.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.