summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fstatements.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/language%2Fstatements.html')
-rw-r--r--devdocs/c/language%2Fstatements.html113
1 files changed, 113 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fstatements.html b/devdocs/c/language%2Fstatements.html
new file mode 100644
index 00000000..09713f6a
--- /dev/null
+++ b/devdocs/c/language%2Fstatements.html
@@ -0,0 +1,113 @@
+ <h1 id="firstHeading" class="firstHeading">Statements</h1> <p>Statements are fragments of the C program that are executed in sequence. The body of any function is a compound statement, which, in turn is a sequence of statements and declarations:</p>
+<div class="c source-c"><pre data-language="c">int main(void)
+{ // start of a compound statement
+ int n = 1; // declaration (not a statement)
+ n = n+1; // expression statement
+ printf("n = %d\n", n); // expression statement
+ return 0; // return statement
+} // end of compound statement, end of function body</pre></div> <p><br> There are five types of statements:</p>
+<div class="t-li1">
+<span class="t-li">1)</span> <a href="#Compound_statements">compound statements</a>
+</div> <div class="t-li1">
+<span class="t-li">2)</span> <a href="#Expression_statements">expression statements</a>
+</div> <div class="t-li1">
+<span class="t-li">3)</span> <a href="#Selection_statements">selection statements</a>
+</div> <div class="t-li1">
+<span class="t-li">4)</span> <a href="#Iteration_statements">iteration statements</a>
+</div> <div class="t-li1">
+<span class="t-li">5)</span> <a href="#Jump_statements">jump statements</a>
+</div> <table class="t-rev-begin"> <tr class="t-rev t-since-c23">
+<td> <p>An <a href="attributes" title="c/language/attributes">attribute specifier sequence</a> (<span class="t-spar">attr-spec-seq</span>) can be applied to an unlabeled statement, in which case (except for an expression statement) the attributes are applied to the respective statement.</p>
+</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
+</tr> </table> <h3 id="Labels"> Labels</h3> <p>Any statement can be <i>labeled</i>, by providing a name followed by a colon before the statement itself.</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <span class="t-spar">identifier</span> <code>:</code> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>case</code> <span class="t-spar">constant-expression</span> <code>:</code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>default</code> <code>:</code> </td> <td> (3) </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <div class="t-li1">
+<span class="t-li">1)</span> Target for <a href="goto" title="c/language/goto">goto</a>.</div> <div class="t-li1">
+<span class="t-li">2)</span> Case label in a <a href="switch" title="c/language/switch">switch</a> statement.</div> <div class="t-li1">
+<span class="t-li">3)</span> Default label in a <a href="switch" title="c/language/switch">switch</a> statement.</div> <p>Any statement (but not a declaration) may be preceded by any number of <i>labels</i>, each of which declares <span class="t-spar">identifier</span> to be a label name, which must be unique within the enclosing function (in other words, label names have <a href="scope" title="c/language/scope">function scope</a>).</p>
+<p>Label declaration has no effect on its own, does not alter the flow of control, or modify the behavior of the statement that follows in any way.</p>
+<table class="t-rev-begin"> <tr class="t-rev t-until-c23">
+<td> <p>A label shall be followed by a statement.</p>
+</td> <td><span class="t-mark-rev t-until-c23">(until C23)</span></td>
+</tr> <tr class="t-rev t-since-c23">
+<td> <p>A label can appear without its following statement. If a label appears alone in a block, it behaves as if it is followed by a <a href="#Expression_statements">null statement</a>.</p>
+<p>The optional <a href="attributes" title="c/language/attributes"><span class="t-spar">attr-spec-seq</span></a> is applied to the label.</p>
+</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
+</tr> </table> <h3 id="Compound_statements"> Compound statements</h3> <p>A compound statement, or <i>block</i>, is a brace-enclosed sequence of statements and declarations.</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>{</code> <span class="t-spar">statement</span> <code>|</code> <span class="t-spar">declaration</span>...<span class="t-mark">(optional)</span> <code>} </code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-until-c23">(until C23)</span> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>{</code> <span class="t-spar">unlabeled-statement</span> <code>|</code> <span class="t-spar">label</span> <code>|</code> <span class="t-spar">declaration</span>...<span class="t-mark">(optional)</span> <code>} </code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td>
+</tr>
+</table> <p>The compound statement allows a set of declarations and statements to be grouped into one unit that can be used anywhere a single statement is expected (for example, in an <a href="if" title="c/language/if">if</a> statement or an iteration statement):</p>
+<div class="c source-c"><pre data-language="c">if (expr) // start of if-statement
+{ // start of block
+ int n = 1; // declaration
+ printf("%d\n", n); // expression statement
+} // end of block, end of if-statement</pre></div> <p>Each compound statement introduces its own <a href="scope" title="c/language/scope">block scope</a>.</p>
+<p>The initializers of the variables with automatic <a href="storage_duration" title="c/language/storage duration">storage duration</a> declared inside a block and the VLA declarators are executed when flow of control passes over these declarations in order, as if they were statements:</p>
+<div class="c source-c"><pre data-language="c">int main(void)
+{ // start of block
+ { // start of block
+ puts("hello"); // expression statement
+ int n = printf("abc\n"); // declaration, prints "abc", stores 4 in n
+ int a[n*printf("1\n")]; // declaration, prints "1", allocates 8*sizeof(int)
+ printf("%zu\n", sizeof(a)); // expression statement
+ } // end of block, scope of n and a ends
+ int n = 7; // n can be reused
+}</pre></div> <h3 id="Expression_statements"> Expression statements</h3> <p>An expression followed by a semicolon is a statement.</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">expression</span><span class="t-mark">(optional)</span> <code>;</code> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span> <span class="t-spar">expression</span> <code>;</code> </td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td>
+</tr>
+</table> <p>Most statements in a typical C program are expression statements, such as assignments or function calls.</p>
+<p>An expression statement without an expression is called a <i>null statement</i>. It is often used to provide an empty body to a <a href="for" title="c/language/for">for</a> or <a href="while" title="c/language/while">while</a> loop. It can also be used to carry a label in the end of a compound statement or before a declaration:</p>
+<div class="c source-c"><pre data-language="c">puts("hello"); // expression statement
+char *s;
+while (*s++ != '\0')
+ ; // null statement</pre></div> <table class="t-rev-begin"> <tr class="t-rev t-since-c23">
+<td> <p>The optional <a href="attributes" title="c/language/attributes"><span class="t-spar">attr-spec-seq</span></a> is applied to the expression.</p>
+<p>An <span class="t-spar">attr-spec-seq</span> followed by <code>;</code> does not form an expression statement. It forms an <a href="declarations" title="c/language/declarations">attribute declaration</a> instead.</p>
+</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
+</tr> </table> <h3 id="Selection_statements"> Selection statements</h3> <p>The selection statements choose between one of several statements depending on the value of an expression.</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>if</code> <code>(</code> <span class="t-spar">expression</span> <code>)</code> <span class="t-spar">statement</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>if</code> <code>(</code> <span class="t-spar">expression</span> <code>)</code> <span class="t-spar">statement</span> <code>else</code> <span class="t-spar">statement</span> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>switch</code> <code>(</code> <span class="t-spar">expression</span> <code>)</code> <span class="t-spar">statement</span> </td> <td> (3) </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <div class="t-li1">
+<span class="t-li">1)</span> <a href="if" title="c/language/if">if</a> statement</div> <div class="t-li1">
+<span class="t-li">2)</span> <a href="if" title="c/language/if">if</a> statement with an else clause</div> <div class="t-li1">
+<span class="t-li">3)</span> <a href="switch" title="c/language/switch">switch</a> statement</div> <h3 id="Iteration_statements"> Iteration statements</h3> <p>The iteration statements repeatedly execute a statement.</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>while</code> <code>(</code> <span class="t-spar">expression</span> <code>)</code> <span class="t-spar">statement</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>do</code> <span class="t-spar">statement</span> <code>while</code> <code>(</code> <span class="t-spar">expression</span> <code>)</code> <code>;</code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>for</code> <code>(</code> <span class="t-spar">init-clause</span> <code>;</code> <span class="t-spar">expression</span><span class="t-mark">(optional)</span> <code>;</code> <span class="t-spar">expression</span><span class="t-mark">(optional)</span> <code>)</code> <span class="t-spar">statement</span> </td> <td> (3) </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <div class="t-li1">
+<span class="t-li">1)</span> <a href="while" title="c/language/while">while</a> loop</div> <div class="t-li1">
+<span class="t-li">2)</span> <a href="do" title="c/language/do">do-while</a> loop</div> <div class="t-li1">
+<span class="t-li">3)</span> <a href="for" title="c/language/for">for</a> loop</div> <h3 id="Jump_statements"> Jump statements</h3> <p>The jump statements unconditionally transfer flow control.</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>break</code> <code>;</code> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>continue</code> <code>;</code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>return</code> <span class="t-spar">expression</span><span class="t-mark">(optional)</span> <code>;</code> </td> <td> (3) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span><span class="t-mark-rev t-since-c23">(since C23)</span> <code>goto</code> <span class="t-spar">identifier</span> <code>;</code> </td> <td> (4) </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <div class="t-li1">
+<span class="t-li">1)</span> <a href="break" title="c/language/break">break</a> statement</div> <div class="t-li1">
+<span class="t-li">2)</span> <a href="continue" title="c/language/continue">continue</a> statement</div> <div class="t-li1">
+<span class="t-li">3)</span> <a href="return" title="c/language/return">return</a> statement with an optional expression</div> <div class="t-li1">
+<span class="t-li">4)</span> <a href="goto" title="c/language/goto">goto</a> statement</div> <h3 id="References"> References</h3> <ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul><li> 6.8 Statements and blocks (p: 106-112) </li></ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul><li> 6.8 Statements and blocks (p: 146-154) </li></ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 6.8 Statements and blocks (p: 131-139) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 3.6 STATEMENTS </li></ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/statements" title="cpp/language/statements">C++ documentation</a></span> for <span class=""><span>Statements</span></span> </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/language/statements" class="_attribution-link">https://en.cppreference.com/w/c/language/statements</a>
+ </p>
+</div>