Page Content

Tutorials

Understanding C Operators: Building Blocks Of Expressions

C Operators

An operator is a unique symbol that gives the computer instructions on how to manipulate data stored in variables or constants using logic or mathematics. They have an effect on operands, which are the data values (constants or variables) that operators work with. ‘Operands’ can refer to both variables and constants. Operators join variables, constants, and array elements to create expressions. Unary, Binary, Arithmetic, Relational, Logical, Conditional, Assignment, Bitwise, and Increment/Decrement operators are some of the categories into which C operators fall. Binary operations are carried out on two data values, whereas unary operations are carried out on a single data element.

Basic Arithmetic Operators

Addition, subtraction, multiplication, and division are among the arithmetic operations that can be carried out on one or more operands using arithmetic operators. The fundamental arithmetic operators in C Language and C++ are +, -, *, /, and %. Because they work with two values or terms, these are referred to as binary arithmetic operators.

  • + : For Addition.
  • – : For Subtraction Also used as a unary minus (negation).
  • * : For Multiplication.
  • / : For Division.
  • % : For Modulo or Remainder

Only integer types can be utilized with this operator. After dividing an integer, it returns the residual. When using %, the remainder’s sign and the numerator’s sign are always the same.

The entire number of the divisor (b) in the dividend (a) is the outcome of dividing two integers, a and b. We call this integer division. For instance, 17 / 5 evaluates to 3 and 7 / 4 to 1. The modulus operator % yields the remainder of the division. For instance, 17% of 4 produces 3, and 17% of 5 produces 2. You can convert either or both operands to a floating-point number to make sure that while working with integer variables, floating-point division is carried out rather than integer division.

The compiler will automatically convert a floating-point number to an integer if you mix types.

Example using Arithmetic Operators (C):

#include <stdio.h>
int main() {
    int x = 17;
    int y = 3;
    int result_int_div, result_mod;
    float a = 17.0;
    float b = 3.0;
    float result_float_div;
    printf("x + y = %d\n", x + y);       // Addition
    printf("x - y = %d\n", x - y);       // Subtraction 
    printf("x * y = %d\n", x * y);       // Multiplication 
    result_int_div = x / y;              // Integer Division 
    printf("x / y (integer division) = %d\n", result_int_div); // 17 / 3 = 5
    result_mod = x % y;                  // Modulo
    printf("x %% y (remainder) = %d\n", result_mod);         // 17 % 3 = 2 
    result_float_div = a / b;            // Floating-point division 
    printf("a / b (floating division) = %f\n", result_float_div); // 17.0 / 3.0 = 5.666...
    return 0;
}

Basic Assignment Operators

An identifier or variable can be assigned a value using assignment operators. The equal sign (=) is the assignment operator that is most frequently employed. The form of an expression that uses the assignment operator is either variable = expression or identifier = expression. The expression on the right-hand side is evaluated first when this statement is run, and its value is then added to the variable on the left-hand side to replace its previous value. Assignment statements are where the majority of calculations are done. One type of binary operator is the assignment operator.

Additionally, the assignment operator = is handled like an operator in C. Compared to many other operators, it has a lower precedence and a right-to-left associativity. The value assigned to the variable on the left is the value of the assignment expression taken as a whole.

To shorten assignment expressions, C has a number of shorthand assignment operators. These combine the assignment operator with an arithmetic or bitwise operator. The variable op= expression is the standard format. With the exception of the variable on the left being evaluated just once, this is similar to variable = variable op (expression).

Arithmetic assignment operator examples include:

  • += (Addition assignment): c += 3; is the same as c = c + 3;
  • -= (Subtraction assignment): d -= 4; is the same as d = d – 4;
  • *= (Multiplication assignment): e *= 5; is the same as e = e * 5;
  • /= (Division assignment): f /= 3; is the same as f = f / 3;
  • %= (Modulus assignment): g %= 9; is the same as g = g % 9;

There are also bitwise assignment operators: &=, |=, ^=, <<=, >>=

There are three reasons to use assignment operators: the statement is simpler to write, it is typically easier to read, and it may result in faster applications. While the assignment operators all have the same precedence, all operators—aside from the comma operator—have a higher precedence.

Example using Assignment Operators (C):

#include <stdio.h>
int main() {
    int x = 10;
    int y = 5;
    int z = 3;
    x = y;       // Basic assignment x becomes 5
    printf("After x = y; x = %d\n", x);
    x += z;      // Addition assignment x = x + z; x becomes 5 + 3 = 8
    printf("After x += z; x = %d\n", x);
    x *= y;      // Multiplication assignment x = x * y; x becomes 8 * 5 = 40
    printf("After x *= y; x = %d\n", x);
    x /= z;      // Division assignment x = x / z; x becomes 40 / 3 = 13 (integer division)
    printf("After x /= z; x = %d\n", x);
    x %= z;      // Modulus assignment x = x % z; x becomes 13 % 3 = 1
    printf("After x %%= z; x = %d\n", x);
    int a, b, c;
    a = b = c = 0; // Multiple assignment using right-to-left associativity 
    printf("After a = b = c = 0; a = %d, b = %d, c = %d\n", a, b, c); // a, b, c are all 0
    return 0;
}

Relational Operators

Two operands are compared using relational operators to ascertain their relationship. They determine whether one operand is more than, less than, equal to, greater than, or unequal to the other. A relational expression always yields a boolean value, which in C is usually written as 1 for true and 0 for false.
In C/C++, the following relational operators are available:

  • < : Less than.
  • <= : Less than or equal to.
  • > : Greater than.
  • >= : Greater than or equal to6…
  • == : Equal to5… (Note: distinguish from the assignment operator =)
  • != : Not equal to.

Relational operators group from left to right and have the same precedence level. They are more important than logical and assignment operators, but less important than arithmetic operators.

Logical Operators

A compound relational expression is created by combining several relational expressions or conditions using logical operators. They yield Boolean values (1 or 0), just like relational operators do.

In C/C++, there are three logical operators:

  • &&: Logical AND. If and only if both operands are true (non-zero), the result is true (1).
  • ||: Logical OR. If either or both operands are true (non-zero), the outcome is true (1).
  • !: Reasonable NOT. The value of its operand is negated by this unary operator. The result is false (0) if the operand is true (non-zero). The outcome is true (1) if the operand is false (0).

The logical operators && and || are examples of “short-circuit evaluation,” in which the evaluation of the right operand occurs only when the left operand is insufficient to ascertain the expression’s outcome. The precedence of logical NOT (!) is higher than that of &&, which is higher than that of ||. While! has right-to-left associativity, the operators && and || have left-to-right associativity.

Increment and Decrement Operators

The unary operators increment (++) and decrement (–) raise or lower a variable’s value by one, respectively. They offer a shortcut for adding or removing one and are widely utilised.
There are two types of these operators:

  • Prefix: ++variable or –variable. The operation is performed before the value of the operand is used in the expression.
  • Postfix: variable++ or variable–. The operation is performed after the value of the operand is used in the expression.

Operators for increment and decrement have a high precedence and are frequently combined with other unary operators. The highest precedence is occasionally given to postfix operators. While associativity for postfixes can be left-to-right, it is usually right-to-left for prefixes.

Conditional Operator (Ternary Operator)

Because it requires three operands and is the sole ternary operator in C, the conditional operator?: is special. It offers a succinct method for writing basic if-else expressions.

This is the syntax: condition? statement_if_true: statement_if_false.

  • The condition (first operand) is evaluated.
  • If the condition evaluates to true (non-zero), the value of expression_if_true (second operand) is returned.
  • If the condition evaluates to false (zero), the value of expression_if_false (third operand) is returned.

Just above the assignment operators, the conditional operator has relatively little precedence. It has a right-to-left associativity.

Here’s a C code example demonstrating the relational, logical, increment/decrement, and conditional operators:

#include <stdio.h> // Include standard input/output library for printf
int main() {
    int a = 10;
    int b = 20;
    int c = 5;
    int d = 10;
    int result;
    int counter = 0;
    // --- Relational Operators ---
    printf("--- Relational Operators ---\n");
    printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
    // a is less than b? (10 < 20) -> True (1)
    printf("a < b : %d\n", a < b);
    // a is greater than c? (10 > 5) -> True (1)
    printf("a > c : %d\n", a > c);
    // b is less than or equal to a? (20 <= 10) -> False (0)
    printf("b <= a: %d\n", b <= a);
    // b is greater than or equal to c? (20 >= 5) -> True (1)
    printf("b >= c: %d\n", b >= c);
    // a is equal to d? (10 == 10) -> True (1)
    printf("a == d: %d\n", a == d);
    // a is not equal to c? (10 != 5) -> True (1)
    printf("a != c: %d\n", a != c);
    printf("\n");
    // --- Logical Operators ---
    printf("--- Logical Operators ---\n");
    // (a < b) is True (1), (b > c) is True (1)
    // True && True -> True (1)
    printf("(a < b) && (b > c) : %d\n", (a < b) && (b > c));
    // (a == b) is False (0), (c < d) is True (1)
    // False || True -> True (1)
    printf("(a == b) || (c < d) : %d\n", (a == b) || (c < d));
    // !(a == d) : !(True) -> False (0)
    printf("!(a == d) : %d\n", !(a == d));
    printf("\n");
    // --- Increment and Decrement Operators ---
    printf("--- Increment and Decrement Operators ---\n");
    printf("Initial counter: %d\n", counter); // counter is 0
    // Prefix increment: increment first, then use the new value
    result = ++counter; // counter becomes 1, then result is assigned 1
    printf("After result = ++counter; counter = %d, result = %d\n", counter, result); // counter: 1, result: 1
    // Postfix increment: use the current value, then increment
    result = counter++; // result is assigned the current value of counter (1), then counter becomes 2
    printf("After result = counter++; counter = %d, result = %d\n", counter, result); // counter: 2, result: 1
    // Prefix decrement: decrement first, then use the new value
    result = --counter; // counter becomes 1, then result is assigned 1
    printf("After result = --counter; counter = %d, result = %d\n", counter, result); // counter: 1, result: 1
    // Postfix decrement: use the current value, then decrement
    result = counter--; // result is assigned the current value of counter (1), then counter becomes 0
    printf("After result = counter--; counter = %d, result = %d\n", counter, result); // counter: 0, result: 1
    printf("\n");
    // --- Conditional Operator (Ternary) ---
    printf("--- Conditional Operator ---\n");
    // Condition: a > b (10 > 20) is False
    // If false, return value of d (10)
    result = (a > b) ? c : d;
    printf("Result of (a > b) ? c : d : %d\n", result); // result is 10
    // Condition: a == d (10 == 10) is True
    // If true, return value of b (20)
    result = (a == d) ? b : c;
    printf("Result of (a == d) ? b : c : %d\n", result); // result is 20
    return 0;
}
Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.