This summary of C syntax is intended more for aiding comprehension than as an exact statement of the language.
Expressions
The basic expressions are:
expression:
primary
*
expression&lvalue - expression
! expression expression
++
lvalue --lvalue lvalue++
lvalue
--sizeof expression sizeof (type-name) ( type-name) expression expression binop expression
expression ? expression : expression lvalue asgnop expression
expression , expression
primary:
identifier constant string
( expression)
primary (expression-list t) primary [ expression J op primary. identifier primary -> identifier lvalue:
identifier
primary [ expression J lvalue . identifier primary -> identifier
*
expression ( lvalue)The primary-expression operators () [] . ->
have highest priority and group left to right. The unary operators
*
& - ! -++ --
sizeof (type-name)have priority below the primary operators but higher than any binary operator and group right to left. Binary operators group left to right; they have priority decreasing as indicated below.
binop:
* /
%+
» «
< > <= >=
&&
I II
!=
The conditional operator groups right to left.
Assignment operators all have the same priority and all group right to left.
asgnop:
+= -= *= /= %= » = «= &= 1=
The comma operator has the lowest priority and groups left to right.
Declarations
declaration:
decl-specifiers init-declarator-list t;
op decl-specifiers:
type-specifier decl-specifiers t sc-specifier decl-specifiers tOP
op
sc-specijier:
enum identifier { enum-list } enum identifier
init-declarator-list:
init-declarator
init-declarator , init-declarator-list init-declarator:
declarator initializer t op declarator:
identifier ( declarator )
*
declarator declarator ()declarator [constant-expression top
J
struct-or-union-specifier:struct { struct-decl-list }
struct identifier { struct-decl-list } struct identifier
union { struct-decl-list }
union identifier { struct-decl-list } union identifier
struct-decl-list:
struct-declaration
struct-declaration struct-decl-list struct-declaration:
type-specifier struct-declarator-list ; struct-declarator-list:
struct-declarator
struct-declarator, struct-declarator-list
struct-declarator:
declarator
declarator: constant-expression : constant-expression
initializer:
= expression
= { initializer-list }
= { initializer-list , } initializer-list:
expression
initializer-list , initializer-list { initializer-list }
{ initializer-list , } type-name:
type-specifier abstract-declarator abstract-declarator:
empty
( abstract-declarator )
*
abstract-declarator abstract-declarator ()abstract-declarator [constant-expression t ] op typedef-name:
identifier
Statements
compound-statement:
{declaration-list t statement-list t} op op
declaration-list:
declaration
declaration declaration-list statement-list:
statement
statement statement-list statement:
compound-statement expression ;
if ( expression) statement
if ( expression) statement else statement while ( expression) statement
do statement while ( expression) ; for (exp opt;eXp opt;exp op,) statement switch ( expression) statement case constant-expression: statement default: statement
break;
continue;
return;
return expression;
goto identifier;
identifier: statement
External definitions
program:
external-definition
external-definition program external-definition:
function-definition data-definition
function-definition:
decl-specifier t function-declarator function-body op
function-declarator:
declarator (parameter-list t ) op parameter-list:
identifier
identifier, parameter-list function-body:
declaration-list t compound-statement op
data-definition:
extern declaration;
static declaration;
Preprocessor
#define identifier token-stringo t
#define identifier ( identifier, ... ltoken-string t
#undef identifier op
#include "filename"
#include <filename>
#if restricted-constant-expression
#ifdef identifier
#ifndef identifier
#else
#endif
#line constant" filename"
C LIBRARIES
C LIBRARIES
GENERAL
This chapter and Chapter 4 describe the libraries that are supported on the UNIX operating system. A library is a collection of related functions and/or declarations that simplify programming effort by linking only what is needed, allowing use of locally produced functions, etc. All of the functions described are also described in Section 3 of the AT&T UNIX PC UNIX System V Manual. Most of the declarations described are in Section 5 of the AT&T UNIX PC UNIX System V Manual. The main libraries on the UNIX system are:
C library
Object file library
Math library
This is the basic library for C language programs. The C library is composed of functions and declarations used for file access, string testing and manipulation, character testing and manipulation, memory allocation, and other functions.
This library is described later in this chapter.
This library provides functions for the access and manipulation of object files.
This library is described in Chapter 4.
This library provides exponential, bessel functions, logarithmic, hyperbolic, and trigonometric functions. This library is described in Chapter 4.
tam library This library contains the AT&T UNIX PC "terminal access method" (tam) functions.
Some libraries consist of two portions-functions and declarations. In some cases, the user must request that the functions (and/or declarations) of a specific library be included in a program being compiled. In other cases, the functions (and/or declarations) are included automatically.
Including Functions
When a program is being compiled, the compiler will automatically search the C language library to locate and include functions that are used in the program. This is the case only for the C library and no other library. In order for the compiler to locate and include functions from other libraries, the user must specify these libraries on the command line for the compiler. For example, when using functions of the math library, the user must request that the math library be searched by including the argument -1m on the command line, such as:
cc file.c -1m
The argument -1m must come after all files that reference functions in the math library in order for the link editor to know which functions to include in the a.out file.
This method should be used for all functions that are not part of the C language library.
Including Declarations
Some functions require a set of declarations in order to operate properly. A set of declarations is stored in a file under the /usr/include directory. These files are referred to as header files. In order to include a certain header file, the user must specify this request within the C language program. The request is in the form:
#include <file.h>
w here file. h is the name of the file. Since the header files define the type of the functions and various preprocessor constants, they must be included before invoking the functions they declare.
The remainder of this chapter describes the functions and header files of the C Library. The description of the library begins with the actions required by the user to include the functions and/or header files in a program being compiled (if any). Following the description of the actions required is information in three-column format of the form:
function reference (N) Brief description.
The functions are grouped by type while the reference refers to section 'N' in the AT&T UNIX PC UNIX System V Manual.
Following this, are descriptions of the header files associated with these functions (if any).