Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How can I print a character in a printf format string?
How can I print a '%' character in a printf format string? I tried \%, but it didn't work.
✍: Guest
Simply double the percent sign: %% .
The reason it's tricky to print % signs with printf is that % is essentially printf's escape character. Whenever printf sees a %, it expects it to be followed by a character telling it what to do next. The two-character sequence %% is defined to print a single %.
To understand why \% can't work, remember that the backslash \ is the compiler's escape character, and controls how the compiler interprets source code characters at compile time. In this case, however, we want to control how printf interprets its format string at run-time. As far as the compiler is concerned, the escape sequence \% is undefined, and probably results in a single % character. It would be unlikely for both the \ and the % to make it through to printf, even if printf were prepared to treat the \ specially.
2015-11-09, 1123👍, 0💬
Popular Posts:
In below sample code if we create a object of class2 which constructor will fire first? Public Class...
what are the advantages of hosting WCF Services in IIS as compared to self hosting? There are two ma...
What is PMP(project management plan)? The project management plan is a document that describes the p...
What is the FP per day in your current company?
How Can we change priority & what levels of priority are provided by Dot Net? Thread Priority ca...