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, 1201👍, 0💬
Popular Posts:
How To Avoid the Undefined Index Error? - PHP Script Tips - Processing Web Forms If you don't want y...
What will be printed as the result of the operation below: main() { char *p1; char *p2; p1=(char *)m...
How is the MVC design pattern used in Struts framework? In the MVS design pattern, there 3 component...
What will the following piece of code do? int f(unsigned int x) { int i; for (i=0; x!=0; x>&a...
How can you determine the size of an allocated portion of memory? You can't, really. free() can , bu...