1.2.2 OperatorsOperators support the same arithmetic, logical, comparative, and bitwise operators as ANSI C. The following sections list the four types of operators and show their usage.
Arithmetic OperatorsRemainder
Operator
|
Purpose
|
*
|
multiplication
|
/
|
division
|
%
|
modulus (remainder)
|
+
|
addition
|
-
|
subtraction
|
^
|
exponentiate See note below.
|
Note: Origin C, by default, treats the caret characterCaret Character(^) as an exponentiate operatorExponentiate Operator. This is done to be consistent with LabTalk. ANSI C uses the caret character as the exclusive OR operator. You can force Origin C to treat the caret character as the exclusive OR operator by using a special pragma statement before your code.
out_int("10 raised to the 3rd is ", 10^3);
#pragma xor(push, FALSE)
out_int("10 XOR 3 is ", 10^3);
#pragma xor(pop) // set back to the default action of xor
Dividing an integer by another integer will give an integer result by default. Use the pragma statement below before codes to make Origin C compiler to treat all numeric literals as double type.
out_double("3/2 is ", 3/2); // output 1
#pragma numlittype(push, TRUE)
out_double("3/2 is ", 3/2); // output 1.5
#pragma numlittype(pop) // set back to the default action of numlittype
The modulus operatorModulus Operator calculates the remainder of the left operand divided by the right operand. This operator can only be applied to integral operands.
out_int("The remainder of 11 divided by 2 is ", 11 % 2);
Comparison Operators
Comparison operators evaluate to true or false with true yielding 1 and false yielding 0.
Operator
|
Purpose
|
>
|
greater than
|
>=
|
greater than or equal to
|
<
|
less than
|
<=
|
less than or equal to
|
==
|
equal to
|
!=
|
not equal to
|
if( aa >= 0 )
out_str("aa is greater than or equal to zero");
if( 12 == aa )
out_str("aa is equal to twelve");
if( aa < 99 )
out_str("aa is less than 99");
Logical Operators
Logical operators evaluate to true or false with true yielding 1 and false yielding 0. The operands are evaluated from left to right. Evaluation stops when the entire expression can be determined.
Operator
|
Purpose
|
!
|
NOT
|
&&
|
AND
|
||
|
OR
|
Consider the following two examples:
expr1A && expr2
expr1B || expr2
expr2 will not be evaluated if expr1A evaluates to false or expr1B evaluates to true. This behavior is to the programmer's advantage and allows efficient code to be written. The following demonstrates the importance of ordering more clearly.
if( NULL != ptr && ptr->dataValue < upperLimit )
process_data(ptr);
In the above example the entire 'if' expression will evaluate to false if ptr is equal to NULL. If ptr is NULL then it is very important that the dataValue not be compared to the upperLimit because reading the dataValue member from a NULL pointer can cause an application to end abruptly.
Bitwise OperatorsAND OperatorOR OperatorXOR Operator
Bitwise operators allow you to test and set individual bits. The operator treats the operands as an ordered array of bits. The operands of a bitwise operator must be of integral type.
Operator
|
Purpose
|
~
|
complement
|
<<
|
shift left
|
>>
|
shift right
|
&
|
AND
|
^
|
exclusive OR (XOR) See note below.
|
|
|
inclusive (normal) OR
|
Note: Origin C, by default, treats the caret character as an exponentiate operator. This is done to be consistent with LabTalk. ANSI C uses the caret character as the exclusive OR operator. You can force Origin C to treat the caret character as the exclusive OR operator by using a special pragma statement before your code.
out_int("10 raised to the 3rd is ", 10^3);
#pragma xor(push, FALSE)
out_int("10 XOR 3 is ", 10^3);
#pragma xor(pop)
|