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, 1299👍, 0💬
Popular Posts:
How To Set session.gc_divisor Properly? - PHP Script Tips - Understanding and Using Sessions As you ...
What is CMM and different levels? explain? The Capability Maturity Model (CMM) is a process capabili...
How do you estimate maintenance project and change requests?
What is shadowing ? When two elements in a program have same name, one of them can hide and shadow t...
What Happens to Your Transactions When ERROR 1213 Occurred? - MySQL FAQs - Transaction Management: C...