Files
oldlinux-files/study/Ref-docs/C/syntax.html
2024-02-19 00:25:23 -05:00

484 lines
18 KiB
HTML

<HTML><HEAD><TITLE>Syntax</TITLE></HEAD><BODY BGCOLOR="#FFFFFF">
<H1><A NAME="Syntax">Syntax</A></H1><HR>
<P><B>
<A HREF="#C Tokens">C Tokens</A>
&#183; <A HREF="#Integer and Floating-Point Constants">Integer
and Floating-Point Constants</A>
&#183; <A HREF="#Character Constants and String Literals">Character
Constants and String Literals</A>
&#183; <A HREF="#Declaration Syntax">Declaration Syntax</A>
&#183; <A HREF="#Storage Class and Type Parts">Storage
Class and Type Parts</A>
&#183; <A HREF="#Declarators">Declarators</A>
&#183; <A HREF="#Object Initializers and Bitfield Specifications">Object
Initializers and Bitfield Specifications</A>
&#183; <A HREF="#Function Definition Syntax">Function Definition Syntax</A>
&#183; <A HREF="#Expression Syntax">Expression Syntax</A>
</B></P>
<HR>
<P>The final stage of preprocessing is to convert all remaining
preprocessing tokens in the translation unit to
<A HREF="#C Tokens">C tokens</A>. The translator
then parses these C tokens into one or more
<A HREF="declare.html#Declarations" tppabs="http://ccs.ucsd.edu/c/declare.html#Declarations">declarations</A>.
In particular:</P>
<UL>
<LI>Declarations that define
<B><A NAME="object">objects</A></B> specify the storage
for data that a program manipulates.</LI>
<LI>Declarations that are
<A HREF="function.html#Function Definitions" tppabs="http://ccs.ucsd.edu/c/function.html#Function Definitions">function definitions</A>
specify the actions that a program performs.</LI>
</UL>
<P>You use <A HREF="express.html#Expressions" tppabs="http://ccs.ucsd.edu/c/express.html#Expressions">expressions</A>
in declarations to specify values
to the translator or to specify the computations that the program
performs when it executes. This document shows the forms of all C tokens.
It also summarizes the syntax of declarations, function definitions,
and expressions. You use these syntactic forms, with
<A HREF="preproc.html#Preprocessing" tppabs="http://ccs.ucsd.edu/c/preproc.html#Preprocessing">preprocessing</A>
directives and macros, to write a C program.</P>
<H2><A NAME="C Tokens">C Tokens</A></H2>
<P>Each C token derives from a
<A HREF="preproc.html#Preprocessing Tokens" tppabs="http://ccs.ucsd.edu/c/preproc.html#Preprocessing Tokens">preprocessing token</A>.
Additional restrictions apply, however,
so not all preprocessing tokens form valid C tokens.
You must ensure that only valid C tokens remain in the
<A HREF="preproc.html#translation unit" tppabs="http://ccs.ucsd.edu/c/preproc.html#translation unit">translation unit</A>
after preprocessing.</P>
<P>Every preprocessing
<A HREF="preproc.html#name" tppabs="http://ccs.ucsd.edu/c/preproc.html#name">name</A> forms a valid C token.
Some of the names that you write are
<B><A NAME="keyword">keyword</A></B> C tokens (names that have
special meaning to the translator). The following Table
lists all defined keywords:</P>
<PRE>
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while</PRE>
<P>A <B><A NAME="name">name</A></B> C token
is a preprocessing name that is not a keyword:</P>
<P><IMG SRC="name.gif" tppabs="http://ccs.ucsd.edu/c/gif/name.gif"></P>
<P>You must ensure that
<A HREF="portable.html#distinct external names" tppabs="http://ccs.ucsd.edu/c/portable.html#distinct external names">distinct names</A> with
<A HREF="declare.html#external linkage" tppabs="http://ccs.ucsd.edu/c/declare.html#external linkage">external linkage</A>
differ within the first six characters,
even if the translator does not distinguish between
lowercase and uppercase letters when comparing names.</P>
<H3><A NAME="Integer and Floating-Point Constants">
Integer and Floating-Point Constants</A></H3>
<P>Every
<A HREF="preproc.html#preprocessing number" tppabs="http://ccs.ucsd.edu/c/preproc.html#preprocessing number">preprocessing number</A>
in the translation unit must be either an
<B><A NAME="integer constant">integer constant</A></B> or a
<B><A NAME="floating-point constant">floating-point constant</A></B>
C token. An integer constant is a preprocessing number
that represents a value of an integer type:</P>
<P><IMG SRC="int_con.gif" tppabs="http://ccs.ucsd.edu/c/gif/int_con.gif"></P>
<P>The value of an integer constant depends on its form:</P>
<UL>
<LI>A leading <CODE>0x</CODE> or <CODE>0X</CODE> indicates a
<A HREF="express.html#hexadecimal integer constant" tppabs="http://ccs.ucsd.edu/c/express.html#hexadecimal integer constant">hexadecimal</A>
(base 16) integer.
<LI>A leading <CODE>0</CODE> indicates an
<A HREF="express.html#octal integer constant" tppabs="http://ccs.ucsd.edu/c/express.html#octal integer constant">octal</A>
(base 8) integer.
<LI>A leading nonzero digit indicates a
<A HREF="express.html#decimal integer constant" tppabs="http://ccs.ucsd.edu/c/express.html#decimal integer constant">decimal</A>
(base 10) integer.
</UL>
<P>You write any combination of:</P>
<UL>
<LI>at most one <CODE>l</CODE> or <CODE>L</CODE> suffix
to indicate a <I>long</I> type</LI>
<LI>at most one <CODE>u</CODE> or <CODE>U</CODE> suffix
to indicate an <I>unsigned</I> type</LI>
</UL>
<P>A floating-point constant is a preprocessing number that represents a
<A HREF="#floating-point constant">value</A>
of a floating-point type.
You write either a decimal point, an exponent, or both to distinguish
a floating-point constant from an integer constant:
<P><IMG SRC="flt_con.gif" tppabs="http://ccs.ucsd.edu/c/gif/flt_con.gif"></P>
<P>You write at most one <CODE>f</CODE> or <CODE>F</CODE> suffix
to indicate type <I>float,</I>
or at most one <CODE>l</CODE> or <CODE>L</CODE> suffix
to indicate type <I>long double.</I></P>
<H3><A NAME="Character Constants and String Literals">
Character Constants and String Literals</A></H3>
<P>A <B><A NAME="character constant">character constant</A></B>
C token has the same form as a preprocessing
<A HREF="preproc.html#character constant" tppabs="http://ccs.ucsd.edu/c/preproc.html#character constant">character constant</A>:</P>
<P><IMG SRC="char_con.gif" tppabs="http://ccs.ucsd.edu/c/gif/char_con.gif"></P>
<P>Its <A HREF="express.html#character constant" tppabs="http://ccs.ucsd.edu/c/express.html#character constant">value</A>
depends on the character(s) you specify and any prefix you write.</P>
<P>A <B><A NAME="string literal">string literal</A></B>
C token has the same form as a preprocessing
<A HREF="preproc.html#string literal" tppabs="http://ccs.ucsd.edu/c/preproc.html#string literal">string literal</A>:</P>
<P><IMG SRC="str_lit.gif" tppabs="http://ccs.ucsd.edu/c/gif/str_lit.gif"></P>
<P>Its <A HREF="express.html#string literal" tppabs="http://ccs.ucsd.edu/c/express.html#string literal">value</A>
depends on the character(s) you specify and any prefix you write.</P>
<P>An <B><A HREF="charset.html#Escape Sequences" tppabs="http://ccs.ucsd.edu/c/charset.html#Escape Sequences">escape sequence</A></B>
has the same form as within a preprocessing
character constant or string literal:</P>
<P><IMG SRC="escape.gif" tppabs="http://ccs.ucsd.edu/c/gif/escape.gif"></P>
<P>An <B><A NAME="operator token">operator</A></B> or
<B><A NAME="punctuator token">punctuator</A></B> token
has the same form as a preprocessing
<A HREF="preproc.html#operator token" tppabs="http://ccs.ucsd.edu/c/preproc.html#operator token">operator</A> or
<A HREF="preproc.html#punctuator token" tppabs="http://ccs.ucsd.edu/c/preproc.html#punctuator token">punctuator</A>,
except that the tokens
<CODE>#</CODE> and <CODE>##</CODE> (and <CODE>%:</CODE> and <CODE>%:%:</CODE>, with
<A HREF="lib_over.html#Amendment 1" tppabs="http://ccs.ucsd.edu/c/lib_over.html#Amendment 1">Amendment 1</A>)
have meaning only during preprocessing. Moreover, the remaining
Amendment 1 additions map to other C tokens:</P>
<UL>
<LI><CODE>&lt;:</CODE> becomes <CODE>[</CODE>
<LI><CODE>:&gt;</CODE> becomes <CODE>]</CODE>
<LI><CODE>&lt;%</CODE> becomes <CODE>{</CODE>
<LI><CODE>%&gt;</CODE> becomes <CODE>}</CODE>
</UL>
<P>The following table shows all remaining operators and
punctuators:</P>
<PRE>
... &amp;&amp; -= &gt;= ~ + ; ]
&lt;&lt;= &amp;= -&gt; &gt;&gt; % , &lt; ^
&gt;&gt;= *= /= ^= &amp; - = {
!= ++ &lt;&lt; |= ( . &gt; |
%= += &lt;= || ) / ? }
-- == ! * : [</PRE>
<H2><A NAME="Declaration Syntax">Declaration Syntax</A></H2>
<P>The translator parses all C tokens that constitute a translation unit:</P>
<P><IMG SRC="tr_unit.gif" tppabs="http://ccs.ucsd.edu/c/gif/tr_unit.gif"></P>
<P>as one or more
<B><A HREF="declare.html#Declarations" tppabs="http://ccs.ucsd.edu/c/declare.html#Declarations">declarations</A></B>,
some of which are
<B><A HREF="#Function Definition Syntax">function definitions</A></B>.
A declaration (other than a function definition)
takes a variety of forms:</P>
<P><IMG SRC="decl.gif" tppabs="http://ccs.ucsd.edu/c/gif/decl.gif"></P>
<P>Declarations can contain other declarations. You cannot write
a function definition inside another declaration, however.
There are many contexts for declarations.
Some forms of declarations are permitted only in certain
<A HREF="declare.html#Declaration Contexts and Levels" tppabs="http://ccs.ucsd.edu/c/declare.html#Declaration Contexts and Levels">contexts</A>.</P>
<H3><A NAME="Storage Class and Type Parts">Storage
Class and Type Parts</A></H3>
<P>You begin a declaration with an optional
<B><A NAME="storage class">storage class</A></B> keyword,
intermixed with zero or more
<B><A NAME="type part">type parts</A></B>:</P>
<P><IMG SRC="type_pt.gif" tppabs="http://ccs.ucsd.edu/c/gif/type_pt.gif"></P>
<P>The storage class keyword is from the set:</P>
<PRE>
auto extern register static typedef</PRE>
<P>You write a type part as any one of the following.</P>
<UL>
<LI>a type qualifier keyword, from the set:
</UL>
<PRE>
const volatile</PRE>
<UL>
<LI>a type specifier keyword, from the set:
</UL>
<PRE>
char double float int long
short signed unsigned void</PRE>
<UL>
<LI>a structure, union, or enumeration specification
<LI>a type definition name
</UL>
<P>You can write only certain
<A HREF="types.html" tppabs="http://ccs.ucsd.edu/c/types.html">combinations</A> of type parts.</P>
<H3><A NAME="Declarators">Declarators</A></H3>
<P>You can follow the storage class and type part of a declaration
with a list of <B>declarators</B> separated by commas. Each declarator
can specify a name for the entity that you are declaring as well as
additional type information:</P>
<P><IMG SRC="decl_tor.gif" tppabs="http://ccs.ucsd.edu/c/gif/decl_tor.gif"></P>
<P>You write a declarator as, in order:</P>
<P>1. zero or more
<B><A NAME="pointer decoration">pointer decorations</A></B></P>
<P>2. an optional name or a declarator in parentheses</P>
<P>3. zero or more
<B><A NAME="array decoration">array decorations</A></B>
or at most one
<B><A NAME="function decoration">function decorations</A></B></P>
<P>A pointer decoration consists of an asterisk (<CODE>*</CODE>) followed
by an optional list of type qualifier keywords:</P>
<P><IMG SRC="ptr_mod.gif" tppabs="http://ccs.ucsd.edu/c/gif/ptr_mod.gif"></P>
<P>An array decoration consists of an optional expression enclosed
in brackets (<CODE>[]</CODE>):</P>
<P><IMG SRC="arr_mod.gif" tppabs="http://ccs.ucsd.edu/c/gif/arr_mod.gif"></P>
<P>A function decoration is a sequence of one of the following:</P>
<UL>
<LI>zero or more parameter names</LI>
<LI>one or more parameter declarations</LI>
</UL>
<P>In either sequence, the parameters are separated by commas and
enclosed in parentheses:</P>
<P><IMG SRC="fun_mod.gif" tppabs="http://ccs.ucsd.edu/c/gif/fun_mod.gif"></P>
<P>Some of these forms are permitted in certain
<A HREF="function.html" tppabs="http://ccs.ucsd.edu/c/function.html">contexts</A> and not in others.</P>
<H3><A NAME="Object Initializers and Bitfield Specifications">Object
Initializers and Bitfield Specifications</A></H3>
<P>You can follow each declarator with one of the following:</P>
<UL>
<LI>an optional object initializer, consisting of an equal sign
(<CODE>=</CODE>) followed by a value</LI>
<LI>an optional bitfield size, consisting of a colon (<CODE>:</CODE>)
followed by an expression</LI>
</UL>
<P>You write an
<A HREF="declare.html#Object Initializers" tppabs="http://ccs.ucsd.edu/c/declare.html#Object Initializers">object initializer</A>
<B><A NAME="value">value</A></B> as either an expression
or a list of such values separated by commas and enclosed in braces
<CODE>{}</CODE>:</P>
<P><IMG SRC="value.gif" tppabs="http://ccs.ucsd.edu/c/gif/value.gif"></P>
<P>You can write a trailing comma after the last value in a comma
separated list of object initializers.</P>
<H2><A NAME="Function Definition Syntax">Function Definition Syntax</A></H2>
<P>A function definition declares a function and specifies the
actions it performs when it executes:</P>
<P><IMG SRC="fun_def.gif" tppabs="http://ccs.ucsd.edu/c/gif/fun_def.gif"></P>
<P>You write a function definition as, in order:</P>
<P>1. an optional
<A HREF="#storage class">storage class</A> and
<A HREF="#type part">type parts</A></P>
<P>2. a <A HREF="#Declarators">declarator</A></P>
<P>3. zero or more parameter declarations each terminated by a semicolon</P>
<P>4. a <B><A NAME="block">block</A></B></P>
<P>The declarator contains a function decoration that describes
the parameters to the function. You can write parameter declarations
before the block only if the function decoration contains a list of
parameter names.</P>
<P>A block consists of braces surrounding, in order:</P>
<P>1. zero or more declarations each terminated by a semicolon</P>
<P>2. zero or more statements</P>
<P><IMG SRC="block.gif" tppabs="http://ccs.ucsd.edu/c/gif/block.gif"></P>
<P>A block contains a sequence of
<A HREF="function.html#Statements" tppabs="http://ccs.ucsd.edu/c/function.html#Statements">statements</A> that specifies
the actions performed by the block when it executes:</P>
<P><IMG SRC="statment.gif" tppabs="http://ccs.ucsd.edu/c/gif/statment.gif"></P>
<P>Here, <CODE><I>opt</I></CODE> represents an optional expression:</P>
<P><IMG SRC="opt.gif" tppabs="http://ccs.ucsd.edu/c/gif/opt.gif"></P>
<P>Statements specify the flow of control through a function when
it executes. A statement that contains expressions also computes values
and alters the values stored in objects when the statement executes.</P>
<H2><A NAME="Expression Syntax">Expression Syntax</A></H2>
<P>You use expressions to specify values to the translator or to
specify the computations that a program performs when it executes.</P>
<UL>
<LI>You write an expression as one or more
<B><A NAME="term">terms</A></B> separated by
<B><A NAME="infix operator">infix operators</A></B>:</LI>
<LI>Each term is preceded by zero or more
<B><A NAME="prefix operator">prefix operators</A></B>.
Each term is followed by zero or more
<B><A NAME="postfix operator">postfix operators</A></B></LI>
</UL>
<P><IMG SRC="expr.gif" tppabs="http://ccs.ucsd.edu/c/gif/expr.gif"></P>
<P>Only certain
<A HREF="express.html" tppabs="http://ccs.ucsd.edu/c/express.html">combinations</A> of
operators and terms form valid expressions.</P>
<P>You write a term as one of the following:</P>
<UL>
<LI>a name that is declared as a function, object, or enumeration
constant
<LI>an <A HREF="#integer constant">integer constant</A>
<LI>a <A HREF="#floating-point constant">floating-point constant</A>
<LI>a <A HREF="#character constant">character constant</A>
<LI>a <A HREF="#string literal">string literal</A>
<LI>the
<A HREF="express.html#sizeof operator" tppabs="http://ccs.ucsd.edu/c/express.html#sizeof operator"><I>sizeof</I></A> operator
followed by a declaration enclosed in parentheses
<LI>an expression enclosed in parentheses
</UL>
<P><IMG SRC="term.gif" tppabs="http://ccs.ucsd.edu/c/gif/term.gif"></P>
<P>You write an infix operator as one of the following:</P>
<UL>
<LI>one of the infix operator tokens
<LI>the conditional operator pair <CODE>?</CODE> <CODE>:</CODE>
enclosing another expression
</UL>
<P><IMG SRC="infix.gif" tppabs="http://ccs.ucsd.edu/c/gif/infix.gif"></P>
<P>You write a prefix operator as one of the following:</P>
<UL>
<LI>the keyword <CODE>sizeof</CODE></LI>
<LI>one of the prefix operator tokens</LI>
<LI>a <A HREF="express.html#Type Cast" tppabs="http://ccs.ucsd.edu/c/express.html#Type Cast"><I>type cast</I></A> operator
(consisting of a declaration enclosed in parentheses)</LI>
</UL>
<P><IMG SRC="prefix.gif" tppabs="http://ccs.ucsd.edu/c/gif/prefix.gif"></P>
<P>You can write only certain
<A HREF="declare.html#Declaration Contexts and Levels" tppabs="http://ccs.ucsd.edu/c/declare.html#Declaration Contexts and Levels">forms</A>
of declarations in a type cast.</P>
<P>You write a postfix operator as one of the following:</P>
<UL>
<LI>the postfix operator token <CODE>++</CODE>
<LI>the postfix operator token <CODE>--</CODE>
<LI>an array subscript expression, enclosed in brackets <CODE>[]</CODE>
<LI>a function call argument expression list, enclosed in parentheses
<CODE>()</CODE>
<LI>the member selection operator (a period), followed by the name
of a structure or union member
<LI>the member selection operator <CODE>-&gt;</CODE>, followed by the name
of a structure or union member
</UL>
<P><IMG SRC="postfix.gif" tppabs="http://ccs.ucsd.edu/c/gif/postfix.gif"></P>
<P>You can write only certain
<A HREF="express.html" tppabs="http://ccs.ucsd.edu/c/express.html">forms</A> of expressions in some contexts.</P>
<HR>
<P>See also the
<B><A HREF="index.html#Table of Contents" tppabs="http://ccs.ucsd.edu/c/index.html#Table of Contents">Table of Contents</A></B> and the
<B><A HREF="_index.html" tppabs="http://ccs.ucsd.edu/c/_index.html">Index</A></B>.</P>
<P><I>
<A HREF="crit_pb.html" tppabs="http://ccs.ucsd.edu/c/crit_pb.html">Copyright</A> &#169; 1989-1996
by P.J. Plauger and Jim Brodie. All rights reserved.</I></P>
</BODY></HTML>