@c ??? It might be a good idea to turn each example of an expression
@c into a small program that prints output and thus shows what
@c the expression does.

@c This is part of The GNU C Reference Manual
@c Copyright (C) 2007-2009 Free Software Foundation, Inc.
@c See the file gnu-c-manual.texi for copying conditions.

@node Expressions and Operators
@chapter Expressions and Operators

@menu
* Expressions::
* Unary Operators::
* Binary Operators::
* Member Access Expressions::
* Conditional Expressions::
* Statements and Declarations in Expressions::
* Operator Precedence::
* Order of Evaluation::
@c * Overflow::
@end menu

@node Expressions
@section Expressions
@cindex expressions

An @dfn{expression} consists of at least one operand and zero or more
operators.  The operands may be any value, including constants, variables,
and function calls that return values.  Here are some examples:

@example
@group
47
2 + 2
function()
@end group
@end example

@noindent
The last of those, @code{function()}, is only an expression if
@code{function()} has a return type other than @code{void}.

You can use parentheses to group subexpressions:

@example
( 2 * ( ( 3 + 10 ) - ( 2 * 6 ) ) )
@end example

@noindent
Innermost expressions are evaluated first.  In the above example,
@code{3 + 10} and @code{2 * 6} evaluate to @code{13} and @code{12},
respectively.  Then @code{12} is subtracted
from @code{13}, resulting in @code{1}.  Finally, @code{1} is multiplied by
@code{2}, resulting in @code{2}.  The outermost parentheses are completely
optional.

@cindex operators

An @dfn{operator} specifies an operation to be performed on its operand(s).
Operators may have one, two, or three operands, depending on the operator.

@node Unary Operators
@section Unary Operators
@cindex unary operators
@cindex operators, unary

Unary operators perform an operation on a single operand.

@menu
* The Increment Operator::
* The Decrement Operator::
* The Positive Operator::
* The Negative Operator::
* The Logical Negation Operator::
* The Bitwise Complement Operator::
* The Address Operator::
* The Label Address Operator::
* The Indirection Operator::
* The Complex Conjugation Operator::
* The sizeof Operator::
* Type Casts::
* Array Subscripts::
* Function Calls as Expressions::
@end menu

@node The Increment Operator
@subsection The Increment Operator
@cindex increment operator

The increment operator @code{++} adds 1 to its operand.  The operand must
be a either a variable of one of the primitive data types, a pointer, or an
enumeration variable.  You can use the increment operator either before or after
the operand.  Here are some examples:

@example
@group
char w = '1';
int x = 5;
char y = 'B';
float z = 5.2;
int *p = &x;

x++;   /* @r{@code{x} is now 6.} */
++y;   /* @r{@code{y} is now `C' (on ASCII systems).} */
++w;   /* @r{@code{y} is now the character `2' (not the value 2).} */
z++;   /* @r{@code{z} is now 6.2.} */
++p;   /* @r{@code{p} is now @code{&x} + @code{sizeof(int)}, most likely an invalid pointer.} */
@end group
@end example

@noindent
A prefix increment adds 1 before the operand is evaluated.  A postfix
increment adds 1 after the operand is evaluated.  In the previous
examples, changing the position of the operator would make no difference.
However, there are cases where it does make a difference:

@example
@group
int x = 5;
printf ("%d \n", x++);    /* @r{Print @code{x} and then increment it.} */
/* @r{@code{x} is now equal to 6.} */
printf ("%d \n", ++x);    /* @r{Increment @code{x} and then print it.} */
@end group
@end example

@noindent
The output of the above example is:

@example
@group
5
7
@end group
@end example

 
@node The Decrement Operator
@subsection The Decrement Operator
@cindex decrement operator

The decrement operator @code{--} subtracts 1 from its operand.  The operand
must be a either a variable of one of the primitive data types, a pointer, or
an enumeration variable.  Here are some examples:

@example
@group
int x = 5;
char y = 'B';
float z = 5.2;
int *p = &x;

x--;   /* @r{@code{x} is now 4.} */
y--;   /* @r{@code{y} is now `A' on ASCII systems.} */
z--;   /* @r{@code{z} is now close to 4.2.} */
p--;   /* @r{@code{p} is now @code{&x} - @code{sizeof(int)}.} */
@end group
@end example

You can use the decrement operator either before or after the operand.
A prefix decrement subtracts 1 before the
operand is evaluated.  A postfix increment subtracts 1 after the operand is
evaluated.  In the previous examples, that wouldn't have made any
difference.  However, there are cases where it does make a difference:

@example
@group
int x = 5;
printf ("%d \n", x--);   /* @r{Print @code{x} and then decrement it.} */
/* x is now 4 */
printf ("%d \n", --x);   /* @r{Decrement @code{x} and then print it.} */
@end group
@end example

The output of the above example is:

@example
@group
5
3
@end group
@end example



@node The Positive Operator
@subsection The Positive Operator
@cindex positive operator

You can use the positive operator @code{+} on numeric values
to indicate that their value is positive.  By default, values are positive
unless explicitly stated to be negative, so there is no need to use this
operator as far as the compiler is concerned.  However, you can use it to
visually reinforce the fact that a value is positive.  Here are some examples:

@example
@group
int x = +5;
float y = +3.14159;
@end group
@end example


@node The Negative Operator
@subsection The Negative Operator
@cindex negative operator

You can use the negative operator @code{-} on numeric variables and constants
to negate their value.  Here are some examples:

@example
@group
int x = -5;
float y = -3.14159;
@end group
@end example

If the operand you use with the negative operator is of an unsigned data
type, then the result cannot negative, but rather is the
maximum value of the unsigned data type, minus the value of the operand.

Many systems use twos-complement arithmetic, and on such systems the
most negative value a signed type can hold is further away from zero
than the most positive value.   For example, on one platform, this
program:

@example
@group
#include <limits.h>
#include <stdio.h>

int main (int argc, char *argv[]) 
@{
  int x;
  x = INT_MAX;
  printf("INT_MAX  = %d\n", x);
  x = INT_MIN;
  printf("INT_MIN  = %d\n", x);
  x = -x;
  printf("-INT_MIN = %d\n", x);
  return 0;
@}
@end group
@end example

Produces this output:

@example
@group
INT_MAX  = 2147483647
INT_MIN  = -2147483648
-INT_MIN = -2147483648
@end group
@end example


@node The Logical Negation Operator
@subsection The Logical Negation Operator
@cindex logical negation operator

You can use the logical negation operator @code{!} to get the logical
opposite of its operand.  If its operand is 0 (or null, if the operand is
a pointer), then the result of the logical negation operator is 1.  If its
operand is anything other than 0 (or null), then the result of the logical
negation operator is 0.  In any case, the result is an integer value.  Here
are some examples:

@example
@group
int x = !5;   /* @r{@code{x} is 0.} */

/* @r{Since @code{x} is 0, @code{!x} is 1.} */
if (!x)
  printf ("x is 0");
@end group
@end example


@node The Bitwise Complement Operator
@subsection The Bitwise Complement Operator
@cindex bitwise complement operator

You can use the bitwise complement operator @code{~} to get the one's
complement of its operand.  The operand must be an integer or character type.
The bitwise complement operator examines its operand's bits, and
changes all 0 bits to 1 and all 1 bits to 0.  Here is an example:

@example
@group
unsigned char x = 42;
unsigned char y;
y = ~x; 
@end group
@end example

@noindent
In binary notation, this example assigns the value @code{01000010} to @code{x},
then performs a bitwise complement, storing the value @code{10111101} in @code{y}.

Using signed data types with the bitwise complement operator may cause
portability problems, so you should use unsigned data types for maximum
portability.


@node The Address Operator
@subsection The Address Operator
@cindex address operator

You can use the address operator @code{&} to obtain the memory address
of its operand.  You can use this operator both with variables of any data type
(including arrays and structures) and with functions, but you can't
use it with literal values.  You should only store the result of the
address operator in pointer variables.

@example
@group
int x = 5;
int *ptr = &x;
@end group
@end example


@node The Label Address Operator
@subsection The Label Address Operator
@cindex address operator
 
As a GNU C extension, you can also take the address of a label with
the label address operator @code{&&}.  The result is a @code{void*}
pointer which can be used with @code{goto}.  @xref{The goto
Statement}.

@node The Indirection Operator
@subsection The Indirection Operator
@cindex indirection operator

You can use the indirection operator @code{*} to obtain the value stored
at the address specified by its operand.  This is known as
@dfn{dereferencing} its operand.  Its operand must be a pointer.

@example
@group
int x = 5;
int y;
int *ptr;

ptr = &x;    /* @r{@code{ptr} now holds the address of @code{x}.} */

y = *ptr;    /* @r{@code{y} gets the value stored at the address}
                @r{stored in @code{ptr}.} */
@end group
@end example

The result of using the indirection operator with pointers that have
not been initialized is unspecified; usually the program will crash.

@node The Complex Conjugation Operator
@subsection The Complex Conjugation Operator
@cindex complex conjugation operator
@cindex conjugation operator
 
As a GNU C extension, you can use the complex conjugation operator @code{~} to
perform complex conjugation on its operand --- that is, it reverses the sign of
its imaginary component.   The operand must be an expression of a complex number
type.  Here is an example:
 
@example
@group
__complex__ int x = 5 + 17i;
 
printf ("%d  \n", (x * ~x));
@end group
@end example
 
Since an imaginary number @math{(a + bi)} multiplied by its conjugate is equal
to @math{a^2 + b^2}, the above @code{printf} statement will print 314, which
is equal to @math{25 + 289}.



@node The sizeof Operator
@subsection The sizeof Operator
@cindex sizeof operator

You can use the @code{sizeof} operator to obtain the size (in bytes)
of the data type of its operand.  The operand may be an actual type
specifier (such as @code{int} or @code{float}), as well as any valid
expression.  When the operand is a type name, it must be enclosed in
parentheses.  Here are some examples:

@example
@group
size_t a = sizeof(int);
size_t b = sizeof(float);
size_t c = sizeof(5);
size_t d = sizeof(5.143);
size_t e = sizeof a;
@end group
@end example

The result of the @code{sizeof} operator is of a type called @code{size_t},
which is defined in the header file @code{<stddef.h>}.  @code{size_t} is
an unsigned integer type, perhaps identical to @code{unsigned int} or
@code{unsigned long int}; it varies from system to system.

The @code{size_t} type is often a convenient type for a loop index,
since it is guaranteed to be able to hold the number of elements in
any array; this is not the case with @code{int}, for example.

The @code{sizeof} operator can be used to automatically compute the
number of elements in an array:

@example
@group
#include <stddef.h>
#include <stdio.h>

static const int values[] = @{ 1, 2, 48, 681 @};
#define ARRAYSIZE(x) (sizeof x/sizeof x[0])

int main (int argc, char *argv[]) 
@{
    size_t i;
    for (i = 0; i < ARRAYSIZE(values); i++) 
    @{
        printf("%d\n", values[i]);
    @}
    return 0;
@}
@end group
@end example

There are two cases where this technique does not work.  The first is
where the array element has zero size (GNU C supports zero-sized
structures as an extension).  The second is where the array is in
fact a function parameter (@pxref{Function Parameters}).

@node Type Casts
@subsection Type Casts
@cindex type casts
@cindex casts

You can use a type cast to explicitly cause an expression to be of a specified
data type.  A type cast consists of a type specifier enclosed in parentheses,
followed by an expression.  To ensure proper casting, you should also enclose
the expression that follows the type specifier in parentheses.  Here is
an example:

@example
@group
float x;
int y = 7;
int z = 3;
x = (float) (y / z);
@end group
@end example

In that example, since @code{y} and @code{z} are both integers, integer
division is performed, and even though @code{x} is a floating-point
variable, it receives the value 2.  Explicitly casting the result
of the division to @code{float} does no good, because the computed
value of @code{y/z} is already 2.

To fix this problem, you need to convert one of the operands to a
floating-point type before the division takes place:

@example
@group
float x;
int y = 7;
int z = 3;
x = (y / (float)z);
@end group
@end example

@noindent Here, a floating-point value close to 2.333@dots{} is assigned to @code{x}.

Type casting only works with scalar types (that is, integer,
floating-point or pointer types).  Therefore this is not allowed:

@example
@group
struct fooTag @{ /* members ... */ @};
struct fooTag foo;
unsigned char byteArray[8];

foo = (struct fooType) byteArray; /* @r{Fail!} */
@end group
@end example

@node Array Subscripts
@subsection Array Subscripts
@cindex array subscripts

You can access array elements by specifying the name of the array, and the
array subscript (or index, or element number) enclosed in brackets.  Here is
an example, supposing an integer array called @code{my_array}:

@example
@group
my_array[0] = 5;
@end group
@end example


@node Function Calls as Expressions
@subsection Function Calls as Expressions
@cindex function calls, as expressions

A call to any function which returns a value is an expression.

@example
@group
int function(void);
@dots{}
a = 10 + function();
@end group
@end example


@node Binary Operators
@section Binary Operators
@cindex binary operators
@cindex operators, binary

@menu
* The Addition Operator::
* The Subtraction Operator::
* The Multiplication Operator::
* The Division Operator::
* The Modulus Operator::
* The Shift Operators::
* The Bitwise AND Operator::
* The Bitwise Inclusive OR Operator::
* The Bitwise Exclusive OR Operator::
* The Comparison Operators::
* Logical Operators::
* Assignment Operators::
* The Comma Operator::
@end menu

@node The Addition Operator
@subsection The Addition Operator
@cindex addition operator

You use the addition operator @code{+} to add two operands.  You put
the operands on either side of the operator, and it does not matter
which operand goes on which side (in the absence of side effects):
@code{3 + 5} and @code{5 + 3} both result in 8.  The operands must be
either expressions of a primitive data type or pointers.

@example
@group
x = 5 + 3;
y = 10 + 37;
z = 1 + 2 + 3 + 4 + 5;
@end group
@end example

When you use more than one addition operator (and more than two operands),
such as in the last example, the expression is evaluated from left to right.

@node The Subtraction Operator
@subsection The Subtraction Operator
@cindex subtraction operator 

You use the subtraction operator @code{-} to subtract its second operand
from its first operand.  You put the operands on either side of the operator,
and it does matter which operand goes on which side:  @code{3 - 5} and
@code{5 - 3} do not have the same result.  The operands must be either
expressions of a primitive data type or pointers.

@example
@group
x = 5 - 3;
y = 57 - 10;
z = 5 - 4 - 3 - 2 - 1;
@end group
@end example

When you use more than one subtraction operator (and more than two operands),
such as in the last example, the expression is evaluated from left to
right.


@node The Multiplication Operator
@subsection The Multiplication Operator
@cindex multiplication operator

You use the multiplication operator @code{*} to multiply two operands
together.  You put the operands on either side of the operator, and it does
not matter which operand goes on which side:  @code{3 * 5} and @code{5 * 3}
both result in 15.  The operands must be expressions of a primitive
data type.

@example
@group
x = 5 * 3;
y = 47 * 1;
z = 1 * 2 * 3 * 4 * 5;
@end group
@end example

When you use more than one multiplication operator (and more than two
operands), such as in the last example, the expression is evaluated from
left to right.


@node The Division Operator
@subsection The Division Operator
@cindex division operator 

You use the division operator @code{/} to divide its first operand
by its second operand.  You put the operands on either side of the
operator, and it does matter which operand goes on which side:  @code{3 / 5}
and @code{5 / 3} do not have the same result.  The operands must be
expressions of a primitive data type.

@example
@group
x = 5 / 3;
y = 940 / 20;
z = 100 / 2 / 2;
@end group
@end example

When you use more than one division operator (and more than two operands),
such as in the last example, the expression is evaluated from left to right.

Integer division of positive values truncates towards zero, so 5/3 is
1.  However, if either operand is negative, the direction of rounding
is implementation-defined.

@c @ref{Signed Integer Division} for
@c information about overflow in signed integer division.

@c We should state how GCC behaves if an operand is negative.

@node The Modulus Operator
@subsection The Modulus Operator
@cindex modulus operator 

You use the modulus operator @code{%} to obtain the remainder produced
by dividing its two operands.  You put the operands on either side of
the operator, and it does matter which operand goes on which
side:  @code{3 % 5} and @code{5 % 3} do not have the same result.
The operands must be expressions of a primitive data type.

@example
@group
x = 5 % 3;
y = 74 % 47;
z = 47 % 32 % 21;
@end group
@end example

When you use more than one modulus operator (and more than two operands),
like in the last example, the expression is evaluated from left to right.

A common application of the modulus operator is to determine if one
number is divisible by another number.  If it is divisible, then the
remainder is zero.  Here is an example of that:

@example
@group
int counter;
for (counter = 0; counter <= 100; counter++)
  @{
    if (counter % 5 == 0)
      printf ("%d\n", counter);
  @}
@end group
@end example

That prints all of the integers from 0 to 100 that are divisible by 5.


@node The Shift Operators
@subsection The Shift Operators
@cindex shift operators

You use the left-shift operator @code{<<} to shift its first operand's bits
to the left.  You specify the number of bit-places shifted with the second
operand.  If there is a 1 bit in the leftmost bit position, it will be
discarded.  New bits that are added to the rightmost bit position will all
be 0.

@example
@group
x = 47;    /* @r{47 is 00101111 in binary.} */
x << 1;    /* @r{00101111 << 1 is 01011110.} */
@end group
@end example


You use the right-shift operator @code{>>} to shift its first operand's bits
to the right.  You specify the number of bit-places shifted with the second
operand.  If there is a 1 bit in the rightmost bit position, it will be
discarded.  New bits that are added to the leftmost bit position
may be either 0 or 1.  If the first operand is positive, then the added
bits will be 0.  If it is signed and negative, the added bits will be
either 0 @emph{or} whatever value was previously in the leftmost bit
position (though formally, the result is undefined).

@example
@group
x = 47;   /* @r{47 is 00101111 in binary.} */
x >> 1;   /* @r{00101111 >> 1 is 00010111.} */
@end group
@end example

For both @code{<<} and @code{>>}, if the second operand is greater
than the bit-width of the first operand, or the second operand is
negative, the behaviour is undefined.

You can use the shift operators to perform a variety of interesting
hacks.  For example, given a date with the day of the month numbered
as @code{d}, the month numbered as @code{m}, and the year @code{y}, you
can store the entire date in a single number @code{x}:

@example
int d = 12;
int m = 6;
int y = 1983;
int x = ((y << 4) + m) << 5) +  d;
@end example

@noindent
You can then extra the original day, month, and year out of @code{x}
using a combination of shift operators and modular division:

@example
d = x % 32;
m = (x >> 5) % 16;
y = x >> 9;
@end example

@node The Bitwise AND Operator
@subsection The Bitwise AND Operator
@cindex bitwise AND operator

The bitwise AND operator @code{&} examines each bit in its two
operands, and when two corresponding bits are both 1, the resulting bit
is 1.  In every other case the result is 0.  Here is an example of
how this operator works, using binary numbers:

@example
11001001 & 10011011 = 10001001
@end example

If you look closely at that, you'll see that when a bit is 1
in both operands, the corresponding bit in the result is set to 1.
Otherwise it is set to 0.  Here is another example, this time in C:

@example
@group
char x = 149, y = 34, z;
z = x & y;
@end group
@end example


@node The Bitwise Inclusive OR Operator
@subsection The Bitwise Inclusive OR Operator
@cindex bitwise inclusive OR operator

The bitwise inclusive OR operator @code{|} examines each bit
in its two operands, and when two corresponding bits are both
0, the resulting bit is 0.  In every other case the resulting
bit is 1.  Here is an example of how this operator works, using
binary numbers:

@example
11001001 | 10011011 = 11011011
@end example

Here is another example, this time in C:

@example
@group
char x = 149, y = 34, z;
z = x | y;
@end group
@end example


@node The Bitwise Exclusive OR Operator
@subsection The Bitwise Exclusive OR Operator
@cindex bitwise exclusive OR operator

The bitwise exclusive OR operator @code{^} (also known as XOR) examines
each bit in its two operands, and when two corresponding bits are different,
the resulting bit is 1.  When they are the same, the resulting bit is 0.
Here is an example of how this operator works, using binary numbers:

@example
11001001 ^ 10011011 = 01010000
@end example

Here is another example, this time in C:

@example
@group
char x = 149, y = 34, z;
z = x ^ y;
@end group
@end example


@node The Comparison Operators
@subsection The Comparison Operators
@cindex comparison operators

You use the comparison operators to determine how two operands relate to
each other: are they equal to each other, is one larger than the other,
is one smaller than the other, and so on.   When you use any of the
comparison operators, the result is either 1 or 0, meaning true or false,
respectively.

In the following code examples, the variables @code{x} and @code{y} stand
for any two expressions of arithmetic types, or pointers.


@menu
* The Equal-to Operator::       
* The Not-Equal-to Operator::   
* The Less-Than Operator::      
* The Less-Than-or-Equal-to Operator::  
* The Greater-Than Operator::   
* The Greater-Than-or-Equal-to Operator::  
@end menu

@node The Equal-to Operator
@subsubsection The Equal-to Operator
@cindex equal-to operator

Use the equal-to operator @code{==} to test two operands for
equality.  It evaluates to 1 if the two operands are equal, and 0 if the
two operands are not equal.

@example
@group
if (x == y)
  puts ("x is equal to y");
else
  puts ("x is not equal to y");
@end group
@end example

Comparing floating-point types for exact equality is usually a bad
idea since it is unreliable.  @ref{Real Number Types} for more
information.

@node The Not-Equal-to Operator
@subsubsection The Not-Equal-to Operator
@cindex not-equal-to operator

Use the not-equal-to operator @code{!=} to test two operands for
inequality.  If the two operands are not equal, the result is 1.  Otherwise,
if the two operands @emph{are} equal, the result is 0.

@example
@group
if (x != y)
  puts ("x is not equal to y");
else
  puts ("x is equal to y");
@end group
@end example

Comparing floating-point values with @code{!=}  is usually a bad
idea since it is unreliable.  @ref{Real Number Types} for more
information.

@node The Less-Than Operator
@subsubsection The Less-Than Operator
@cindex less-than operator

Use the less-than operator @code{<} to determine if the first operand
is less than the second operand.  If it is, the result is 1.  Otherwise, the
result is 0.

@example
@group
if (x < y)
  puts ("x is less than y");
else
  puts ("x is not less than y");
@end group
@end example


@node The Less-Than-or-Equal-to Operator
@subsubsection The Less-Than-or-Equal-to Operator
@cindex less-than-or-equal-to operator

Use the less-than-or-equal-to operator @code{<=} to determine if the first
operand is less than or equal to the second operand.  If it is, the result
is 1.  Otherwise, the result is 0.

@example
@group
if (x <= y)
  puts ("x is less than or equal to y");
else
  puts ("x is not less than or equal to y");
@end group
@end example


@node The Greater-Than Operator
@subsubsection The Greater-Than Operator
@cindex greater-than operator

Use the greater-than operator @code{>} to determine if the first operand
is greater than the second operand.  If it is, the result is 1. Otherwise,
the result is 0.

@example
@group
if (x > y)
  puts ("x is greater than y");
else
  puts ("x is not greater than y");
@end group
@end example


@node The Greater-Than-or-Equal-to Operator
@subsubsection The Greater-Than-or-Equal-to Operator
@cindex greater-than-or-equal-to operator

Use the greater-than-or-equal-to operator @code{>=} to determine if the
first operand is greater than or equal to the second operand.  If it is, the
result is 1.  Otherwise, the result is 0.

@example
@group
if (x >= y)
  puts ("x is greater than or equal to y");
else
  puts ("x is not greater than or equal to y");
@end group
@end example



@node Logical Operators
@subsection Logical Operators
@cindex logical operators

You can use the logical operators to test the truth value of two
operands.  The operands can be expressions of a primitive type,
or pointers.

Note that while the comparison operators return the value 1 for a true
expression, any nonzero expression is considered true in C.


@menu
* The Logical AND Operator::    
* The Logical OR Operator::     
@end menu

@node The Logical AND Operator
@subsubsection The Logical AND Operator
@cindex logical AND operator

Use the logical AND operator @code{&&} to test if two expressions are
both true.  If the first one is false, then the second one is not
evaluated. 

@example
@group
if ((x == 5) && (y == 10))
  printf ("x is 5 and y is 10");
@end group
@end example


You can also build an expression using more than one AND operator,
and more than two operands, like this:

@example
@group
if ((x == 5) && (y == 10) && (z == 15))
  printf ("x is 5 and y is 10 and z is 15");
@end group
@end example

@dfn{Short-circuit evaluation} is used.  That is, if the left operand
of @code{&&} is false, the right operand is not evaluated, so this
code fragment will print @samp{phew}:

@example
@group
0 && die() || printf("phew")
@end group
@end example

@node The Logical OR Operator
@subsubsection The Logical OR Operator
@cindex logical OR operator

Use the logical OR operator @code{||} to test if at least one of two
expressions is true.  If the first expression is true, then the
second expression is not evaluated.

@example
@group
if ((x == 5) || (y == 10))
   printf ("x is 5 or y is 10");
@end group
@end example

You can also build an expression using more than one OR operator, and
more than two operands, like this:

@example
@group
if ((x == 5) || (y == 10) || (z == 15))
    printf ("x is 5 or y is 10 or z is 15");
@end group
@end example

Short-circuit evaluation is used, so if the left operand is true, the
right operand is not evaluated.

@node Assignment Operators
@subsection Assignment Operators
@cindex assignment operators

You use the assignment operators to give values to variables.  The first
operand---the operand to which a value is being assigned, also known as the
``lvalue''---cannot be a literal value or any other constant value.
Except as noted, the operands must be of a primitive data type, or a
pointer.


@menu
* The Assignment Operator::     
* The Compound Assignment Operators::
@c * Generalized Lvalues::  
@end menu

@node The Assignment Operator
@subsubsection The Assignment Operator
@cindex assignment operator

Use the standard assignment operator @code{=} to assign the value of its
right operand to its left operand.  Unlike the other assignment operators,
you can use this operator with variables of a structure type, in addition
to arithmetic types and pointers.

@example
@group
x = 10;
y = 45 + 2;
z = (2 * (3 + function () ));
@end group
@end example


@node The Compound Assignment Operators
@subsubsection The Compound Assignment Operators
@cindex compound assignment operators
@cindex assignment operators, compound


You use the compound assignment operators to perform an operation on
both the left and right operands, and then assign the resulting
expression to the left operand.  Here is a list of the compound
assignment operators, and a brief description of what they do:

@itemize

@item
@code{+=}

This operator adds its two operands together, and then assigns the result of
the addition to the left operand.


@item
@code{-=}

This operator subtracts its right operand from its left operand, and
assigns the result of the subtraction to the left operand.


@item
@code{*=}

This operator multiplies its two operands together, and then assigns the
result of the multiplication to the left operand.


@item
@code{/=}

This operator divides its left operand by its right operand, and assigns
the result of the division to the left operand.


@item
@code{%=}

This operator performs modular division on its operands, and assigns the
result of the division to the left operand.


@item
@code{<<=}

This operator performs a left shift operation on its left operand, shifting
by the number of bits specified by the right operand, and assigns the
result of the shift to the left operand.


@item
@code{>>=}

This operator performs a right shift operation on its left operand,
shifting by the number of bits specified by the right operand, and assigns
the result of the shift to the left operand.


@item
@code{&=}

This operator performs a bitwise AND operation on its two operands, and
assigns the result of the operation to the left operand.


@item
@code{^=}

This operator performs a bitwise exclusive OR operation on its two operands,
and assigns the result of the operation to the left operand.


@item
@code{|=}

This operator performs a bitwise inclusive OR operation on its two
operands, and assigns the result of the operation to the left operand.


@end itemize
@comment  __End of compound assignment operator list

Here is an example of using one of the compound assignment operators:

@example
x += y;
@end example

@noindent
That produces the same result (since there no side effects in the
simple variable @code{x} as an lvalue) as:

@example
x = x + y;
@end example


@c GNU C Extension -- temporarily commented out for manual 0.1
@c @node Generalized Lvalues
@c @subsubsection Generalized Lvalues
@c @cindex compound expressions as lvalues
@c @cindex expressions, compound, as lvalues
@c @cindex conditional expressions as lvalues
@c index expressions, conditional, as lvalues
@c @cindex casts as lvalues
@c @cindex generalized lvalues
@c @cindex lvalues, generalized
@c @cindex extensions, @code{?:}
@c @cindex @code{?:} extensions
@c 
@c Compound expressions, conditional expressions and casts are allowed as
@c lvalues provided their operands are lvalues.  This means that you can take
@c their addresses or store values into them.
@c 
@c For example, you can assign a value to a compound expression, provided the
@c last expression in the sequence is an lvalue.  These two expressions are
@c equivalent:
@c 
@c @example
@c (a, b) += 5
@c a, (b += 5)
@c @end example
@c 
@c Similarly, you can take the address of a compound expression.  So, these two
@c expressions are equivalent:
@c 
@c @example
@c &(a, b)
@c a, &b
@c @end example
@c 
@c A conditional expression is a valid lvalue if its type is not void and if
@c both the second and third operands are valid lvalues.  For example, these two
@c expressions are equivalent:
@c 
@c @example
@c (a ? b : c) = 5
@c (a ? b = 5 : (c = 5))
@c @end example
@c 
@c A type cast is a valid lvalue if its operand is an lvalue.  A simple
@c assignment whose left-hand side is a cast works by converting the
@c right-hand side first to the specified type, then to the type of the
@c inner left-hand side expression.  After this is stored, the value is
@c converted back to the specified type to become the value of the
@c assignment.  Thus, if @code{a} has type @code{char *}, the following two
@c expressions are equivalent:
@c 
@c @example
@c (int)a = 5
@c (int)(a = (char *)(int)5)
@c @end example
@c 
@c An assignment-with-arithmetic operation such as @code{+=} applied to a cast
@c performs the arithmetic using the type resulting from the cast, and then
@c continues as in the previous case.  Therefore, these two expressions are
@c equivalent:
@c 
@c @example
@c (int)a += 5
@c (int)(a = (char *)(int) ((int)a + 5))
@c @end example

@c You cannot take the address of an lvalue cast, because the use of its
@c address would not work out coherently.  Suppose that @code{&(int)f} were
@c permitted, where @code{f} has type @code{float}.  Then the following
@c statement would try to store an integer bit-pattern where a floating
@c point number belongs:
@c 
@c @example
@c *&(int)f = 1;
@c @end example
@c 
@c This is quite different from what @code{(int)f = 1} would do---that
@c would convert 1 to floating point and store it.  Rather than cause this
@c inconsistency, we think it is better to prohibit use of @code{&} on a cast.
@c 
@c If you really do want an @code{int *} pointer with the address of
@c @code{f}, you can simply write @code{(int *)&f}.



@node The Comma Operator
@subsection The Comma Operator
@cindex comma operator

You use the comma operator @code{,} to separate two expressions.  For instance,
the first expression might produce a value that is used by the second expression:
@c This works because there is a sequence point after the evaluation
@c of the left hand side.
@example
@group
x++, y = x * x;
@end group
@end example

More commonly, the comma operator is used in @code{for} statements, like
this:

@example
@group
/* @r{Using the comma operator in a @code{for} statement.} */

for (x = 1, y = 10;  x <=10 && y >=1;  x++, y--)
  @{
    @dots{}
  @}
@end group
@end example

@noindent
This lets you conveniently set, monitor, and modify multiple control
expressions for the @code{for} statement.

A comma is also used to separate function parameters; however, this
is @emph{not} the comma operator in action.   In fact, if the comma
operator is used as we have discussed here in a function call (without
enclosing it in an additional set of parentheses), then the compiler will
interpret that as calling the function with an extra parameter.

If you want to use the comma operator in a function argument, you need
to put parentheses around it.  That's because commas in a function
argument list have a different meaning: they separate arguments.
Thus,

@example
function (x,  y=47,  x,  z);
@end example

@noindent
is interpreted as a function call with four arguments, but

@example
function (x,  (y=47,  x),  z);
@end example

@noindent
is a function call with just three arguments.  (The second argument is
@code{(y=47, x)}.)

@node Member Access Expressions
@section Member Access Expressions
@cindex member access expressions

You can use the member access operator @code{.} to access the members
of a structure or union variable.  You put the name of the structure
variable on the left side of the operator, and the name of the
member on the right side.

@example
@group
struct point
@{
  int x, y;
@};

struct point first_point;

first_point.x = 0;
first_point.y = 5;
@end group
@end example

@cindex indirect member access operator
You can also access the members of a structure or union variable via
a pointer by using the indirect member access operator @code{->}.
@code{x->y} is equivalent to @code{(*x).y}.

@example
@group
struct fish
  @{
    int length, weight;
  @};

struct fish salmon;

struct fish *fish_pointer = &salmon;

fish_pointer->length = 3;
fish_pointer->weight = 9;
@end group
@end example 

@xref{Pointers}.


@node Conditional Expressions
@section Conditional Expressions
@cindex conditional expressions
@cindex expressions, conditional
@cindex ternary operator

You use the conditional operator to cause the entire conditional
expression to evaluate to either its second or its third operand, based
on the truth value of its first operand.  Here's an example:

@example
a ? b : c
@end example

If expression @code{a} is true, then expression @code{b} is evaluated
and the result is the value of @code{b}.  Otherwise, expression
@code{c} is evaluated and the result is @code{c}.  

Expressions @code{b} and @code{c} must be compatible.   That is, they
must both be 

@enumerate
@item arithmetic types
@item compatible @code{struct} or @code{union} types
@item pointers to compatible types (one of which might be the NULL pointer)
@end enumerate 

Alternatively, one operand is a pointer and the other is a
@code{void*} pointer.

Here is an example

@example
a = (x == 5) ? y : z;
@end example

Here, if @code{x} equals 5, then @code{a} will receive
the value of @code{y}.  Otherwise, @code{a} will receive the value of
@code{z}.  This can be considered a shorthand method for writing a simple
@code{if}@dots{}@code{else} statement.  The following example will
accomplish the same task as the previous one:

@example
@group
if (x == 5)
    a = y;
else
    a = z;
@end group
@end example

If the first operand of the conditional operator is true, then the third
operand is never evaluated.  Similarly, if the first operand is false,
then the second operand is never evaluated.  The first operand is always
evaluated.


@c GNU C Extension

@node Statements and Declarations in Expressions
@section Statements and Declarations in Expressions
@cindex statements inside expressions
@cindex declarations inside expressions
@cindex expressions containing statements
@cindex macros, statements in expressions

As a GNU C extension, you can build an expression using compound
statement enclosed in parentheses.  This allows you to included
loops, switches, and local variables within an expression.

Recall that a compound statement (also known as a block) is a sequence of
statements surrounded by braces.  In this construct, parentheses go around
the braces.  Here is an example:

@example
@group
(@{ int y = function (); int z;
    if (y > 0) z = y;
   else z = - y;
   z; @})
@end group
@end example

That is a valid (though slightly more complex than necessary) expression
for the absolute value of @code{function ()}.

The last thing in the compound statement should be an expression
followed by a semicolon; the value of this subexpression serves as the
value of the entire construct.  (If you use some other kind of statement
last within the braces, the construct has type @code{void}, and thus
effectively no value.)

This feature is especially useful in making macro definitions ``safe'' (so
that they evaluate each operand exactly once).  For example, the
``maximum'' function is commonly defined as a macro in standard C as
follows:

@example
#define max(a,b) ((a) > (b) ? (a) : (b))
@end example

@noindent
@cindex side effects, macro argument
But this definition computes either @code{a} or @code{b} twice, with bad
results if the operand has side effects.  In GNU C, if you know the
type of the operands (here let's assume @code{int}), you can define
the macro safely as follows:

@example
#define maxint(a,b) \
  (@{int _a = (a), _b = (b); _a > _b ? _a : _b; @})
@end example

If you don't know the type of the operand, you can still do this, but you
must use @code{typeof} expressions or type naming.

@comment Add reference to typeof

Embedded statements are not allowed in constant expressions, such as
the value of an enumeration constant, the width of a bit field, or
the initial value of a static variable.


@node Operator Precedence
@section Operator Precedence in C
@cindex operator precedence
@cindex precedence, operator

When an expression contains multiple operators, such as @code{a + b *
f()}, the operators are grouped based on rules of @dfn{precedence}.
For instance, the meaning of that expression is to call the function
@code{f} with no arguments, multiply the result by @code{b}, then add
that result to @code{a}.  That's what the C rules of operator
precedence determine for this expression.

The following is a list of types of expressions, presented in order of
highest precedence first.  Sometimes two or more operators have equal
precedence; all those operators are applied from left to right
unless stated otherwise.

@enumerate

@item
Function calls, array subscripting, and membership access operator
expressions.

@item
Unary operators, including logical negation, bitwise complement,
increment, decrement, unary positive, unary negative, indirection
operator, address operator, type casting, and @code{sizeof}
expressions.  When several unary operators are consecutive,
the later ones are nested within the earlier ones: @code{!-x}
means @code{!(-x)}.

@item
Multiplication, division, and modular division expressions.

@item
Addition and subtraction expressions.

@item
Bitwise shifting expressions.

@item
Greater-than, less-than, greater-than-or-equal-to, and less-than-or-equal-to@*
expressions.

@item
Equal-to and not-equal-to expressions.

@item
Bitwise AND expressions.

@item
Bitwise exclusive OR expressions.

@item
Bitwise inclusive OR expressions.

@item
Logical AND expressions.

@item
Logical OR expressions.

@item
Conditional expressions (using @code{?:}).  When used as
subexpressions, these are evaluated right to left.

@item
All assignment expressions, including compound assignment.  When multiple
assignment statements appear as subexpressions in a single larger expression,
they are evaluated right to left.

@item
Comma operator expressions.

@end enumerate

@c ??? Lots more examples are needed here!

@c ??? You need to explain when parentheses are strongly
@c recommended in order to make the structure clear.

The above list is somewhat dry and is apparently straightforward, but
it does hide some pitfalls.  Take this example:

@example
foo = *p++;
@end example

Here @code{p} is incremented as a side effect of the expression, but
@code{foo} takes the value of @code{*(p++)} rather than @code{(*p)++},
since the unary operators bind right to left.   There are other
examples of potential surprises lurking behind the C precedence
table.  For this reason if there is the slightest risk of the reader
misunderstanding the meaning of the program, you should use
parentheses to make your meaning clear.

@node Order of Evaluation
@section Order of Evaluation

In C you cannot assume that multiple subexpressions are evaluated in
the order that seems natural.  For instance, consider the expression
@code{++a * f()}.  Does this increment @code{a} before or after
calling the function @code{f}?  The compiler could do it in either
order, so you cannot make assumptions.

@c ??? Get the full information about these rules and fully document them!

@c find an accessible way to discuss sequence points.   
@c include an authoritative list of where sequence points occur, 
@c with an appropriate list of counterexamples such as f(x++, x++).


@c @node Overflow
@c @section Overflow

@c @include overflow.texi
