Operators in C

Operators in C


One of the most important features of C is that it has a very rich set of built in operators including arithmetic, relational, logical, and bitwise operators.

An operator can be any symbol like + - * / that specifies what operation need to be performed on the data.

For ex: + indicates addition operation
* indicates multiplication operation

An operand can be a constant or a variable.

An expression is combination of operands and operators that reduces to a single value.

For ex: Consider the following expression a + b here a and b are operands, while + is an operator.

Types of C operators

C language offers many types of operators.

  • Arithmetic operators.
  • Assignment operators.
  • Relational operators.
  • Logical operators.
  • Bit wise operators.
  • Conditional operators (ternary operators).
  • Increment/decrement operators.
  • Special operators.


Arithmetic Operators


An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).


Operator
Description
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (Remainder )



Assignment Operators in C


An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

It assign the value from right to left side variable.


Operator
Description
Explanation
=Simple Equal
C = A + B will assign the value of A + B to C
+=Add & assignment
C += A is equivalent to C = C + A
-=Subtract & assignment
C -= A is equivalent to C = C - A
*=Multiply & assignment
C *= A is equivalent to C = C * A
/=Divide & assignment
C /= A is equivalent to C = C / A
%=Modulus & assignment
C %= A is equivalent to C = C % A

Relational Operators in C


A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.


Operator
Description
>greater than 
<less than 
>=greater than or equal to 
<=less than or equal to 
==equal to 
!=not equal to 

Logical Operators in C


Logical operators are used to combine two or more condition for building complex expression.


Operator
Operator NameDescription
&&ANDLogical AND. True only if all operands are true
||ORLogical OR. True only if either one operand is true
!NOTLogical NOT. True only if the operand is 0


Increment and Decrement Operators in C


Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.

Type of Increment Operator

  • Pre increment/decrement Operator (++a, --a)
  • Post increment/decrement Operator (a++, a--)

In pre increment / decrement first increment/decrement the value of variable and then used inside the expression (initialize into another variable)

In post-increment / decrement first value of variable is used in the expression (initialize into another variable) and then increment / decrement the value of variable.

Conditional Operator in C


The conditional operator (? :) is a ternary operator. This operator needs 3 operands to perform the operation.

Syntax:

Variable = <test expression> ? <expression 1> : <expression 2>

Or

<test expression> ? <expression 1> : <expression 2>



The conditional operator works as follows:

 Test Expression is Condition

 Expression 1 is Statement Followed if Condition is True

 Expression 2 is Statement Followed if Condition is False


Example 1:

int num1=10,num2=20,max;
max=(num1>num2)?num1:num2; printf("Max is %d",max);

The output is:

max is 20

Example 2:

int num=45;
(num%2==0)?printf("Number is Even"):printf("Number is Odd");

The Output is:

Number is Odd


CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad