Apache Awk Bash C cftp daemontools DHCP djbdns DNS Emacs Email ezmlm Fetchmail find GDB Hardware HTML HTTP Intro ISDN less Make Math mc mirrordir MySQL Peripherals Perl PHP3 pppd qmail Regexps Shell System Tables test To do Typical ucspi-tcp Versions Index TCP/IP slides

C syntax notes

struct [struct-type-name]
    [type variable-names] ;
  } [structure-variables];
union [union-type-name]
    [type variable-names] ;
  } [union-variables];
else if(expression)
  case const-expr:
  case const-expr:


Example Notes
int intarray[]={ 1, 2, 3, 5, 8, 13, 21 }; The dimension of the array is obtained from the number of initializers
char astring[]={ 'e', 'x', 'p', 'l', 'i', 'c', 'i', 't', ' ',
't', 'e', 'r', 'm', 'i', 'n', 'a', 't', 'o', 'r', 0 };
char bstring[]="no explicit terminator"; The dimension of the character array is given by the number of characters in the string, plus one for the terminating NULL
char *colors[]={ "Black", "Red", "Green", "Blue", "White" }; Each member of the array has a initializer as per above examples
struct {
   int ival;
   char *sval;
   char cval;
} astruct = { 1234, "example", 'x' };
Struct initialization is equivalent to one initializer per structure member. Unions only accep initialization for the first member
int bidim[2][3] = {
   { 23, 45, 98 },
   { 19, 33, 44 }
Bidimensional arrays are initialized in the order of the first dimension, which must be explicit (the second can be ommited, being obtained from the number of elements in ecah sub-initializer)
struct {
   int ival;
   char *sval;
} sarr[]= {
   { 14, "one" },
   { 23, "two" },
   { 44, "three" }
Initializing an array of struct is initializing each member

C usages

Self referential structures

struct node
    char *item;
    struct node *next;

Operator precedence

  Symbol Name Associativity Class
() Subexpression grouping and function call Left to right Primary expressions and postfix operators
[ ] Array subscripting
-> Struct pointer
. Struct member
++ Postfix increment
-- Postfix decrement
  ! Logical negation Right to left Unary operators
~ One's complement
++ Prefix increment
-- Prefix decrement
- Unary negation
+ Unary plus
(type) Type casting
* Pointer indirection
& Address of ...
sizeof Size of ...
  * Multiplication Left to right Multiplicative operators
/ Division
% Modulus or remainder
  + Addition Left to right Additive operators
- Subtraction
  << Bitwise left shift Left to right Shift operators (bitwise)
>> Bitwise right shift
  < Less than ... Left to right Relational operators
<= Less than or equal to ...
> Greater than ...
>= Greater than or equal to ...
  == Equality test Left to right Equality operators
!= Inequality test
  & Bitwise AND Left to right Bitwise operators
  ^ Bitwise XOR Left to right Bitwise operators
  | Bitwise OR Left to right Bitwise operators
  && Logical AND Left to right Logical operators
  || Logical OR Left to right Logical operators
  ?: Conditional test Right to left Conditional operators
  = Assignment Right to left Assignment operators
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Extract remainder and assign
<<= Bitwise left shift and assign
>>= Bitwise right shift and assign
&= Bitwise AND and assign
^= Bitwise XOR and assign
|= Bitwise OR and assign
, Comma operator Left to right Sequence operators
Last update: Wed, 2 Nov 2005 10:16:21 GMT