C
04-Expression

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 ExpressionMeaning
operand + operandadd the operands
operand - operandsubtract the right from the left operand
operand * operandmultiply the operands
operand / operanddivide the left by the right operand
operand % operandremainder 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 ExpressionMeaning
operand == operandoperands are equal
operand > operandleft is greater than the right
operand >= operandleft is greater than or equal to the right
operand < operandleft is less than the right
operand <= operandleft is less than or equal to the right
operand != operandleft 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 ExpressionMeaning
operand && operandboth operands are true
operand || operandone of the operands is true
! operandthe 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

ExpressionShorthandLonghandMeaning
operand += operandi += 4i = i + 4add 4 to i and assign to i
operand -= operandi -= 4i = i - 4subtract 4 from i and assign to i
operand *= operandi *= 4i = i * 4multiply i by 4 and assign to i
operand /= operandi /= 4i = i / 4divide i by 4 and assign to i
operand %= operandi %= 4i = i % 4remainder after i / 4 then assign to i

Unary Operands

ExpressionShorthandLonghandMeaning
++operand++ii = i + 1(prefix) increment i by 1
operand++i++i = i + 1(postfix) increment i by 1
--operand--ii = 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.

TypeRank
long doublehighest
double...
float...
long long...
long...
int...
short...
charlowest

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