
@c This is part of The GNU C Reference Manual
@c Copyright (C) 2007 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::
* The Ternary Operator::
@c * Statements and Declarations in Expressions::
* Order of Evaluation::
@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 Indirection Operator::
@c * 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.  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 6.} */
y++;   /* @r{@code{y} is now `C'.} */
z++;   /* @r{@code{z} is now 6.2.} */
p++;   /* @r{@code{p} is now @code{&x} + @code{sizeof(int)}.} */
@end group
@end example

You can use the increment operator either before or after the operand.
A prefix increment adds 1 before the
operand is evaluated.  A postfix increment adds 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 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

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'.} */
z--;   /* @r{@code{z} is now 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 indicate that their value is negative.  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 is not negative, but rather the
maximum value of the unsigned data type, minus the value of the operand.


@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.} */
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 int x = 500;
unsigned int y;
y = ~x;
@end group
@end example

Using signed data types with the bitwise complement operator may cause
portability problems, so 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 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.

@c GNU C Extension -- temporarily commented out for manual release 0.1, covering ANSI C89
@c
@c @node The Complex Conjugation Operator
@c @subsection The Complex Conjugation Operator
@c @cindex complex conjugation operator
@c @cindex conjugation operator
@c 
@c You can use the complex conjugation operator @code{~} to perform complex
@c conjugation on its operand --- that is, it reverses the sign of its
@c imaginary component.   The operand must be an expression of a complex number
@c type.  Here is an example:
@c 
@c @example
@c @group
@c __complex__ int x = 5 + 17i;
@c 
@c printf ("%d  \n", (x * ~x));
@c @end group
@c @end example
@c 
@c Since an imaginary number @math{(a + bi)} multiplied by its conjugate is equal
@c to @math{a^2 + b^2}, the above @code{printf} statement will print 314, which
@c 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.  You must enclose the operand in parentheses after the
operator.  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);
@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.


@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.  By explicitly casting the result
of the division to @code{float}, the floating-point value 2.333@dots{}
is retained and assigned to @code{x}.

Type casting also works with custom data types.  Here is an example of converting
an array of 8 bytes to a 8-byte structure type:

@example
@group
struct fooType
@{
  float f;
  unsigned short int a;
  unsigned short int b;
@};

struct fooType foo;
unsigned char byteArray[8];

foo = (struct fooType) byteArray;
@end group
@end example

@noindent
In practice, you should also pack your structure type to ensure that
the compiler doesn't add any padding, thereby increasing the size of
the type.  In general, different compilers may use different
representations for data types, so such constructs are not portable.


@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::
* Member Access Operators::
@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.


@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 unsigned, then the added
bits will be 0.  If it is signed, the added bits will be either 0 @emph{or}
whatever value was previously in the leftmost bit position.

@example
@group
x = 47;   /* @r{47 is 00101111 in binary.} */
x >> 1;   /* @r{00101111 >> 1 is 00010111.} */
@end group
@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 = 01011001
@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 primitive 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


@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


@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


@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



@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 primitive 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:

@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.

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

Even though what may be intended by such a function call is to
call the function with the parameters @code{x}, @code{y}, and
@code{z}, with @code{y} set to 47, what will happen is that the
function will be called with the parameters @code{x}, @code{y=47},
@code{y}, and @code{z}.  So if you want to include expressions that
use the comma operator in a function call, surround the comma operator
expression with parentheses.

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

That will call @code{function} with the parameters @code{x},
@code{y}, and @code{z}, with @code{y} set to 47.



@node Member Access Operators
@subsection Member Access Operators
@cindex member access operators

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 The Ternary Operator
@section The Ternary Operator
@cindex ternary operator, the
@cindex operator, ternary
@cindex conditional operator

C has only one ternary operator --- the conditional operator @code{? :}.

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.

Put the first operand is before the question mark; this operand
may be any expression.  If it evaluates to true (nonzero), then
the second operand (which is also an expression), which is placed
in between the question mark and the colon, is evaluated, and
becomes the value of the conditional expression.  Otherwise, the third
operand (also an expression), which is placed after the colon, is
evaluated, and becomes the value of the conditional expression.  Here
is an example

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

In that example, if @code{x} equals 5, then @code{a} will receive
the value @code{y}.  Otherwise, @code{a} will receive the value
@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 != 0)
    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
@c
@c @node Statements and Declarations in Expressions
@c @section Statements and Declarations in Expressions
@c @cindex statements inside expressions
@c @cindex declarations inside expressions
@c @cindex expressions containing statements
@c @cindex macros, statements in expressions
@c 
@c A compound statement enclosed in parentheses may appear as an expression
@c in GNU C.  This allows you to use loops, switches, and local variables
@c within an expression.
@c 
@c Recall that a compound statement (also known as a block) is a sequence of
@c statements surrounded by braces; in this construct, parentheses go around
@c the braces.  Here is an example:
@c 
@c @example
@c @group
@c (@{ int y = function (); int z;
@c     if (y > 0) z = y;
@c    else z = - y;
@c    z; @})
@c @end group
@c @end example

@c That is a valid (though slightly more complex than necessary) expression
@c for the absolute value of @code{function ()}.
@c 
@c The last thing in the compound statement should be an expression
@c followed by a semicolon; the value of this subexpression serves as the
@c value of the entire construct.  (If you use some other kind of statement
@c last within the braces, the construct has type @code{void}, and thus
@c effectively no value.)
@c 
@c This feature is especially useful in making macro definitions ``safe'' (so
@c that they evaluate each operand exactly once).  For example, the
@c ``maximum'' function is commonly defined as a macro in standard C as
@c follows:
@c 
@c @example
@c #define max(a,b) ((a) > (b) ? (a) : (b))
@c @end example
@c 
@c @noindent
@c @cindex side effects, macro argument
@c But this definition computes either @code{a} or @code{b} twice, with bad
@c results if the operand has side effects.  In GNU C, if you know the
@c type of the operands (here let's assume @code{int}), you can define
@c the macro safely as follows:
@c 
@c @example
@c #define maxint(a,b) \
@c   (@{int _a = (a), _b = (b); _a > _b ? _a : _b; @})
@c @end example
@c 
@c Embedded statements are not allowed in constant expressions, such as
@c the value of an enumeration constant, the width of a bit field, or
@c the initial value of a static variable.
@c 
@c If you don't know the type of the operand, you can still do this, but you
@c must use @code{typeof} expressions or type naming.

@comment Add reference to typeof typedef


@node Order of Evaluation
@section Order of Evaluation
@cindex order of evaluation
@cindex evaluation, order of
@cindex operator precedence
@cindex precedence, operator

When an expression consists of subexpressions, such as
@code{a + b * f()}, the subexpressions are not all evaluated at once,
nor are they necessarily evaluated left to right as they appear in the
expression.  In the preceding example, for instance, the function call
@code{f()} will be evaluated first, the result of which will be multiplied
by @code{b}, the result of which will be added to @code{a}.  That is not
intuitive simply by looking at the expression, but there is a definite
order in which subexpressions are evaluated.

The following is a list of types of expressions, presented in the same
order that they are evaluated.  The expressions that are evaluated first
are said to have the highest precedence, and those that are evaluated last
the low precedence.  Some types of expressions have the same precedence.
If two or more subexpressions have the same precedence, then they
are usually evaluated left to right.  If they are @emph{not} evaluated
left to right, then that is explicitly stated in the list.

@comment I do not know where the complex conjugation operator belongs
@comment in this list, though this doesn't matter for the C89 material.  --TJR

@enumerate

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

@item
Logical negation, bitwise complement, increment, decrement, unary positive,
unary negative, indirection operator, address operator, type casting, and
@code{sizeof} expressions.  When used as subexpressions, these are evaluated
right to left.

@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 operator 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

