34 operators:
+ - * / %
< <= > >= == !=
&& || !
& | ^ ~ << >>
=
+ - ++ -- (type) sizeof , ()
?: (ternary operator)
[] * . ->
Operator Precedence and Associativity:
?: conditional operator
++ increment operator
-- decrement operator
(type)
,
()
-----------------
[] arrays
* pointers
. structures/unions
-> structures/unions
Arithmetic operators:
+ - * / %
sign operators: + -
ternary operator
int c = -5;
int c = +10; //unary operator
int c;
c = 5+2; //binary operator
c=5-2;
c=5*2;
c=5/2; ==> 2
c=5%2; ==> 1
2 | 5 | 2 (/) quotient
4
------------
1 (%) remainder
float c;
c=5.0+2.0;
c=5.0-2.0;
c=5.0*2.0;
c=5.0/2.0; ==> 2.500000
c=5.0%2.0; ==> invalid
Relational operators: output boolean
< <= > >= == !=
int c; //int, float, double, char
boolean --> int
false --> 0
true --> 1
c=5<2;
c=5<=2;
c=5>2;
c=5>=2;
c=5==2;
c=5!=2;
conditions
Logical operators: && || !
AND gate
OR gate
NOT gate
int c;
c = 10 && 20; ==> T && T ==> T ==> 1
c = -10 && 20; ==> T && T ==> T ==> 1
c = 0 && 10; ==> F && T ==> F ==> 0
c = 10 || 20; ==> 1
c = !(10); ==> !(T) ==> F ==> 0
Note: by default logical &&, || gates are short circuit operators in C.
decision ==> club conditions
a,b,c
5, 3, 8
a>b
a>c
a>b && a>c ==> a is bigger
int --> boolean
0 ==> false
non-zero ==> true
Truth Table:
Z = A && B;
A B Z
0 0 0
0 1 0
1 0 0
1 1 1
Z = A || B;
A B Z
0 0 0
0 1 1
1 0 1
1 1 1
Z = !A;
A Z
0 1
1 0
Bitwise Operators
& | ^ ~ << >>
&
int c;
c = 10 | 5; //15
c = ~(10);
1010 ==> 0101 ==> 5
1010
0101
---------------
1111 ==> 15
Assignments:
Digital gates: AND, OR, NOT, NAND, NOR, X-OR, X-NOR
Number systems: decimal, binary, octal, hexa-decimal ==> conversions
bitwise operators
1 byte ==> 8 bits
1 nibble ==> 4 bits
4 bytes ==> 32 bits
//signed, unsigned
//long, short
4 bits ==> 16 values ==> 0 to 15 (unsigned) / -8 to +7 (signed)
8 bits ==> 256 values ==> 0 to 255 (unsigned) / -128 to +127 (signed)
32 bits ==> signed range ==> -2147483648 to 2147483647
unsigned range ==> 0 to 4294967295
8421
abcd
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111