quadmath_005fsnprintf.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 2010-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.2 or
  6. any later version published by the Free Software Foundation; with no
  7. Invariant Sections, with the Front-Cover Texts being "A GNU Manual,"
  8. and with the Back-Cover Texts as in (a) below. A copy of the
  9. license is included in the section entitled "GNU Free Documentation
  10. License."
  11. (a) The FSF's Back-Cover Text is: "You have the freedom to
  12. copy and modify this GNU manual. -->
  13. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>GCC libquadmath: quadmath_snprintf</title>
  16. <meta name="description" content="GCC libquadmath: quadmath_snprintf">
  17. <meta name="keywords" content="GCC libquadmath: quadmath_snprintf">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  24. <link href="I_002fO-Library-Routines.html#I_002fO-Library-Routines" rel="up" title="I/O Library Routines">
  25. <link href="GNU-Free-Documentation-License.html#GNU-Free-Documentation-License" rel="next" title="GNU Free Documentation License">
  26. <link href="strtoflt128.html#strtoflt128" rel="prev" title="strtoflt128">
  27. <style type="text/css">
  28. <!--
  29. a.summary-letter {text-decoration: none}
  30. blockquote.smallquotation {font-size: smaller}
  31. div.display {margin-left: 3.2em}
  32. div.example {margin-left: 3.2em}
  33. div.indentedblock {margin-left: 3.2em}
  34. div.lisp {margin-left: 3.2em}
  35. div.smalldisplay {margin-left: 3.2em}
  36. div.smallexample {margin-left: 3.2em}
  37. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  38. div.smalllisp {margin-left: 3.2em}
  39. kbd {font-style:oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. pre.smalldisplay {font-family: inherit; font-size: smaller}
  45. pre.smallexample {font-size: smaller}
  46. pre.smallformat {font-family: inherit; font-size: smaller}
  47. pre.smalllisp {font-size: smaller}
  48. span.nocodebreak {white-space:nowrap}
  49. span.nolinebreak {white-space:nowrap}
  50. span.roman {font-family:serif; font-weight:normal}
  51. span.sansserif {font-family:sans-serif; font-weight:normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  57. <a name="quadmath_005fsnprintf"></a>
  58. <div class="header">
  59. <p>
  60. Previous: <a href="strtoflt128.html#strtoflt128" accesskey="p" rel="prev">strtoflt128</a>, Up: <a href="I_002fO-Library-Routines.html#I_002fO-Library-Routines" accesskey="u" rel="up">I/O Library Routines</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  61. </div>
  62. <hr>
  63. <a name="quadmath_005fsnprintf-_002d_002d_002d-Convert-to-string"></a>
  64. <h3 class="section">3.2 <code>quadmath_snprintf</code> &mdash; Convert to string</h3>
  65. <p>The function <code>quadmath_snprintf</code> converts a <code>__float128</code> floating-point
  66. number into a string. It is a specialized alternative to <code>snprintf</code>, where
  67. the format string is restricted to a single conversion specifier with <code>Q</code>
  68. modifier and conversion specifier <code>e</code>, <code>E</code>, <code>f</code>, <code>F</code>, <code>g</code>,
  69. <code>G</code>, <code>a</code> or <code>A</code>, with no extra characters before or after the
  70. conversion specifier. The <code>%m$</code> or <code>*m$</code> style must not be used in
  71. the format.
  72. </p>
  73. <dl compact="compact">
  74. <dt>Syntax</dt>
  75. <dd><p><code>int quadmath_snprintf (char *s, size_t size, const char *format, ...)</code>
  76. </p>
  77. </dd>
  78. <dt><em>Arguments</em>:</dt>
  79. <dd><table>
  80. <tr><td width="15%"><var>s</var></td><td width="70%">output string</td></tr>
  81. <tr><td width="15%"><var>size</var></td><td width="70%">byte size of the string, including tailing NUL</td></tr>
  82. <tr><td width="15%"><var>format</var></td><td width="70%">conversion specifier string</td></tr>
  83. </table>
  84. </dd>
  85. <dt>Note</dt>
  86. <dd><p>On some targets when supported by the C library hooks are installed
  87. for <code>printf</code> family of functions, so that <code>printf (&quot;%Qe&quot;, 1.2Q);</code>
  88. etc. works too.
  89. </p>
  90. </dd>
  91. <dt>Example</dt>
  92. <dd><div class="smallexample">
  93. <pre class="smallexample">#include &lt;quadmath.h&gt;
  94. #include &lt;stdlib.h&gt;
  95. #include &lt;stdio.h&gt;
  96. int main ()
  97. {
  98. __float128 r;
  99. int prec = 20;
  100. int width = 46;
  101. char buf[128];
  102. r = 2.0q;
  103. r = sqrtq (r);
  104. int n = quadmath_snprintf (buf, sizeof buf, &quot;%+-#*.20Qe&quot;, width, r);
  105. if ((size_t) n &lt; sizeof buf)
  106. printf (&quot;%s\n&quot;, buf);
  107. /* Prints: +1.41421356237309504880e+00 */
  108. quadmath_snprintf (buf, sizeof buf, &quot;%Qa&quot;, r);
  109. if ((size_t) n &lt; sizeof buf)
  110. printf (&quot;%s\n&quot;, buf);
  111. /* Prints: 0x1.6a09e667f3bcc908b2fb1366ea96p+0 */
  112. n = quadmath_snprintf (NULL, 0, &quot;%+-#46.*Qe&quot;, prec, r);
  113. if (n &gt; -1)
  114. {
  115. char *str = malloc (n + 1);
  116. if (str)
  117. {
  118. quadmath_snprintf (str, n + 1, &quot;%+-#46.*Qe&quot;, prec, r);
  119. printf (&quot;%s\n&quot;, str);
  120. /* Prints: +1.41421356237309504880e+00 */
  121. }
  122. free (str);
  123. }
  124. return 0;
  125. }
  126. </pre></div>
  127. </dd>
  128. </dl>
  129. </body>
  130. </html>