summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Fgets.html
blob: 3dfd399dab714ac104b0557cd14db09ee58d771b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    <h1 id="firstHeading" class="firstHeading">gets, gets_s</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdio.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-until-c11"> <td> <pre data-language="c">char *gets( char *str );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-until-c11">(removed in C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">char *gets_s( char *str, rsize_t n );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1)</span> Reads <code><a href="std_streams" title="c/io/std streams">stdin</a></code> into the character array pointed to by <code>str</code> until a newline character is found or end-of-file occurs. A null character is written immediately after the last character read into the array. The newline character is discarded but not stored in the buffer.</div> <div class="t-li1">
<span class="t-li">2)</span> Reads characters from <code><a href="std_streams" title="c/io/std streams">stdin</a></code> until a newline is found or end-of-file occurs. Writes only at most <code>n-1</code> characters into the array pointed to by <code>str</code>, and always writes the terminating null character (unless str is a null pointer). The newline character, if found, is discarded and does not count toward the number of characters written to the buffer. <dl>
<dd>The following errors are detected at runtime and call the currently installed <a href="../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <ul>
<li> <code>n</code> is zero </li>
<li> <code>n</code> is greater than <code>RSIZE_MAX</code> </li>
<li> <code>str</code> is a null pointer </li>
<li> endline or eof not encountered after storing <code>n-1</code> characters to the buffer. </li>
</ul> </dd>
<dd>In any case, <code>gets_s</code> first finishes reading and discarding the characters from <code><a href="std_streams" title="c/io/std streams">stdin</a></code> until new-line character, end-of-file condition, or read error before calling the constraint handler. </dd>
<dd>As with all bounds-checked functions, <code>gets_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../io" title="c/io"><code>&lt;stdio.h&gt;</code></a>.</dd>
</dl>
</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> str </td> <td> - </td> <td> character string to be written </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>str</code> on success, a null pointer on failure.</p>
<p>If the failure has been caused by end of file condition, additionally sets the <i>eof</i> indicator (see <code><a href="feof" title="c/io/feof">feof()</a></code>) on <code><a href="std_streams" title="c/io/std streams">stdin</a></code>. If the failure has been caused by some other error, sets the <i>error</i> indicator (see <code><a href="ferror" title="c/io/ferror">ferror()</a></code>) on <code><a href="std_streams" title="c/io/std streams">stdin</a></code>.</p>
<h3 id="Notes"> Notes</h3> <p>The <code>gets()</code> function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on <code>stdin</code>). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. <code><a href="fgets" title="c/io/fgets">fgets()</a></code> and <code>gets_s()</code> are the recommended replacements.</p>
<p><b>Never use <code>gets()</code>.</b></p>
<h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> K.3.5.4.1 The gets_s function (p: 602-603) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.19.7.7 The gets function (p: 298) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.9.7.7 The gets function </li></ul>
</ul>         <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fscanf" title="c/io/fscanf"> <span class="t-lines"><span>scanf</span><span>fscanf</span><span>sscanf</span><span>scanf_s</span><span>fscanf_s</span><span>sscanf_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> reads formatted input from <code><a href="std_streams" title="c/io/std streams">stdin</a></code>, a file stream or a buffer <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="fgets" title="c/io/fgets"> <span class="t-lines"><span>fgets</span></span></a></div> </td> <td> gets a character string from a file stream <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="fputs" title="c/io/fputs"> <span class="t-lines"><span>fputs</span></span></a></div> </td> <td> writes a character string to a file stream <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="https://en.cppreference.com/w/c/experimental/dynamic/getline" title="c/experimental/dynamic/getline"> <span class="t-lines"><span>getline</span><span>getwline</span><span>getdelim</span><span>getwdelim</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-dynamic-tr t-mark-ts">(dynamic memory TR)</span></span></span></div> </td> <td> read from a stream into an automatically resized buffer until delimiter/end of line <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/gets" title="cpp/io/c/gets">C++ documentation</a></span> for <code>gets</code> </td>
</tr> </table>            <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/io/gets" class="_attribution-link">https://en.cppreference.com/w/c/io/gets</a>
  </p>
</div>