Expression
Binary Operations
Binary arithmetic operations are math actions we can do on whole numbers. These include adding, subtracting, multiplying, dividing and finding the remainder.
Arithmetic Expression | Meaning |
---|---|
operand + operand | add the operands |
operand - operand | subtract the right from the left operand |
operand * operand | multiply the operands |
operand / operand | divide the left by the right operand |
operand % operand | remainder of the division of left by right |
Relational Expressions
C language has six ways to compare things. These comparisons are called relational expressions. They look at two values and decide if a condition is true or not. If it's true, they give back 1; if it's false, they give back 0. The result you get from these comparisons is always an int
type.
Relational Expression | Meaning |
---|---|
operand == operand | operands are equal |
operand > operand | left is greater than the right |
operand >= operand | left is greater than or equal to the right |
operand < operand | left is less than the right |
operand <= operand | left is less than or equal to the right |
operand != operand | left is not equal to the right |
Logical Expressions
C language doesn't have special words for true or false. Instead, it sees the number 0 as false and any other number as true. C has three ways to do logical operations. These operations give back 1 if they are true and 0 if they are false. The result you get from these operations is always an int
type.
Logical Expression | Meaning |
---|---|
operand && operand | both operands are true |
operand || operand | one of the operands is true |
! operand | the operand is not true |
DeMorgan's Law
DeMorgan's Law is a rule in logic that helps us simplify complex statements. It has two parts:
- "Not (A and B)" is the same as "(Not A) or (Not B)"
- "Not (A or B)" is the same as "(Not A) and (Not B)"
Example:
// below logically identical
// example #1
c = !(a && b)
c = !a || !b
// example #2
c = !a && !b
c = !(a || b)
Binary operands
Expression | Shorthand | Longhand | Meaning |
---|---|---|---|
operand += operand | i += 4 | i = i + 4 | add 4 to i and assign to i |
operand -= operand | i -= 4 | i = i - 4 | subtract 4 from i and assign to i |
operand *= operand | i *= 4 | i = i * 4 | multiply i by 4 and assign to i |
operand /= operand | i /= 4 | i = i / 4 | divide i by 4 and assign to i |
operand %= operand | i %= 4 | i = i % 4 | remainder after i / 4 then assign to i |
Unary Operands
Expression | Shorthand | Longhand | Meaning |
---|---|---|---|
++operand | ++i | i = i + 1 | (prefix) increment i by 1 |
operand++ | i++ | i = i + 1 | (postfix) increment i by 1 |
--operand | --i | i = i - 1 | (prefix) decrement i by 1 |
operand-- | i-- | i = i - 1 | (postfix) decrement i by 1 |
- Prefix does the operation first, then gives you the result.
- Postfix gives you the result first, then does the operation.
Type Casting
Type conversions are supported by the C programming language. In order to convert the type of an operand, you put the target type in parenthesis before the operand. This statement is known as a cast.
int main() {
int a = 5, b = 2;
printf("Result of int division: %d\n", a / b); // 2
printf("Result of double division: %f\n", (double)a / b); // 2.5
return 0;
}
Mixed-Type Expression
Computers use different parts to handle whole numbers and decimal numbers. So, they can only work with calculations that use the same type of numbers. But sometimes, calculations use both types of numbers. So, we need rules to change one type of number into another type for these calculations.
Type | Rank |
---|---|
long double | highest |
double | ... |
float | ... |
long long | ... |
long | ... |
int | ... |
short | ... |
char | lowest |
In a mixed-type expression, lower-ranked types are automatically converted to the type of the highest-ranked operand. This is called "promotion".
int a = 5;
double b = 3.14;
b = a; // 'a' is automatically promoted to a double
"Narrowing" is the opposite of promotion. It's when a value of a higher-ranked type is assigned to a variable of a lower-ranked type. This can cause a loss of data.
double a = 3.14;
int b;
b = a; // 'a' is truncated to an int, so 'b' becomes 3, not 3.14