Assignment

A value may be placed into a variable by an assignment, whose simplest form is:
         identifier = expression;
which must occur as part of a statement sequence inside the function where the variable named by the identifier has been declared. The symbol Ò=Ó is known as the assignment operator. The value on the right hand side of this operator is placed in the variable on the left hand side. The type of the value must match with or be convertible into the variable's declared type.

Examples are

    IntVal = 45:            /*integer literal is assigned*/

    RVal1 = 345.678;        /*real literal is assigned*/

    IntVal = 45 + 9;        /*integer expression is assigned*/

    RVal2 = RVal1 * 43.2;   /*real expression is assigned*/

    Continue = RVal2>10;    /*Boolean expression assigned to int*/

Other forms of assignment

C allows most simple arithmetic operations to be combined with assignment if the target of the assignment is the first of the operands on the right hand side. Thus:

AssignmentEquivalent long formOperator
I += 3;I = I + 3;Addition
I -= 3;I = I - 3;Subtraction
I *= K + 3;I = I * (K + 3);Multiplication
I /= 3;I = I / 3;Division
I %= 4;I = I % 4;Modulus (remainder)
I ^= J;I = I ^ J;Bitwise exclusive OR
I &= 7;I = I & 7;Bitwise AND
I |= 4;I = I | 4;Bitwise OR
I >>= 3;I = I >> 3;Right bitwise shift
I <<= 2;I = I << 2;Left bitwise shift


Next - conditionals.

back to note on statements.

Back to Contents page.