Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Control structures== C is a [[free-form language]]. [[Indentation style|Bracing style]] varies from [[Computer programming|programmer]] to programmer and can be the subject of debate. See [[Indentation style]] for more details. ===Compound statements=== In the items in this section, any <statement> can be replaced with a '''compound statement'''. Compound statements have the form: <syntaxhighlight lang=C> { <optional-declaration-list> <optional-statement-list> } </syntaxhighlight> and are used as the body of a function or anywhere that a single statement is expected. The declaration-list declares variables to be used in that [[Scope (programming)|scope]], and the statement-list are the actions to be performed. Brackets define their own scope, and variables defined inside those brackets will be automatically deallocated at the closing bracket. Declarations and statements can be freely intermixed within a compound statement (as in [[C++]]). ===Selection statements=== C has two types of [[selection statement]]s: the [[If statement|{{code|if}} statement]] and the [[Switch statement|{{code|switch}} statement]]. The {{code|if}} statement is in the form: <syntaxhighlight lang=C> if (<expression>) <statement1> else <statement2> </syntaxhighlight> In the {{code|if}} statement, if the {{code|<expression>}} in parentheses is nonzero (true), control passes to {{code|<statement1>}}. If the {{code|else}} clause is present and the {{code|<expression>}} is zero (false), control will pass to {{code|<statement2>}}. The {{code|else <statement2>}} part is optional and, if absent, a false {{code|<expression>}} will simply result in skipping over the {{code|<statement1>}}. An {{code|else}} always matches the nearest previous unmatched {{code|if}}; braces may be used to override this when necessary, or for clarity. The {{code|switch}} statement causes control to be transferred to one of several statements depending on the value of an [[Expression (mathematics)|expression]], which must have [[integral type]]. The substatement controlled by a switch is typically compound. Any statement within the substatement may be labeled with one or more {{code|case}} labels, which consist of the keyword {{code|case}} followed by a constant expression and then a colon (:). The syntax is as follows: <syntaxhighlight lang=C> switch (<expression>) { case <label1> : <statements 1> case <label2> : <statements 2> break; default : <statements 3> } </syntaxhighlight> No two of the case constants associated with the same switch may have the same value. There may be at most one {{code|default}} label associated with a switch. If none of the case labels are equal to the expression in the parentheses following {{code|switch}}, control passes to the {{code|default}} label or, if there is no {{code|default}} label, execution resumes just beyond the entire construct. Switches may be nested; a {{code|case}} or {{code|default}} label is associated with the innermost {{code|switch}} that contains it. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a {{code|break;}} statement is encountered. Fall-through is useful in some circumstances, but is usually not desired. In the preceding example, if {{code|<label2>}} is reached, the statements {{code|<statements 2>}} are executed and nothing more inside the braces. However, if {{code|<label1>}} is reached, both {{code|<statements 1>}} and {{code|<statements 2>}} are executed since there is no {{code|break}} to separate the two case statements. It is possible, although unusual, to insert the {{code|switch}} labels into the sub-blocks of other control structures. Examples of this include [[Duff's device]] and [[Simon Tatham]]'s implementation of [[coroutine]]s in [[Putty (SSH)|Putty]].<ref>{{cite web | first=Simon | last=Tatham | author-link=Simon Tatham | date=2000 | url=https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html | title = Coroutines in C| access-date=2017-04-30}}</ref> ===Iteration statements=== C has three forms of [[iteration]] statement: <syntaxhighlight lang=C> do <statement> while ( <expression> ) ; while ( <expression> ) <statement> for ( <expression> ; <expression> ; <expression> ) <statement> </syntaxhighlight> In the [[while loop|{{code|while}}]] and {{code|do}} statements, the sub-statement is executed repeatedly so long as the value of the {{code|expression}} remains non-zero (equivalent to true). With {{code|while}}, the test, including all side effects from {{code|<expression>}}, occurs before each iteration (execution of {{code|<statement>}}); with {{code|do}}, the test occurs after each iteration. Thus, a {{code|do}} statement always executes its sub-statement at least once, whereas {{code|while}} may not execute the sub-statement at all. The statement: <syntaxhighlight lang=C> for (e1; e2; e3) s; </syntaxhighlight> is equivalent to: <syntaxhighlight lang=C> e1; while (e2) { s; cont: e3; } </syntaxhighlight> except for the behaviour of a {{code|continue;}} statement (which in the {{code|for}} loop jumps to {{code|e3}} instead of {{code|e2}}). If {{code|e2}} is blank, it would have to be replaced with a {{code|1}}. Any of the three expressions in the {{code|for}} loop may be omitted. A missing second expression makes the {{code|while}} test always non-zero, creating a potentially infinite loop. Since [[C99]], the first expression may take the form of a declaration, typically including an initializer, such as: <syntaxhighlight lang=C> for (int i = 0; i < limit; ++i) { // ... } </syntaxhighlight> The declaration's scope is limited to the extent of the {{code|for}} loop. ===Jump statements=== Jump statements transfer control unconditionally. There are four types of [[Branch (computer science)|jump statements]] in C: [[GOTO|{{code|goto}}]], {{code|continue}}, {{code|break}}, and [[return statement|{{code|return}}]]. The {{code|goto}} statement looks like this: <syntaxhighlight lang=C> goto <identifier> ; </syntaxhighlight> The [[identifier]] must be a [[label (programming language)|label]] (followed by a colon) located in the current function. Control transfers to the labeled statement. A {{code|continue}} statement may appear only within an [[C syntax#Iteration statements|iteration statement]] and causes control to pass to the loop-continuation portion of the innermost enclosing iteration statement. That is, within each of the statements <syntaxhighlight lang=C> while (expression) { /* ... */ cont: ; } do { /* ... */ cont: ; } while (expression); for (expr1; expr2; expr3) { /* ... */ cont: ; } </syntaxhighlight> a {{code|continue}} not contained within a nested iteration statement is the same as {{code|goto cont}}. The {{code|break}} statement is used to end a {{code|for}} loop, {{code|while}} loop, {{code|do}} loop, or {{code|switch}} statement. Control passes to the statement following the terminated statement. A function returns to its caller by the {{code|return}} statement. When {{code|return}} is followed by an expression, the value is returned to the caller as the value of the function. Encountering the end of the function is equivalent to a {{code|return}} with no expression. In that case, if the function is declared as returning a value and the caller tries to use the returned value, the result is undefined. ====Storing the address of a label==== [[GNU Compiler Collection|GCC]] extends the C language with a unary {{code|&&}} operator that returns the address of a label. This address can be stored in a {{code|void*}} variable type and may be used later in a {{code|goto}} instruction. For example, the following prints {{code|"hi "}} in an infinite loop: <syntaxhighlight lang=C> void *ptr = &&J1; J1: printf("hi "); goto *ptr; </syntaxhighlight> This feature can be used to implement a [[jump table]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)