C Operators ?


Operators are a useful and powerful feature of the C/C++ programming language. Without them, the functionality of C is useless. It makes it easy for a programmer to write codes very efficiently and easily.

“An operator is a symbol that specifies the operation to be performed on various types of operands.”(operand may be a constant or variable or function which return a value). An operator may be associated with one or two or three operands.


Example:-   a + b

                    a - b

                    a * b


Note: In these expressions the identifiers a and b are operands and the symbols +, -, *. are operators.

Classification of Operators based on operands

  1. Unary Operators:-An operator that operates on one operand to produce a result is called unary operator.
    Example:--10,-a, *b etc.

  2. Binary Operators:-An An operator that operates on two operands in an expression to produce a result is called a binary operator.
    Example:-a+b, a*b etc.

  3. Ternary Operators:-An operator that operates on three operands to produce a result is called a ternary operator.
    Example:-a?b :c

Classfication based on the types of operation:-

Operators Symbols
Arithmetic Operators +, - , *
Assignment Operators = , +=
Increment/Decrement Operators ++ , --
Relational Operators < , >= , > ,<=
Logical Operators && , ||, !,
Conditional Operators ?:
Bitwise Operators & , ^
Special Operators ,

Arithmetic Operator

The operators that are used to perform arithmetic operations such as addition( + ), subtraction( - ), multiplication( * ), division( / ), and modulus (%) operations are called arithmetic operators.


Relational Operator

The operators like <, >, =, != etc. that are used to find the realtionship between two operands are called relational operators.

Logical Operator

Logical operators are used to check (or) compare the logical relations between the expressions. Logical operator, returns 1 if given condition is true, 0 if given condition is false.


Note:

In C language, the output of logical operations depends on output of relational operations. So, output of logical expression is true denoted by 1 or false denoted by 0(zero).


Assignment Operator

An operator which is used to assign the data or result of an expression into a variable is called an assignment operator    ( = ).

Increment Operators

++ is an increment operator. This is a unary operator. It increments the value of a variable by one.

Types of increment operators:-

  • Post increment:-If the increment operator ++ is placed immediately after(post) the operand.
    Example:-
    void main()
    {
    int a=20;
    a++;
    printf("%d",a)
    }

    Output :-
    a=21

    In this program, operand value is used first and then the operand value is incremented by 1.

  • Pre increment:-If the increment operator ++ is placed before (pre) the operand, then the operator is called pre-increment.
    Example:-
    void main()
    {
    int a=20;
    ++a;
    printf("%d",a)
    }

    Output :-
    a=21

    In this program, pre-increment means increment before(pre) the operand value is used. So,the operand value is incremented by 1 and this is incremented value is used.
Decrement Operators

-- is a decrement operator. This is a unary operator. It decrements the value of a variable by one.

Types of decrement operators:-

  • Post decrement:-if the decrement operator -- is placed after(post) the operand, then the operator is called post decrement.
    Example:-
    void main()
    {
    int a=10;
    a--;
    printf("%d", a);
    }

    Output :-
    a=9;

    operand value is used first and then the operand value is decremented by 1.

  • Pre-decrement:-if the decrement operator -- is placed before(pre) the operand, then the operator is called pre-decrement.
    Example :-
    void main()
    {
    int a=10;
    --a;
    printf("%d", a);
    }
    Output :-
    a=9;

    the operand value is decremented by 1 first and this decremented value is used.

Conditional Operator

The conditional operator is sometimes called a ternary operator because it involves three operands. The conditional operator contains a condition followed by two statements or values. If the condition is true the first statement is executed, otherwise the second statement or value.
(exp1)?exp2:exp3;


Bitwise Operator

The operators that are used to manipulate the bits of given data are called bit wise operators.


Bitwise Operators Symbols
Bit-wise Negate ~
left shift <<
Right shift >>
Bit-wise and &
Bit-wise xor ^
Bit-wise or |

Special Operators

There are various types of special operators.

  • Comma Operators(The Comma operator can be used to link the related expressions together.)
  • sizeof Operator(sizeof returns the size of a variable or datatype.)
  • pointer Operator(it is a complement of &. It is a unary operator that returns the value of the variable at the address specified by its operand.)
  • address Operator(The reference operator noted by ampersand ("&"), is also a unary operator in c languages that uses for assign address of the variables. It returns the pointer address of the variable. )