Share this post on:

Operators in C Programming Specifies and operation to be performed that yields a value. The variable, constant can be joined by various operators to form an expression. An operand is a data item on which an operator acts. Some operators require two operands, while others act upon only one operand.

C includes a large number of operators that fall under several different categories, Which are :-

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Relational Operators
  • Logical Operators
  • Condition Operators
  • Comma Operators
  • Sizeof Operators
  • Bitwise Operators
  • Other Operators

Arithmetic Operators : Arithmetic Operators are used for  numeric Calculations.

They are of two Types:

  1. Unary arithmetic operators
  2. Binary arithmetic operators

Unary Arithmetic Operators : Unary arithmetic require only one operand.

For Example : +x               -y

Here “-“ changes the sign of the operand y.

Binary Arithmetic Operators:  Binary operators requires two operand.

There are five binary arithmetic operators:

1.       +                             Addition

2.                                     Subtraction

3.       *                             Multiplication

4.       /                              Division

5.       %                            gives the remainder in integer division

 

%(modulus operators) cannot be applied with floating point operands. There is no exponent operators in C, however there is a library function pow.() to carry out exponentiation operators. Unary plus and Unary minus operators are different from the addition and subtraction operators.

 

When both operands are integers , the resulting value is always an integer. Let us take two variable a and b. The value of a 17 and b is 4, the results of the following  operators:

 

 

Expression

 

Result

 

a+b

 

21

 

a-b

 

13

 

a*b

 

68

 

a/b

 

4(decimal part truncate)

 

a%b

 

1(reminder after integer division)

 

After division operation the  decimal part will be truncated and result is only integer part of quotient. After modular operation the result will be remainder part of integer division. The second operands must be nonzero for division and modulus operations.

 

Example:

#include <stdio.h>

{

int a=14,b=4;

printf(*Sum=%d\n”,a+b);

printf(*Difference=%d\n”,a-b);

printf(*Products=%d\n”,a*b);

printf(*Quotient=%d\n”,a/b);

printf(*Remainder=%d\n”,a%b);

return 0;

}

 

Assignment Operators : A Value can be stored in a variable using assignment operator. The operand on the left hand side should be a variable, while the operand on the right hand side can be any variable, constant or expression. The value of right hand operand is assignment to left hand operand . here are some example of assignment
expression:

x=8

y=5

S=x+y-2

Y=x

x=y

 

The value that is being assignment is considered as value of the assignment expression. For example x=8 is an assignment expression whose value is 8.

 

Here all the three variables x, y, x will be assigned value 20, and the value of the whole expression will be 20. If we put a semicolon after the assignment expression then it becomes an assignment statement.

For example  these are assignment statement:

X=8;

Y=5;

S=x+y-2;

X=y=z=20;

Increment and Decrement Operators:  The increment (++) and decrement(–) operators are unary operators because they operate on a single operand. The increment operator increment the value of the variable by 1, While decrement operator decrements the value of the variable by 1.

++x                         is equivalent to                 x=x+1

–x                          is equivalent to                 x=x+1

 These operators should be used only with variable; they can’t be used with constant or expressions. For example the expression ++3 or (x+ y+ z) are invalid.

 

Prefix Increment / Decrement : Here first the value of variable increment/decrement then the new value is used in the operation. Let us take a variable x whose value is 3.

The statement y=++x means first increment the value of x by 1, then assign the value if x to y. This single statement is equivalent to these two statement :

x=x+1;

y=x;

Now value of x is 4 and value of y is also 4.

The statement y=–x ; means first decrement the value of x by 1 then assign the value of x to y.

This statement is equivalent to these two statement.

X=x+1;

Y=x;

Now the value of x=3 and value of y is also 3.

 

Example:

#inclue <stdio.h>

int main(void)

{

int x=8;

printf(“x=%d\t”,x);

printf(“x=%d\t,++x);

printf(“x=%d\t,x);

printf(“x=%d\t,–x);

printf(“x=%d\n”,x);

return 0;

}

 

Output: x=8        x=9         x=9         x=8         x=8

 

Relational Operators :  Relational Operators are used to compare value of two expressions. An expression that contains relational operators is called relational expression. If the relational is true then the value of relational expression is 1 and id the relation is false then the value of expression is 0.

The relational operators are:-

 

Operators

 

meaning

 

 

Less than

 

<=

 

Less than or equal to

 

==

 

Equal to

 

!=

 

Not equal to

 

 

Greater than

 

>=

 

Greater then or equal to

Let us take two variable a=9 and b=5, and form simple relational expressions with them :=

 

Expression

 

Relational

 

Value of expression

 

a<b

 

False

 

0

 

a<=b

 

False

 

0

 

a==b

 

False

 

0

 

a!=b

 

True

 

1

 

a>b

 

True

 

1

 

a>=b

 

True

 

1

 

a==b

 

False

 

0

 

The relational operators are generally used in if else construct and loops. In our next program we will use the if statement to illustrate the use of relational operators. The if control statement evaluates an expression, and if this expression is true(non zero) then the next statement is executed, otherwise the next statement is skipped.

 

Logical or Boolean Operators :  An expression that combines two or more expression is termed as a logical expression. For combining these expressions we use logical operates. These operators return 0 for false and 1 for true. The operands may be constant, variable or expressions.

C has three logical operators :

 

Operators

 

 Name

 

&&

 

AND

 

||

 

OR

 

!

 

NOT

 

Here logical not is unary operator while the other tow are binary operators. Before studying these operators let us understand the concept of true and false. In c any non-zero is regarded as true and zero regarded as false.

 

Conditional Operators : Conditional Operators is a ternary operator (? and 🙂 which requires three expressions as operands. It is written as –

TextExpression ? expression1 : expression

Firstly the TestExpression is evaluated.

  1. If TestExpression is true(nonzero), then expression1 is evaluated and it becomes the value of the overall conditional expression.
  2. If TestExpression is false(zero), then expression2 is evaluated and it becomes the value of overall conditional expression.

for example consider this conditional expression –

a = b ? a : b

Here first the expression a>b is evaluated , if the value is true then the value of variable a becomes the value of conditional expression otherwise the value , and of b becomes the value of conditional expression.

Suppose a=5 and b=8 and we use the above conditional expression in a statement as –

max = a > b ? a : b;

First the expression a>b is evaluated, it is false so the value of b becomes the value of conditional expression and it is assigned to variable max.

Example :

#include<stdio.h>

int main(void)

{

int a,b,max;

printf(“Enter a value for a and b: “);

scanf(“%d%d”, &a,&b);

max = a>b ? a : b;

printf(“Larger of %d and %d is %d\n”,a,b,max);

return 0 ;

}

Output:

Enter value for a and b :12 7

Larger of 12 and 7 is 12

Comma Operator : The comma operator ( , ) is used to permit different expressions to appear in situations where only one expression would be used. The expressions are separated by the comma operator. The separated expressions are evaluated from left to right and the type and value of the rightmost expression is the type and value of the compound expression.

For example consider this expression –

a=8, b=7, c=9, a +b + c

Here we have combined 4 expressions, initially 8 is assigned to variable a, then 7 is assigned to the variable b, 9 is assigned to variable to c and after this a +b +c is evaluated which becomes the value of whole expression. So the value of the above expression is 24, Now consider this statement –

sum = (a=8, b=7, c=9, a +b +c);

here the value of the whole expression on right side will be assigned to variable sum that is sum will be assigned value 24. Since precedence of comma operator is lower than that of assignment operator, the parentheses are necessary here. The comma operator helps make the code more compact, for example without the use of comma operator the above task would have been done in 4 statement.

a=8;

b=7;

c=9;

sum=a +b +c;

Example:

#include<stdio.h>

int main(void)

{

int a, b, c, sum;

sum=(a=8, b=7, c=9, a +b +c);

printf(“Sum=%d\n”,sum);

return 0;

}

Output:

sum=24

sizeof Operator : sizeof is an unary operator that gives the size of its operand in terms of bytes. The operand can be a variable, constant or any datatype(int, float, char etc), for example sizeof (int) gives the byte occupied by the int datatype.

Example:

#include<stdio.h>

int main(void)

{

int var;

printf(“Size of int=%u\n”,sizeof(int));

printf(“Size of float=%u\n”,sizeof(float));

printf(“Size of var=%u\n”,sizeof(var));

printf(“Size of an integer constant =%u\n”, sizeof(45));

return 0;

}

Generally sizeof operator is used to make portable program that is program that can be run on different machines. For example if we write our program assuming int to be of 2 bytes, then it won’t run correctly on a machine on which int is of 4 bytes. s to make general code that can run on all machine we can use sizeof operator.

Type conversion

Bitwise Operator: C has the ability to support manipulated of data at the bit level. Bitwise operator operate on integer only and they are used for operation on individuals bits. The bitwise operators are –

Bitwise OperatorName
&Bitwise AND
|Bitwise OR
~One’s compliment
<< Left shift
>> Right shift
^Bitwise XOR

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

One Comment

  1. Pingback: What is Pointer is C Programming - ArzaTechs

Leave a Comment

Your email address will not be published. Required fields are marked *