=
a = 1
sets a
to 1 (and returns 1)
b = 1.23
sets b
to 1.23 (and returns 1.23)
c = a
sets c
to whatever is in a
(and returns the value of a
)
+
and -
a = -32
sets a
to -32
b = -1.4
sets b
to -1.4
c = +10
sets c
to 10
+
and -
"a = 1 + 1
sets a
to 2
b = 3.5 + 2.1
sets b
to 5.6
c = a - 2 + 1
sets c
to whatever is in
a
less 1, because the statement is evaluated from left
to right. If a
is set to 4, evaluation would proceed like:
a - 2 + 1 4 - 2 + 1 2 + 1 3
*
and /
a = 5 * 2
sets a
to 10 (5 times 2)
b = 7.0 / 4.0
sets b
to 1.75
c = 7 / 4
sets c
to 1 (because integer
divison always rounds down)
%
a = 5 % 2
sets a
to 1
c = 7 % 4
sets c
to 3
(a/b)*b + a%b
is equal to a
a%b
is equal to a
if 0 < a < b
or 0 < -a < -b
++
and --
++a
sets a
to (a+1)
and returns
that value
b = ++a
sets both a
and b
to
(a+1)
++
and --
a++
sets a
to (a+1)
but returns
the original value of a
b = a++
sets b
to a
and a
to (a+1)
(
and )
3 * 5 / 2
is the same as (3 * 5) / 2
,
which is evaluated as:
3 * (5 / 2)
,
it is evaluated as:
||
a || b
returns 1 if either a
or b
are non-zero, and returns 0 otherwise
0 || 0 || 1
returns 1
0 || 0 || 0
returns 0
a
is set to 0,
(a-- || ++a || a++ || a++ || --a)
returns 1 and sets a
to 2 because:
a--
sets a
to -1 and returns 0, so evaluation moves on to:
++a
sets a
to 0 and returns 0, so evaluation moves on to:
a++
sets a
to 1 and returns 0, so evaluation moves on to:
a++
sets a
to 2 and returns 1, so evaluation stops
--a
untouched
&&
a && b
returns 1 if both a
and b
are non-zero, and returns 0 otherwise
1 && 1 && 0
returns 0
1 && 1 && 1
returns 1
==
and !=
a == b
returns 1 if both a
and b
a != b
returns 1 if a
and b
do not hold the same value, and returns 0 otherwise
1 == 0
returns 0 and 1 != 0
returns 1
17 == 17
returns 1 and 17 != 17
returns 0
==
or !=
when comparing floating-point values
>
, <
,
>=
and <=
a > b
returns 1 if a
is greater than
b
a < b
returns 1 if a
is less than
b
a >= b
returns 1 if a
is greater than
or equal to b
a <= b
returns 1 if a
is less than
or equal to b
=>
and =<
will cause errors
!
!a
returns 1 if a
is non-zero, and returns 0 otherwise
|
9 | 3
is (1001 | 0011)
in
binary which evaluates to 1011
,
since the OR operator sets every result bit to 1 if it is set to 1 in either
of the arguments.(0 | 0)
is 0
,
but (0 | 1)
, (1 | 0)
, and (1 | 0)
are all 1
.
&
9 & 3
evaluates to 0001
or 1
,
since the AND operator only sets a result bit to 1
when both
of the bits are set to 1
.(1 & 1)
is 1
,
but (0 & 0)
, (0 & 1)
, and (1 & 0)
are all 0
.
^
9 ^ 3
evaluates to 1010
, since the XOR
operator only sets a result bit to 1
when only 1
of the two bits is set to 1
and the other is set to
0
.(0 ^ 1)
and (1 ^ 0)
are 1
but both (0 ^ 0)
and (1 ^ 1)
are 0
.
<<
and >>
a = 2 << 3
sets a
to 16
(because it shifts 000010
3 bits left to 010000
)
b = 13 >> 2
sets b
to 3
(because it shifts 01110
2 bits right to 00011
)
~
~a
inverts all the bits
~0100101
evaluates to
1011010
+=
, -=
,
*=
, /=
, %=
,
<<=
, >>=
,
&=
, ^=
, |=
a *= 2
multiplies a
by 2 and stores the result
in a
a >>= 2
shifts a
right 2 bits and stores the result
in a
sizeof
sizeof
operator returns the size in units
(where a unit is typically a byte) of its argument
sizeof char
(or sizeof(char)
) returns 1
sizeof(int)
returns 4 on a machine which uses 32 bit
int
s
int a; sizeof(a)
returns 4 (for 32 bit int
s)
sizeof(1 + 3)
returns 4 (for 32 bit int
s)
int a; a = 3; sizeof(a++);would leave
a
set to 3