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 (programming language)
(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!
== Syntax == {{Main|C syntax}} C has a [[formal grammar]] specified by the C standard.<ref name="h&s5e">{{cite book |last1=Harbison |first1=Samuel P. |last2=Steele |first2=Guy L. |author-link2=Guy L. Steele, Jr. |title=C: A Reference Manual |edition=5th |publisher=[[Prentice Hall]] |year=2002 |location=[[Englewood Cliffs, NJ]] |isbn=978-0-13-089592-9}} Contains a [[Backus-Naur form|BNF]] grammar for C.</ref> Line endings are generally not significant in C; however, line boundaries do have significance during the preprocessing phase. Comments may appear either between the delimiters <code>/*</code> and <code>*/</code>, or (since C99) following <code>//</code> until the end of the line. Comments delimited by <code>/*</code> and <code>*/</code> do not nest, and these sequences of characters are not interpreted as comment delimiters if they appear inside [[String literal|string]] or character literals.{{sfnp|Kernighan|Ritchie|1988|p=192}} C source files contain declarations and function definitions. Function definitions, in turn, contain declarations and [[Statement (computer science)|statements]]. Declarations either define new types using keywords such as <code>struct</code>, <code>union</code>, and <code>enum</code>, or assign types to and perhaps reserve storage for new variables, usually by writing the type followed by the variable name. Keywords such as <code>char</code> and <code>int</code> specify built-in types. Sections of code are enclosed in braces (<code>{</code> and <code>}</code>, sometimes called "curly brackets") to limit the scope of declarations and to act as a single statement for control structures. As an imperative language, C uses ''statements'' to specify actions. The most common statement is an ''expression statement'', consisting of an expression to be evaluated, followed by a semicolon; as a [[Side effect (computer science)|side effect]] of the evaluation, [[Function (computer programming)|functions may be called]] and [[Assignment (computer science)|variables assigned]] new values. To modify the normal sequential execution of statements, C provides several control-flow statements identified by reserved keywords. [[Structured programming]] is supported by <code>if</code> ... [<code>else</code>] conditional execution and by <code>do</code> ... <code>while</code>, <code>while</code>, and <code>for</code> iterative execution (looping). The <code>for</code> statement has separate initialization, testing, and reinitialization expressions, any or all of which can be omitted. <code>break</code> and <code>continue</code> can be used within the loop. Break is used to leave the innermost enclosing loop statement and continue is used to skip to its reinitialisation. There is also a non-structured <code>[[goto]]</code> statement which branches directly to the designated [[Label (computer science)|label]] within the function. <code>[[Switch statement|switch]]</code> selects a <code>case</code> to be executed based on the value of an integer expression. Different from many other languages, control-flow will [[Switch statement#Fallthrough|fall through]] to the next <code>case</code> unless terminated by a <code>break</code>. Expressions can use a variety of built-in operators and may contain function calls. The order in which arguments to functions and operands to most operators are evaluated is unspecified. The evaluations may even be interleaved. However, all side effects (including storage to variables) will occur before the next "[[sequence point]]"; sequence points include the end of each expression statement, and the entry to and return from each function call. Sequence points also occur during evaluation of expressions containing certain operators (<code>&&</code>, <code>||</code>, <code>[[?:]]</code> and the [[comma operator]]). This permits a high degree of object code optimization by the compiler, but requires C programmers to take more care to obtain reliable results than is needed for other programming languages. Kernighan and Ritchie say in the Introduction of ''The C Programming Language'': "C, like any other language, has its blemishes. Some of the operators have the wrong precedence; some parts of the syntax could be better."{{sfnp|Kernighan|Ritchie|1978|p=3}} The C standard did not attempt to correct many of these blemishes, because of the impact of such changes on already existing software. === Character set === The basic C source character set includes the following characters: * Lowercase and uppercase letters of the [[ISO basic Latin alphabet]]: <code>a</code>β<code>z</code>, <code>A</code>β<code>Z</code> * Decimal digits: <code>0</code>β<code>9</code> * Graphic characters: <code>! " # % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~</code> * [[Whitespace character]]s: ''[[Space (punctuation)|space]]'', ''[[horizontal tab]]'', ''[[vertical tab]]'', ''[[form feed]]'', ''[[newline]]'' The ''newline'' character indicates the end of a text line; it need not correspond to an actual single character, although for convenience C treats it as such. Additional multi-byte encoded characters may be used in [[string literal]]s, but they are not entirely [[Software portability|portable]]. Since [[C99]] multi-national Unicode characters can be embedded portably within C source text by using <code>\uXXXX</code> or <code>\UXXXXXXXX</code> encoding (where <code>X</code> denotes a hexadecimal character). The basic C execution character set contains the same characters, along with representations for [[Bell character|alert]], [[backspace]], and [[carriage return]]. [[Run time (program lifecycle phase)|Run-time]] support for extended character sets has increased with each revision of the C standard. === Reserved words === The following reserved words are [[case sensitive]]. C89 has 32 reserved words, also known as 'keywords', which cannot be used for any purposes other than those for which they are predefined: {{div col|colwidth=13em}} * <code>auto</code> * <code>[[Break statement|break]]</code> * <code>case</code> * <code>char</code> * <code>[[const]]</code> * <code>[[Continue (keyword)|continue]]</code> * <code>default</code> * <code>do</code> * <code>[[Double-precision floating-point format|double]]</code> * <code>[[Conditional (computer programming)|else]]</code> * <code>[[Enumerated type|enum]]</code> * <code>[[extern]]</code> * <code>[[Floating-point arithmetic|float]]</code> * <code>[[For loop|for]]</code> * <code>[[goto]]</code> * <code>[[Conditional (computer programming)|if]]</code> * <code>[[Integer (computer science)|int]]</code> * <code>[[Long integer|long]]</code> * <code>[[Register (keyword)|register]]</code> * <code>[[Return statement|return]]</code> * <code>[[Short integer|short]]</code> * <code>[[Signed number representations|signed]]</code> * <code>[[sizeof]]</code> * <code>[[Static (keyword)|static]]</code> * <code>[[Struct (C programming language)|struct]]</code> * <code>[[Switch statement|switch]]</code> * <code>[[typedef]]</code> * <code>[[Union type|union]]</code> * <code>[[Signed number representations|unsigned]]</code> * <code>[[Void type|void]]</code> * <code>[[Volatile variable|volatile]]</code> * <code>[[While loop|while]]</code> {{div col end}} C99 added five more reserved words: (β‘ indicates an alternative spelling alias for a C23 keyword) {{div col|colwidth=13em}} * <code>[[Inline function|inline]]</code> * <code>[[restrict]]</code> * <code>_Bool</code> β‘ * <code>[[Complex data type|_Complex]]</code> * <code>[[Complex data type|_Imaginary]]</code> {{div col end}} C11 added seven more reserved words:<ref name="ISOIEC 9899">{{Cite web|url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf|title=ISO/IEC 9899:201x (ISO C11) Committee Draft|website=open-std.org|date=December 2, 2010|access-date=September 16, 2011|archive-date=December 22, 2017|archive-url=https://web.archive.org/web/20171222215122/http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf|url-status=live}}</ref> (β‘ indicates an alternative spelling alias for a C23 keyword) {{div col|colwidth=13em}} * <code>_Alignas</code> β‘ * <code>_Alignof</code> β‘ * <code>_Atomic</code> * <code>_Generic</code> * <code>_Noreturn</code> * <code>_Static_assert</code> β‘ * <code>_Thread_local</code> β‘ {{div col end}} C23 reserved fifteen more words: {{div col|colwidth=13em}} * <code>alignas</code> * <code>alignof</code> * <code>bool</code> * <code>constexpr</code> * <code>false</code> * <code>nullptr</code> * <code>static_assert</code> * <code>thread_local</code> * <code>true</code> * <code>typeof</code> * <code>typeof_unqual</code> * <code>_BitInt</code> * <code>_Decimal32</code> * <code>_Decimal64</code> * <code>_Decimal128</code> {{div col end}} Most of the recently reserved words begin with an underscore followed by a capital letter, because identifiers of that form were previously reserved by the C standard for use only by implementations. Since existing program source code should not have been using these identifiers, it would not be affected when C implementations started supporting these extensions to the programming language. Some standard headers do define more convenient synonyms for underscored identifiers. Some of those words were added as keywords with their conventional spelling in C23 and the corresponding macros were removed. Prior to C89, <code>entry</code> was reserved as a keyword. In the second edition of their book ''[[The C Programming Language]]'', which describes what became known as C89, Kernighan and Ritchie wrote, "The ... [keyword] <code>entry</code>, formerly reserved but never used, is no longer reserved." and "The stillborn <code>entry</code> keyword is withdrawn."{{sfnp|Kernighan|Ritchie|1988|pp=192, 259}} === Operators === {{Main|Operators in C and C++}} C supports a rich set of [[Operator (computer programming)|operators]], which are symbols used within an [[Expression (computer science)|expression]] to specify the manipulations to be performed while evaluating that expression. C has operators for: * [[arithmetic]]: [[Addition|<code>+</code>]], [[Subtraction|<code>-</code>]], [[Multiplication|<code>*</code>]], [[Division (mathematics)|<code>/</code>]], [[Modulo operation|<code>%</code>]] * [[Assignment (computer science)|assignment]]: <code>=</code> * [[augmented assignment]]: {{codes|+{{=}}|-{{=}}|*{{=}}|/{{=}}|%{{=}}|&{{=}}|{{!}}{{=}}|^{{=}}|<<{{=}}|>>{{=}}|d=,{{space}}}} * [[bitwise logic]]: <code>~</code>, <code>&</code>, <code>|</code>, <code>^</code> * [[bitwise shift]]s: <code><<</code>, <code>>></code> * [[Boolean logic]]: <code>!</code>, <code>&&</code>, <code>||</code> * [[?:|conditional evaluation]]: [[?:|<code>? :</code>]] * equality testing: [[Equality (mathematics)|<code>==</code>]], [[Inequality (mathematics)|<code>!=</code>]] * [[Subroutine|calling functions]]: <code>( )</code> * [[Increment and decrement operators|increment and decrement]]: <code>++</code>, <code>--</code> * [[Record (computer science)|member selection]]: <code>.</code>, <code>-></code> * object size: <code>[[sizeof]]</code> * type: <code>[[typeof]]</code>, <code>typeof_unqual</code> ''since C23'' * [[order relation]]s: <code><</code>, <code><=</code>, <code>></code>, <code>>=</code> * [[Pointer (computer programming)|reference and dereference]]: <code>&</code>, <code>*</code>, <code>[ ]</code> * sequencing: [[Comma operator|<code>,</code>]] * [[Order of operations#Programming languages|subexpression grouping]]: <code>( )</code> * [[type conversion]]: <code>(''typename'')</code> C uses the operator <code>=</code> (used in mathematics to express equality) to indicate assignment, following the precedent of [[Fortran]] and [[PL/I]], but unlike [[ALGOL]] and its derivatives. C uses the operator <code>==</code> to test for equality. The similarity between the operators for assignment and equality may result in the accidental use of one in place of the other, and in many cases the mistake does not produce an error message (although some compilers produce warnings). For example, the conditional expression <code>if (a == b + 1)</code> might mistakenly be written as <code>if (a = b + 1)</code>, which will be evaluated as <code>true</code> unless the value of <code>a</code> is <code>0</code> after the assignment.<ref name="AutoTX-8">{{cite web |url=http://www.cs.ucr.edu/~nxiao/cs10/errors.htm |title=10 Common Programming Mistakes in C++ |website=Cs.ucr.edu |access-date=June 26, 2009 |archive-date=October 21, 2008 |archive-url=https://web.archive.org/web/20081021080953/http://www.cs.ucr.edu/~nxiao/cs10/errors.htm |url-status=live }}</ref> The C [[operator precedence]] is not always intuitive. For example, the operator <code>==</code> binds more tightly than (is executed prior to) the operators <code>&</code> (bitwise AND) and <code>|</code> (bitwise OR) in expressions such as <code>x & 1 == 0</code>, which must be written as <code>(x & 1) == 0</code> if that is the coder's intent.<ref name="AutoTX-9">{{cite book |title=C and the 8051 |edition=3rd |last1=Schultz |first1=Thomas |year=2004 |publisher=PageFree Publishing Inc. |location=Otsego, MI |isbn=978-1-58961-237-2 |page=20 |url={{GBurl|id=rI0c8kWbxooC|pg=PT47}} |access-date=February 10, 2012 }}</ref>
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)