Operators 1
Operators 1
Taken from: Feuer, A. R., (1999). The C Puzzle Book, 3rd Printing.
USA. Addison Wesley Longman Inc. Used for educational
purposes.
Operators
Operators 1: Basic Arithmetic Operators
Explain
Operators 2: Assignment Operators
Explain:
Operators 3: Logic and Increment Operators
Explain:
Operators 4: Bitewise Operators
Explain:
Operators 5: Relational and Conditional Operators
Explain:
Operators 6: Operator Precedence and Evaluation
Explain:
Appendix 1: Precedence Table
Operators
Operators 1
C Programs are built from statements, statements from
expressions, and expressions from operators and operands. C is
unusually rich in operators; Because of this richness, the rules that
determine how operators apply to operands play a central role in
understanding of expressions The rules, known as precedence and
associativity, are summarized in precedence table of apendix 1.
Use the table to solve the problems on this section.
#include <stdio.h>
int main()
{
int x;
x = - 3 + 4 * 5 / 6; printf("%d\n", x); // Operators 1.1
x = 3 + 4 % 5 - 6; printf("%d\n", x); // Operators 1.2
x = - 3 * 4 % - 6 / 5; printf("%d\n", x); // Operators 1.3
x = ( 7 + 6 ) % 5 / 2; printf("%d\n", x); // Operators 1.4
}
11
1
0
1
Explain
Operators 2
#include <stdio.h>
int main()
{
int x = 2, y, z;
10
40
1
1
Explain:
#include <stdio.h>
int main()
{
int x, y, z;
x = 2; y = 1; z = 0;
x = x && y || z; PRINT(x); // Operators 3.1
PRINT(x || y && z); // Operators 3.2
x = y = 1;
z = x ++ - 1; PRINT(x); PRINT(z); // Operators 3.3
z += - x ++ + ++ y; PRINT(x); PRINT(z); // Operators 3.4
z = x / ++ x; PRINT(x); // Operators 3.5
Operators 3
}
1
1
2
0
3
0
4
Explain:
#include <stdio.h>
int main()
{
int x, y, z;
x = 1; y = - 1;
PRINT( ! x | x ); // Operators 4.5
PRINT( ~ x | x ); // Operators 4.6
PRINT( x ^ x ); // Operators 4.7
x <<= 3; PRINT(x); // Operators 4.8
y <<= 3; PRINT(x); // Operators 4.9
y >>= 3; PRINT(x); // Operators 4.10
Operators 4
x | y & z = 3
x | y & ~ z = 3
x ^ y & ~ z = 1
x & y && z = 1
! x | x = 1
~ x | x = -1
x ^ x = 0
x = 8
x = 8
x = 8
Explain:
#include <stdio.h>
int main()
{
int x = 1, y = 1, z = 1;
x += y += z;
PRINT( x < y ? y : x ); // Operators 5.1
PRINT( x < y ? x ++ : y ++ );
PRINT( x ); PRINT( y ); // Operators 5.2
PRINT( z += x < y ? x ++ : y ++ );
PRINT( x ); PRINT( y ); // Operators 5.3
x = 3; y = z = 4;
PRINT( (z >= y >= x) ? 1 : 0 ); // Operators 5.4
PRINT( z >= y && y >= x ); // Operators 5.5
Operators 5
x < y ? y : x = 3
x < y ? x ++ : y ++ = 2
x = 3
y = 3
z += x < y ? x ++ : y ++ = 4
x = 3
y = 4
(z >= y >= x) ? 1 : 0 = 0
z >= y && y >= x = 1
Explain:
#include <stdio.h>
int main()
{
int x, y, z;
x = y = z = 1;
++x || ++y && ++z; PRINT3( x, y, z); // Operators 6.1
x = y = z = 1;
++x && ++y || ++z; PRINT3( x, y, z); // Operators 6.2
x = y = z = 1;
++x && ++y && ++z; PRINT3( x, y, z); // Operators 6.3
x = y = z = - 1;
++x && ++y || ++z; PRINT3( x, y, z); // Operators 6.4
x = y = z = - 1;
++x || ++y && ++z; PRINT3( x, y, z); // Operators 6.5
x = y = z = - 1;
++x && ++y && ++z; PRINT3( x, y, z); // Operators 6.6
Operators 6
x < y ? y : x = 3
x < y ? x ++ : y ++ = 2
x = 3
y = 3
z += x < y ? x ++ : y ++ = 4
x = 3
y = 4
(z >= y >= x) ? 1 : 0 = 0
z >= y && y >= x = 1
Explain:
Operators 7
Operators 8