Sambar Server Documentation

CScript Operators


Assignment Operators

Assignment operators are really just binary operators. The simplest example is '=', which takes the value on the right and places it into the variable on the left.
=assignment
+=addition assignment
-=subtraction assignment
*=multiplication assignment
/=division assignment
%=remainder/modulus assignment
&=   bitwise AND assignment
|=bitwise OR assignment
^=bitwise exclusive OR assignment
<<=left shift assignment
>>=right shift assignment
So, for example, 'i += j' is equivalent to 'i = i + j' (see complete list of equivalents below). The value to the left of the assignment operator has to be a variable where a result can be stored.

Arithmetic Operators

The following are the most common arithmetic operators:
+     Addition
-Subtraction
*Multiplication
/Division
%Modulo (remainder from integer division)
+Addition
Unless parens are used to order evaluation, operator precedence specifies that *, / and % be performed before + or - in an expression. The following table shows the precedence of all operators:
OperatorAssociativityDescription
( )left-to-rightParenthesis
! ~ ++ -- - * &   right-to-leftUnary
* / %left-to-rightMultiply, Divide Modulus
+ -left-to-rightAdd, Subtract
>> <<left-to-rightShift Right, Left
> < =left-to-rightGreater, Less Than
== !=left-to-rightEqual, Not Equal
&left-to-rightBitwise AND
^left-to-rightBitwise Exclusive OR
|left-to-rightBitwise OR
&&left-to-rightLogical AND
||left-to-rightLogical OR
?:right-to-leftConditional Expression
= += -= etc.right-to-leftAssignment
,left-to-rightComma
Note:

C has some operators which allow abbreviation of certain types of arithmetic assignment statements.

Shorthand   Equivalent
i++;i = i + 1;
++i;i = i + 1;
i--;i = i - 1;
--i;i = i - 1;
a += 22;a = a + 22;
a -= 22;a = a - 22;
a *= 22;a = a * 22;
a /= 22;a = a / 22;
a %= 22;a = a % 22;
For auto-increment (++) and decrement (--), if the operator occurs before the variable name (i.e. --i;) change the value of the variable before evaluating the remainder of the expression:
Shorthand   Equivalent
a = i-- * b;a = i * b;
i = i - i;
a = --i * b;i = i - 1;
a = i * b

Unary Operators

Unary operators are operators that only take one argument.
sizeof(i)the number of bytes of storage allocated to i
+123positive 123
-123negative 123
~ione's complement (bitwise complement)
!ilogical negation (i.e., 1 if i is zero, 0 otherwise)
*ireturns the value stored at the address pointed to by i NOT SUPPORTED!
&ireturns the address in memory of i
++iadds one to i, and returns the new value of i
--isubtracts one from i, and returns the new value of i
i++adds one to i, and returns the old value of i
i--subtracts one from i, and returns the old value of i
i[j]array indexing
i (j)calling the function i with argument j
i.jreturns member j of structure i NOT SUPPORTED!
i->jreturns member j of structure pointed to by i NOT SUPPORTED!

Bit Operators

C has several operators for logical bit-operations. For example,
x = x & 0177;
forms the bit-wise AND of x and 0177, effectively retaining only the last seven bits of x. Other operators are:
OperatorDescription
|inclusive OR
^exclusive OR
~1's complement
!logical not
<<left shift
>>right shift

Comma Operator

C allows you to put multiple expression in the same statement, separated by a comma. The expressions are evaluated in left-to-right order. The value of the overall expression is then equal to that of the rightmost expression. For example:
Shorthand   Equivalent
i = ((j = 2), 3);i = 3; j = 2;
myfunct (i, (j = 2, j + 1), 1);j = 2; myfunct (i, 3, 1);
The comma operator has the lowest precedence, so it is always executed last when evaluating an expression. Both of the above examples are artifical, and not very useful. The comma operator can be useful when used in for and while loops.

© 2001 Sambar Technologies. All rights reserved. Terms of Use.