Operators in C

Here we will discuss operators in the C language. What are unary, binary, and ternary operators? How many types of operations are performed in the C language? We will learn these concepts in detail.

An operator is a special symbol designed for a particular task. Operators are used in the program to perform operations on operands. An operand is an object on which an operation is performed, it receives an operator’s action. Operators usually form a part of the mathematical or logical expressions. An expression is any computation involving variables and operators that yields a value. C language supports 44 operators.

Based on the number of operands participating in the operation, operators are divided into 3 types in the C language.

• Unary Operators:- It require only one operand. Example:- a++, a–, ~a, +a, -a, !a, sizeof(a)
• Binary Operators:- It require two operands. Example:- a+b, a-b, a!=b, a<<b e.t.c.
• Ternary Operator:- It requires three operands or expressions. Example:- a>b?a:b

Based on operations performed, operators are divided into several types.

1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Increment and decrement operators
6. Conditional operator
7. Bitwise operators
8. Other Operators

Arithmetic Operators

Arithmetic operators are used to performing mathematical operations. The arithmetic operators can operate on any built-in data type.

Syntax:- operand1 operator operand2

C has no any operator for exponentiation. There is no any meaning for statement 5//3 or 5^3 in C language.

a) Integer mode arithmetic

When arithmetic operators operates on integers then it is called integer mode arithmetic. It always yields an integer value.

integer operator integer = integer

Examples:-
1+3 = 4
‘A’ + 1 = 66
‘a’ + ‘b’ = 195
3 * 2 = 6
8 / 2 = 4
9 % 2 = 1

b) Real mode arithmetic

When operands are of real type then it is called real mode arithmetic. It always yields real value. The modulus operator(%) does not operate on real or floating-point operands.

real operator real = real

Examples:-
3.0 + 4.0 = 7.0
4.0 – 3.0 = 1.0
4.0 * 3.0 = 12.0
5.0 / 2.0 = 2.5
5.0 % 2.0 = Error

c) Mixed mode arithmetic

In mixed mode arithmetic, some of the operands are integers and some of the operands are real but the obtained result is in real.

integer operator real = real
real operator integer = real

Examples:-
4.0 + 5 = 9.0
9.0 – 4 = 5.0
3.0 * 2.0 = 6.0
5.0 / 2 = 2.5
5.0 % 2 = Error

C Program to see a demonstration of Arithmetic operators

``````#include<stdio.h>
int main()
{
int a=9, b=5;
printf("Sum = %d\n", a+b);
printf("Substraction = %d\n", a-b);
printf("Multiplaction = %d\n", a*b);
printf("Division = %d\n", a/b);
printf("Modulus = %d\n", a%b);
return 0;
}``````

Output:-

Sum = 14
Substraction = 4
Multiplaction = 45
Division = 1
Modulus = 4

Some special points for arithmetic operators in c

a) Subtraction Operator (-)

The unary minus operator has the effect of multiplying its operand by -1. Example:- 8 x (-1) = (-8)

``````#include<stdio.h>
int main()
{
int a = 9;
printf("%d \t", -a);
printf("%d \n", - -a);
return 0;
}``````

Output:-

-9 9

b) Division Operators (/)

The division operator (/) produces the quotient. The second operand should be a non-zero number. So, n using division operator (/), if exactly any one of the operand is negative then the result is negative. If both operands are -ve/+ve then result is positive.

``````#include<stdio.h>
int main ()
{
printf("9/5 = %d\n", 9/5);
printf("9/-5 = %d\n", 9/-5);
printf("-9/5 = %d\n", -9/5);
printf("-9/-5 = %d\n", -9/-5);
//Warning for below line:- division by zero
//printf("9/0 = %d", 9/0);
return 0;
}``````

Output:-

9/5 = 1
9/-5 = -1
-9/5 = -1
-9/-5 = 1

Any integer number divided by ten removes the last digit of the number. In next coming many examples we will use this concept to solve the big problems.

``````#include<stdio.h>
int main ()
{
printf("1253/10 = %d\n", 1253/10);
printf("125/10 = %d\n", 125/10);
printf("12/10 = %d\n", 12/10);
printf("1/10 = %d", 1/10);
return 0;
}``````

Output:-

1253/10 = 125
125/10 = 12
12/10 = 1
1/10 = 0

c) Modulus Operators (%)

The remainder operator or modulus operator (%) produces the remainder after integer division i.e x%y = x – (x/y)*y. The operands for modulus operator (%) should be of type integer and the second operand should be a non-zero value. The modulus operator (%) does not operate on floats and doubles. On using modulus operator(%) the sign of the remainder is always same as the sign of the numerator.

``````#include<stdio.h>
int main ()
{
printf("9%5 = %d\n", 9%5);
printf("-9%5 = %d\n", -9%5);
printf("9%-5 = %d\n", 9%-5);
printf("-9%-5 = %d\n", -9%-5);

//Second operand should be non-zero
//printf("9%0 = %d", 9%0);
//Modulus operator does not work on floating-point number
//printf("9.5%5 = %d", 9.5%5);

return 0;
}``````

Output:-

9%5 = 4
-9%5 = -4
9%-5 = 4
-9%-5 = -4

Any number%10 always gives last digits of number. After some times in big problem you need to find last digit of the number the apply % operator on number.

``````#include<stdio.h>
int main ()
{
printf("1253%10 = %d\n", 1253%10);
printf("125%10 = %d\n", 125%10);
printf("12%10 = %d\n", 12%10);
printf("1%10 = %d\n", 1%10);
return 0;
}``````

Output:-

1253%10 = 3
125%10 = 5
12%10 = 2
1%10 = 1

Assignment operator

Assignment operators are used to assign the result of an expression to a variable.

Syntax: `Identifier = expression; `

Here identifier generally represents a variable and expression represents a constant, a variable or a more complex expression. Example:-
a=10;
b=1.2;
c= ‘X’;
area = length*width;
sum=a+b;

Above statements are called assignment statements or assignment expressions. In an assignment statement, the right-side expression to the assignment operator is evaluated first and the obtained result is stored inside the left side variable to that assignment operator. So, we can say that it copies the value on its right side into the left side variable.

The left side operand of the assignment should be a variable. C does not allow any expression or constant to be placed to the left of the assignment operator.
a+b=c; // invalid, left side operand should be a variable
10 = 20; // invalid

C permits the assignment of more than one variable in one statement using multiple assignment operators.
a=b=c=5;
x=y=z=a;

Compound assignment operators

C supports a set of shorthand assignment operators. Following are the advantages of using shorthand assignment operators:

• Shorthand expression is easier to write as the expression on the left side need not be repeated.
• The statement involving shorthand operators are easier to read as they are more concise.
• The statement involving shorthand operators are more efficient and easy to understand.
``````// sample program for Compound assignment operator
#include<stdio.h>
int main()
{
int x=10, a=5;
x += a; // x=x+a
printf("x+=a=%d  ",x);
x = 10;
x -= a;//x=x-a
printf("x-=a=%d",x);
return 0;
}``````

Output:-

15 5

Relational Operator

Relational operator are used to check given condition or expression is true or false. Combination of some operands and constants with relational operators is called a relational expression.

Syntax:- operand1 relational-operator operand2

In the above syntax, operand1 and operand2 can be constants, variables or complex expressions.

If the relation is true then the value of the relational expression is 1 and if the relation is false then the value of the expression is 0. In C language (with relational operator), every non-zero value is 1 i.e. true.

Some more examples are:-
9 == 9 result 1
9 == -9 result 0
9 == 9.0 result 1
2+3*2 == 10 result 0
(2+3)*2 == 10 result 1

``````// demonstration of relational operators
#include<stdio.h>
int main()
{
int a=5, b=9;
printf("%d\t",a>b);
printf("%d\t",a>=b);
printf("%d\t",a<b);
printf("%d\t",a<=b);
printf("%d\t",a==b);
printf("%d\t",a!=b);
return 0;
}``````

Output:-

0 0 1 1 0 1

Some special points for relational operators

The relational operators `<=` and `>=` are only supported in the form shown. In particular =< and => are both invalid and do not mean anything.

Characters are valid operands since they are represented by numeric values (ASCII values).
‘A’ == 65 results 1 //ASCII value of ‘A’ is 65
‘a’ > ‘A’ result 1 //ASCII value of them are 97 and 65 respectively
‘A’ == ‘A’ result 1

The relational operators should not be used for comparing strings because this will result in string addresses being compared, not string contents.
“A” == “a” result 0
“Hello” < “Hi” result 0

Logical operators

C language supports three logical operators:- AND (&&), OR(||) and NOT(!). An expression which combines two or more relational expressions is termed as logical expression. Like the simple relational expressions, a logical expression also yields a value of true (1) or false(0).

Logical AND (&&) gives 0 (false) if anyone or both operand is 0 (false) otherwise it gives 1(true) as a result. The truth table for logical AND (&&) is given below:-

Logical OR ( | | ) gives result 1 (true) if atleast one operand is 1 (true) otherwise it gives result 0 (false). Truth table for logical OR (| |) is given below:-

The logical NOT (!) is an unary operator that negates the logical value of its single operand. If the operand is 0 (false) it produces 1 (true) and if the operand is 1 (true) it produces 0 (false). Truth table for logical NOT (!) is given below:-

Examples of logical expressions:-
Example1 :- (‘a’ > ‘A’) && (8 <=10) gives 1 From above expression,‘a’ > ‘A’ gives 1; 8 <=10 gives 1; Finally 1 && 1 produces 1.
Example2:- (1.4 <=1) || (7!=3) gives 1
From above expression, 1.4 <=1 gives 0; 7!=3 gives 1; finally 0 || 1 produces 1.

Q) Find the output of the below program?

``````#include<stdio.h>
int main()
{
printf("%d ",'a'&&'A');
printf("%d ", 0 || -5);
printf("%d ", !(! 5%5 && !'A'));
printf("%d ", !('a' - 32 == 'A'));
return 0;
}``````

Output:-

1 1 1 0

Some special points on logical operators

For logical AND (&&) operator, if the left operand yields a false (0) value, then the compiler does not evaluate the right operand and directly gives result false (0). Because in logical AND operand if anyone operand is false then the expression is false. Find the output of below two programs after learning increment and decrement operators.

``````#include<stdio.h>
int main()
{
int a=0, b=1;
printf("%d ", a++ && ++b);
printf("%d %d",a,b);
return 0;
}``````

Output:-

0 1 1

For logical OR ( || ) operator, if the left operand yields a true (1) value, then the compiler does not evaluate the right operand and directly gives result 1 (true). We know that for the logical OR operator if anyone operand is true than expression is also true.

``````#include<stdio.h>
int main()
{
int x = 1, y=0;
printf("%d ", ++x || ++y);
printf("%d %d",x,y);
return 0;
}``````

Output:-

1 2 0

Increment and Decrement operators

Increment and decrement operators are also known as unary operators’ because they operate on a single operand. The increment operator (++) adds 1 to its operand and decrement operator (–) subtracts one.

Syntax:-
` ++variable;` // pre-increment operator
` variable++; `//post-increment operator
` --variable;` // pre-decrement operator
` variable--; `//post-decrement operator

x=x+1; and x+=1; is the same as x++;
x=x-1; and x-=1; is the same as x–;

If it is pre operator, the value of the operand is incremented (or decremented) before it fetched for the computation. The altered value is used for the computation of the expression in which it occurs. If it is a post operator, the value of the operand is altered after it is fetched for the computation. The unaltered value is used in the computation of the expression in which it occurs.

Increment operator (++)

1. Prefix operation (x = ++a; )

• First, the value of `a` incremented by 1 and store in the memory location of variable `a`.
• Second, the value of variable `a` assign to the variable x.
``````#include<stdio.h>
int main()
{
int x, y;
x=5;
y=++x;
printf("%d ",x);
printf("%d",y);
}
//Output:- 6 6``````

2. Postfix operation (x = a++)

• First, the value of the variable `a` will assign to the variable x.
• Second, the value of `a` incremented by 1 and store in the memory location of the variable `a`.
``````#include<stdio.h>
int main()
{
int x, y;
x=5;
y=x++;
printf("%d ",x);
printf("%d",y);
}
//Output:- 6 5``````

Decrement operator(- -)

1. Prefix operation (x = – -a; )

• First, the value of `a` decremented by 1 and store in the memory location of variable `a`.
• Second, the value of variable `a` assign to the variable x.
``````#include<stdio.h>
int main()
{
int x, y;
x=5;
y=--x;
printf("%d ",x);
printf("%d",y);
}
// Output:- 4 4``````

2. Postfix operation (x = a- -;)

• First, value of variable `a` will assign to the variable x.
• Second, the value of `a` decremented by 1 and store in the memory location of the variable `a`.
``````#include<stdio.h>
int main()
{
int x, y;
x=5;
y=x--;
printf("%d ",x);
printf("%d",y);
}
//Output:- 4  5``````

Special points for increment and decrement operator

1. It is to be noted that i++; executed faster than i=i+1; and i+=1

2. With increment and decrement operators, the operand must be a variable but not a constant or an expression.

``````//a.c
#include<stdio.h>
int main()
{
int a=3, b=9;
printf("%d\n",++(a*b+1));
printf("%d",++5);
return 0;
}``````
``````a.c: In function ‘main’:
a.c:5:17: error: lvalue required as increment operand
printf("%d\n",++(a*b+1));
^~
a.c:6:15: error: lvalue required as increment operand
printf("%d",++5);``````

lvalue means, that there isn’t a variable the operation is supposed to be performed on.

3. Increment and decrement operators have more precedence than arithmetic operators. So, if c=a+++b will calculated as c=(a++)+b; not as c=a+(++b).

``````#include<stdio.h>
int main()
{
int a=2,b=1,c;
c = a+++b;
printf("%d %d %d",a,b,c);
return 0;
}
//Output:- 3 1 3``````

Conditional Operator

Syntax:- `Variable = expression1? expression2 : expression3;`

First, the expression1 is evaluated; it is treated as a logical condition. If the result is non-zero, then expression2 is evaluated and its value is the final result. Otherwise, expression3 is evaluated and its value is the final result.

This operator is also called a ternary operator because it operates on three operands. The operands may be an expression, constants or variables.

``````#include<stdio.h>
int main()
{
int x;
x=5>2?1:0;
printf("%d",x);
return 0;
}
// output:- 1``````

Here 5>2 So, Expression will be evaluated to the variable x.

The conditional operator works similar to the conditional control statement if-else. We will discuss the if-else concept in control flow statements.

Bitwise Operator

A bitwise operator operates on each bit of data. It is one of the low-level programming language features. Bitwise operator operates on integer only, not on floating-point numbers. Bitwise operations most often find application in device drivers such as modem programs, disk file routines, and printer routines.

List of bitwise operators are:-

The bitwise AND, OR, and NOT are governed by the same truth table as their logical equivalents, except that they work bit by bit.

Bitwise Operator is discussed in more details in the next tutorial:- Bitwise operators in C

Other Operators

Except for above-discussed operators C support some other operators. Those are given below:-

We will discuss here `sizeof()` and the comma operator. Other operators will be discussed later on their own topics.

There are two address operator & and *.

1. `&`:- It gives the address of a variable
2. `*`:- It gives value at the address of a variable (pointer).

We will learn about these operators in more details in the pointer concept.

Sizeof() operator

The operator `sizeof()` is a unary compile-time operator that returns the length, in bytes, of the variable or parenthesized type-specifier that it precedes.

To compute the size of data type, we must enclose the data type name in parentheses.

``````#include<stdio.h>
int main()
{
printf("%ld ",sizeof(char));
printf("%ld ",sizeof(int));
printf("%ld ",sizeof(long));
printf("%ld ",sizeof(float));
printf("%ld ",sizeof(double));
printf("%ld ",sizeof(long double));
return 0;
}
// Output:- 1 4 8 4 8 16``````

To compute the size of the variable or constants, parentheses are optional.

``````#include<stdio.h>
int main()
{
int a=10;
char b='A';
printf("%ld ",sizeof a);
printf("%ld ",sizeof b);
printf("%ld ",sizeof 2);
printf("%ld ",sizeof 2.5);
printf("%ld ",sizeof 'A');
printf("%ld ",sizeof "A");
printf("%ld ",sizeof 2U);
return 0;
}
// Output:- 4 1 4 8 4 2 4``````
``````#include<stdio.h>
int main()
{
int a=2;
printf("%ld ",sizeof(a+3));
printf("%ld ",sizeof a+3);
printf("%ld ",sizeof(a*2.5));
printf("%ld ",sizeof(a/1.0F));
return 0;
}
// Output:- 4 7 8 4``````

Note the difference between the 4th and 5th lines. `size(a+3)` gives 4 bytes but `sizeof a+3` gives 7 bytes as a result.

Comma Operator

This operator allows the evaluation of multiple expressions, separated by the comma, from left to right in order and the evaluated value of the rightmost expression is excepted as the final result.

Syntax:- `variable = (expression1, expression2, ......., expresionN);`

The comma operator allows us to evaluate multiple expressions whenever a single expression is allowed. The comma operator evaluates to its rightmost operand.

``````#include<stdio.h>
int main()
{
int i = 0;
int j ;
j = (i=i+1, i=i+2, i=i+3);
printf("%d",j);
return 0;
}
// Output:- 6``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!