?? cintro.doc
字號:
that you would use a simple value in 'C', you can also use an
expression. We have already seen that with the "return" statement in
the "add" function in our example program. Knowing that we can use
expressions and values interchangably, we could shorten the example
"main" function to:
main()
{
int a, b;
a = 1;
b = 2;
printf("The result of (1+2)=%d\n", add(a,b));
}
or even:
main()
{
int a, b;
a = 1;
b = 2;
printf("The result of (1+2)=%d\n", a+b);
}
or even:
main()
{
printf("The result of (1+2)=%d\n", 1 + 2);
}
Or, if we wanted the 'a, b & c' variables set anyway:
/* Note the use or round brackets '()' to incorporate
a SUB-EXPRESSION into the main expression */
main()
{
int a, b, c;
printf("The result of (1+2)=%d\n", c = (a = 1) + (b = 2));
}
You can see that an entire expression, including three ASSIGNMENTS
was included in a place where only a simple value is used. This is
one of the great powers of 'C', and can result in very small fast
efficent code (at the expense of readability). Numerous examples of
this type of programming may by found in the source code for the
MICRO-C compiler.
Intro to MICRO-C Page: 22
4.1 Unary operators
Unary (single operand) operators are those operators which
perform a computation on only a single value. In most cases, the
operator must be placed immediatly BEFORE the value to be operated
on.
4.1.1 Negate: -
The NEGATE operator causes the value to its right to be
changed in sign. Thus, a positive value will be converted to a
negative value (of the same magnitude) and vice versa. It is
most often used to enter negative numbers, but it is perfectly
legal to apply this operator to a variable or expression.
eg: a = -5; /* a = negative 5 */
b = 10; /* b = positive 10 */
c = -b; /* c = -10 */
d = -(b+5); /* d = -15 */
e = -b+5; /* e = -5 *
f = -a; /* f = 5 */
4.1.2 Bitwise Complement: ~
The BITWISE COMPLEMENT operator reverses the value (0 or 1)
of each individual BIT in the value to its right. This is very
useful when used with the other BITWISE operators (AND, OR and
XOR), to perform test on combinations of bits in a byte (char)
or word (int).
eg: a = 5; /* a = 00000101 */
b = ~a; /* b = 11111010 */
4.1.3 Logical Complement: !
The LOGICAL COMPLEMENT operator reverse the "logical sense"
(TRUE or FALSE) of the value to its right. In 'C' a value is
considered to be logically TRUE if it contains any value other
than zero. A value of zero is considered to be logically FALSE.
The logical complement gives a value of zero (FALSE) if the
original value was non-zero, and a value of one (TRUE) if the
original value was zero. You will see later that there are
CONDITIONAL statements in 'C', which perform certain operations
only if values are TRUE. This operator provides a simple method
of reversing those conditions to occur when the value is FALSE.
eg: if(a) b = 10; /* Set b to 10 if a is TRUE */
if(!a) b = 10; /* Set b to 10 if a is FALSE */
Intro to MICRO-C Page: 23
4.1.4 Increment: ++
The INCREMENT operator must be used on a value that can be
ASSIGNED (such as a variable or array element). It causes the
value to be INCREASED by one (except for a special case with
POINTERS which are advanced by the size of the type to which
they point).
Unlike most other unary operators, the increment operator
may be placed either BEFORE or AFTER the value, and behaves
slightly differently depending on its position. If placed
BEFORE the value, the variable is incremented, and the new
value is passed on as the result. If placed AFTER the value,
the original value is passed on as the result, and the variable
is then incremented.
eg: a = b = 10; /* Set a & b to 10 */
c = ++a; /* c = 11 (a = 11) */
d = b++; /* d = 10 (b = 11) */
4.1.5 Decrement: --
The DECREMENT operator behaves exactly the same as
increment, except that the value is REDUCED instead of
increased.
eg: a = b = 10; /* Set a & b to 10 */
c = --a; /* c = 9 (a=9) */
d = b--; /* d = 10 (b=9) */
4.1.6 Indirection: *
The INDIRECTION operator may only be applied to POINTERS, or
expressions which result in a POINTER VALUE. It causes the
memory contents at the address contained in the pointer to be
accessed, instead of the pointer value itself.
eg: char *ptr; /* Declare a pointer to character */
ptr = 10; /* Set pointer variable to 10 */
*ptr = 5; /* Set 'char' at address 10 to 5 */
a = ptr; /* a = 10 (pointer value) */
b = *ptr; /* b = 5 (Indirect through address 10) */
4.1.7 Address: &
The ADDRESS operator may only be used on a value which can
be ASSIGNED (such as a variable or array element). It returns
the memory address of the value, instead of the value itself.
eg: a = 10; /* Set variable a to 10 */
ptr = &a; /* Get address of 'a' */
b = *ptr; /* b = 10 (contents of 'a') */
*ptr = 15; /* Store 15 at address of 'a' */
c = a; /* c = 15 */
Intro to MICRO-C Page: 24
4.2 Binary Operators
In additon to the "unary" operators presented above, 'C' has a
large complement of BINARY (two operand) operators. The binary
operators take two values, presented on the left and right side of
the operator, and combine them into some form of computed value.
4.2.1 Addition: +
The ADDITION operator computes the SUM of two values.
eg: a = b + 5; /* a = sum of b and 5 */
4.2.2 Subtraction: -
The SUBTRACTION operator computes the DIFFERENCE of two
values.
eg: a = b - 5; /* a = difference of b and 5 */
4.2.3 Multiplication: *
The MULTIPLICATION operator computes the PRODUCT of two
values.
eg: a = b * 5; /* a = b multiplied by 5 */
4.2.4 Division: /
The DIVISION operator computes the QUOTIENT resulting from
the division of the left value by the right value.
eg: a = b / 5; /* a = b divided by 5 */
4.2.5 Modulus: %
The MODULUS operator computes the REMAINDER resulting from
the division of the left value by the right value.
eg: a = b % 5; /* a = remainer of b divided by 5 */
4.2.6 Bitwise And: &
The BITWISE AND operator performs an AND function on each
pair of bits between the values. Bit positions which have a one
(1) bit in BOTH values will receive a one in the result. All
other bit positions will receive zero (0).
eg a = 5; /* a = 00000101 */
b = 6; /* b = 00000110 */
c = a & b; /* c = 00000100 (4) */
Intro to MICRO-C Page: 25
4.2.7 Bitwise Or: |
The BITWISE OR operator performs an OR function on each pair
of bits between the values. Bit positions which have a one (1)
in EITHER value will receive a one in the result. All other bit
positions will receive zero (0).
eg a = 5; /* a = 00000101 */
b = 6; /* b = 00000110 */
c = a | b; /* c = 00000111 (7) */
4.2.8 Bitwise Exclusive Or: ^
The BITWISE EXCLUSIVE OR operator performs an EXCLUSIVE OR
function on each pair of bits between the values. Bit positions
which have a one (1) in EITHER value, but NOT IN BOTH values
will receive a one in the result. All other bit positions will
receive zero (0).
eg a = 5; /* a = 00000101 */
b = 6; /* b = 00000110 */
c = a ^ b; /* c = 00000011 (3) */
4.2.9 Logical And: &&
The LOGICAL AND operator returns TRUE (non-zero) only if
BOTH values are TRUE. If either value is FALSE, FALSE (zero) is
returned.
MICRO-C accomplishes this by evaluating the left value, and
returning zero (FALSE) if it is equal to zero, otherwise the
right value is evaluated and returned. Some 'C' compilers force
the returned value to be either zero (0) or one (1).
eg: if(a && b)
printf("Both 'a' AND 'b' are TRUE");
4.2.10 Logical Or: ||
The LOGICAL OR operator returns TRUE (non-zero) if EITHER
value is true, if both values are FALSE, FALSE (zero) is
returned.
MICRO-C accomplishes this by evaluating the left value, and
returning its value if it is not zero (TRUE), otherwise the
right value is evaluated and returned. Some 'C' compilers force
the returned value to be
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -