Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
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, 1013👍, 0💬
Popular Posts:
Can Sub Procedure/Function Be Called Recursively? - Oracle DBA FAQ - Creating Your Own PL/SQL Proced...
How can we know the number of days between two given dates in PHP? Simple arithmetic: <?php $...
How can I check for HTML errors? HTML validators check HTML documents against a formal definition of...
What is the difference between mysql_fetch_object() and mysql_fetch_array() functions in PHP? mysql_...
Can we get a strongly typed resource class rather than using resource manager? In the previous quest...