Operator In C Programming

Operator In C Programming Language

With the aid of examples, you will learn about the various C programming operator in this course.

An operator is a symbol that instructs the compiler to carry out particular logical or mathematical operations. The built-in operators in the C language include the following categories:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Arithmetic Operators

In this chapter, we’ll examine each operator’s operation.

A symbol that manipulates a value or a variable is called an operator. An operator to execute addition is +, for instance.

C has a large selection of operators to carry out different operations.

Any programming language’s core is its set of operators. Operators are symbols that enable us to carry out particular logical and mathematical operations on operands. The operands are operated by an operator, in other words. As an illustration, the addition operator “+” is employed as follows:

c = a + b;

Here, the addition operators is represented by the symbol “+,” and the operands are “a” and “b.” The compiler is instructed to add both operands, “a” and “b,” by the addition operators.

C Arithmetic Operators

An arithmetic operators applies mathematical operations to numerical numbers, such as addition, subtraction, multiplication, and division (constants and variables).

OperatorMeaning of Operator
+addition or unary plus
subtraction or unary minus
*multiplication
/division
%remainder after division (modulo division)

Example 1: Arithmetic Operators

// Working of arithmetic operators
#include <stdio.h>
int main()
{
    int a = 9,b = 4, c;
    
    c = a+b;
    printf("a+b = %d \n",c);
    c = a-b;
    printf("a-b = %d \n",c);
    c = a*b;
    printf("a*b = %d \n",c);
    c = a/b;
    printf("a/b = %d \n",c);
    c = a%b;
    printf("Remainder when a divided by b = %d \n",c);
    
    return 0;
}

Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

As you would have predicted, the operators +, -, and * compute addition, subtraction, and multiplication, respectively.

The standard formula for 9/4 is 2.25. The program’s output, nevertheless, is 2, not 1.

It is because a and b are both integer variables. The output is therefore also an integer. The compiler displays response 2 rather than 2.25 because it ignores the phrase following the decimal point.

The remainder is computed using the modulo operator%. A=9 divided by B=4 leaves a residual of 1. Only integers can be used using the % operators.

Assume that an is 5, b is 2, c is 5, and d is 2. the C programming language,

// Either one of the operands is a floating-point number
a/b = 2.5
a/d = 2.5
c/b = 2.5
// Both operands are integers
c/d = 2

Operators for Increment and Decrement in C

For changing the value of an operand (constant or variable) by one, the C programming language includes two operators: increment ++ and decrement.

While decrement — reduces the value by 1, increment ++ increases it by 1. These two operators are unary, which means that they only work with one operand.

Example 2: Increment and Decrement Operators

// Working of increment and decrement operators
#include <stdio.h>
int main()
{
    int a = 10, b = 100;
    float c = 10.5, d = 100.5;

    printf("++a = %d \n", ++a);
    printf("--b = %d \n", --b);
    printf("++c = %f \n", ++c);
    printf("--d = %f \n", --d);

    return 0;
}

Output

++a = 11
–b = 99
++c = 11.500000
–d = 99.500000

Here, prefixes consist of the operations ++ and —. As with a++ and a—, these two operators can also be used as postfixes.

Operators for C Assignment

To give a variable a value, use the assignment operators. = is the most typical assignment operators.

OperatorExampleSame as
=a = ba = b
+=a += ba = a+b
-=a -= ba = a-b
*=a *= ba = a*b
/=a /= ba = a/b
%=a %= ba = a%b

Example 3: Assignment Operators

// Working of assignment operators
#include <stdio.h>
int main()
{
    int a = 5, c;

    c = a;      // c is 5
    printf("c = %d\n", c);
    c += a;     // c is 10 
    printf("c = %d\n", c);
    c -= a;     // c is 5
    printf("c = %d\n", c);
    c *= a;     // c is 25
    printf("c = %d\n", c);
    c /= a;     // c is 5
    printf("c = %d\n", c);
    c %= a;     // c = 0
    printf("c = %d\n", c);

    return 0;
}

Output

c = 5
c = 10
c = 5
c = 25
c = 5
c = 0

relationship operators in C

A relational operator verifies the relationship between two operands. It returns 1 if the relation is true and 0 if it is false.

Decision-making and looping both require a relational operator.

OperatorMeaning of OperatorExample
==Equal to5 == 3 is evaluated to 0
>Greater than5 > 3 is evaluated to 1
<Less than5 < 3 is evaluated to 0
!=Not equal to5 != 3 is evaluated to 1
>=Greater than or equal to5 >= 3 is evaluated to 1
<=Less than or equal to5 <= 3 is evaluated to 0

Example 4: Relational Operators

// Working of relational operators
#include <stdio.h>
int main()
{
    int a = 5, b = 5, c = 10;

    printf("%d == %d is %d \n", a, b, a == b);
    printf("%d == %d is %d \n", a, c, a == c);
    printf("%d > %d is %d \n", a, b, a > b);
    printf("%d > %d is %d \n", a, c, a > c);
    printf("%d < %d is %d \n", a, b, a < b);
    printf("%d < %d is %d \n", a, c, a < c);
    printf("%d != %d is %d \n", a, b, a != b);
    printf("%d != %d is %d \n", a, c, a != c);
    printf("%d >= %d is %d \n", a, b, a >= b);
    printf("%d >= %d is %d \n", a, c, a >= c);
    printf("%d <= %d is %d \n", a, b, a <= b);
    printf("%d <= %d is %d \n", a, c, a <= c);

    return 0;
}

Output

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0 5 < 10 is 1 5 != 5 is 0 5 != 10 is 1 5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

logical operator in C

Depending on whether the outcome of the expression is true or false, a term with a logical operator yields either 0 or 1. In C programming, the logical operator is frequently employed in the decision-making process.

OperatorMeaningExample
&&Logical AND. True only if all operands are trueIf c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.
||Logical OR. True only if either one operand is trueIf c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.
!Logical NOT. True only if the operand is 0If c = 5 then, expression !(c==5) equals to 0.

Example 5: Logical Operator

// Working of logical operators

#include <stdio.h>
int main()
{
    int a = 5, b = 5, c = 10, result;

    result = (a == b) && (c > b);
    printf("(a == b) && (c > b) is %d \n", result);

    result = (a == b) && (c < b);
    printf("(a == b) && (c < b) is %d \n", result);

    result = (a == b) || (c < b);
    printf("(a == b) || (c < b) is %d \n", result);

    result = (a != b) || (c < b);
    printf("(a != b) || (c < b) is %d \n", result);

    result = !(a != b);
    printf("!(a != b) is %d \n", result);

    result = !(a == b);
    printf("!(a == b) is %d \n", result);

    return 0;
}

Output

(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

Logic operator program explanation

  • Because both of the operands (a == b) and (c > b) are 1, (a == b) && (c > 5) evaluates to 1. (true).
  • Because operand (c b) is 0, the expression (a == b) && (c b) evaluates to 0. (false).
  • Because (a = b) is 1, (c == b) evaluates to 1. (true).
  • Because both operands (a!= b) and (c b) are 0, (a!= b) || (c b) evaluates to 0. (false).
  • Since (a!= b) is a 0 in the operand,!(a!= b) evaluates to 1. (false). Hence, !(a != b) is 1 (true).
  • Because (a == b) is 1,!(a == b) evaluates to 0 instead (true). as a result,!(a == b) is 0 (false).

Bitwise operator in C

Mathematical operations like addition, subtraction, multiplication, division, etc. are transformed to bit-level computations during computation to speed up processing and conserve power.

In C programming, bit-level operations are carried out via a bitwise operator.

OperatorsMeaning of operators
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
~Bitwise complement
<<Shift left
>>Shift right

Comma Operator

To join together related expressions, use the comma operator. For instance:

int a, c = 5, d;

The sizeof operator

A unary operator called sizeof returns the size of the data (constants, variables, array, structure, etc).

Example 6: sizeof Operator

#include <stdio.h>
int main()
{
    int a;
    float b;
    double c;
    char d;
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));

    return 0;
}

Output

Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

Later lectures will cover more operator including the ternary operator?:, reference operator &, dereference operator *, and member selection operator ->.

Leave a Reply