Handbook-C 20
Handbook-C 20
Like this:
It's just a convention, but one that can greatly help you
while reading or writing a C program as it improves
readability. Uppercase name means constant,
lowercase name means variable.
#define AGE 37
19
In this case, you don't need to add a type, and you
don't also need the = equal sign, and you omit the
semicolon at the end.
20
Operators
C offers us a wide variety of operators that we can use
to operate on data.
arithmetic operators
comparison operators
logical operators
compound assignment operators
bitwise operators
pointer operators
structure operators
miscellaneous operators
Arithmetic operators
In this macro group I am going to separate binary
operators and unary operators.
21
Operator Name Example
= Assignment a = b
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulo a % b
For example:
int a = 2;
int b;
b = a++ /* b is 2, a is 3 */
b = ++a /* b is 4, a is 4 */
Comparison operators
22
Operator Name Example
== Equal operator a == b
Logical operators
! NOT (example: !a )
Compound assignment
operators
Those operators are useful to perform an assignment
and at the same time perform an arithmetic operation:
23
Operator Name Example
+= Addition assignment a += b
Multiplication
*= a *= b
assignment
/= Division assignment a /= b
Miscellaneous operators
The ternary operator
The ternary operator is the only operator in C that
works with 3 operands, and it’s a short way to express
conditionals.
Example:
a ? b : c
24