C – Operators

After reading this C operators topic, you will understand its theory and examples also you will able to use various types of C operators in C programming.

In our routine life, we also use some operators like plus (+), minus (-), etc. to perform simple arithmetic operations like addition, subtraction, etc. That’s why C language provides us a set of operators to perform a specific operation.

An operator is a symbol that defines the kind of operation that compiler has to perform. C programming having various types of operators are classified as

  • Arithmetic operators
  • Assignment operators
  • Increment and decrement operators
  • Relational operators
  • Logical operators
  • Ternary/conditional operators
  • Bitwise operators
  • Special operators

C – Arithmetic Operators

Arithmetic operator used to perform arithmetic operation on variables or constants. For explanation purpose, we will discuss arithmetic operators in table form as shown below,

OperatorOperationExampleResult
+Addition4+26
-Subtraction4-22
*Multiplication4*28
/Division4/22
%Modulo4%20

Example:

Program (1): To demonstrate the use of arithmetic operators.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a,b,sum,sub,mul,div,modu;
printf("Enter two numbers\n");
scanf("%d %d",&a,&b);
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
modu=a%b;
printf("Addition is %d\n",sum);
printf("Subtraction is %d\n",sub);
printf("Multiplication is %d\n",mul);
printf("Division is %d\n",div);
printf("Remainder is %d",modu);
getch();
}

Output (1)

Enter two numbers
23
12
Addition is 35
Subtraction is 11
Multiplication is 276
Division is 1
Remainder is 11

C – Logical Operators:

Logical operators used to combine two or more expression having the relational operator. Logical operators present in expression returns zero when the condition is evaluated as false otherwise it returns non-zero when the condition is evaluated as true. For explanation purpose, we will discuss arithmetic operators in table form as shown below,

OperatorOperationExampleResult
!Notint a=5,c;
c=!a;
c=0 (False)
&&Andint a=5,b=0;
int c=a&&b;
c=0 (False)
||Orint a=5,b=0;
int c=a||b;
c=1 (True)

Example:

Program (1): To demonstrate the use of logical operators.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a=0,b=10;
printf("a=%d,b=%d\n",a,b);
if(a&&b)
printf("a AND b result true\n");
if(a||b)
printf("a OR b result true\n");
printf("NOT a = %d",!a);
getch();
}

Output (1)

a=0,b=10
a OR b result true
NOT a = 1

C – Relational Operators:

Relational operator can be used in expression or to compare two numbers or characters.  For explanation purpose, we will discuss relational operators in table form as shown below,

OperatorOperation
!=Not equal to
<Less than
>Greater than
<=Less than equal to
>=Greater than equal to

The Relational operators with examples are

Example:

Program (1): To demonstrate the use of relational operators.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a=10,b=20;
printf("a=%d,b=%d\n",a,b);
if(a==b)
printf("a is equal to b\n");
if(a!=b)
printf("a is not equal to b\n");
if(a>b)
printf("a is greater than b\n");
if(a<b)
printf("a is less than b\n");
if(a>=b)
printf("a greater than or equal to b\n");
if(a<=b)
printf("a less than or equal to b\n");
getch();
}

Output (1)

a=10,b=20
a is not equal to b
a is less than b
a less than or equal to b

C – Increment and Decrement Operator:

Increment operator adds value 1 to a variable whereas decrement operator subtracts value 1 from a variable. The symbol of increment operator is ++ and the symbol of decrement operator is –. Both increment and decrement operators are unary operator i.e. an operator works on a single variable. Increment and decrement operator can be used as prefix or postfix with variable.

Program (1): To demonstrate the use of Increment and Decrement operators.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a=10,b=20,c,d;
printf("a=%d,b=%d\n",a,b);
a++;
b++;
printf("increment, a++ & b++, a=%d,b=%d\n",a,b);
a--;
b--;
printf("decrement, a-- & b++, a=%d,b=%d\n",a,b);
c=++a;
printf("Pre increment, c=++a, c=%d,a=%d\n",c,a);
c=a++;
printf("Post increment, c=a++, c=%d,a=%d\n",c,a);
d=--b;
printf("Pre decrement, d=--b, d=%d,b=%d\n",d,b);
d=b--;
printf("Post decrement, d=b--, d=%d,a=%d\n",d,b);
getch();
}

Output (1)

a=10,b=20
increment, a++ & b++, a=11,b=21
decrement, a-- & b++, a=10,b=20
Pre increment, c=++a, c=11,a=11
Post increment, c=a++, c=11,a=12
Pre decrement, d=--b, d=19,b=19
Post decrement, d=b--, d=19,a=18

C – Assignment Operators:

Assignment operators, assign a value to variable, constant or expression. For explanation purpose, we will discuss assignment operators in table form as shown below,

OperatorStatement in C programResult in C
+=a+=b;a=a+b;
-=a-=b;a=a-b;

*=
a*=b;a=a*b;
/=a/=b;a=a/b;
%=a%=b;a=a%b;
<<=a<<=b;a=a<
>>=a>>=b;a=a>>b;
&=a&=b;a=a&b;
^=a^=b;a=a^b;

Example:

Program (1): To demonstrate the use of Assignment operators.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a,b;
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
printf("Result of a=4 and b=2 gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a+=b;
printf("Result of a+=b gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a-=b;
printf("Result of a-=b gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a*=b;
printf("Result of a*=b gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a/=b;
printf("Result of a/=b gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a%=b;
printf("Result of a%=b gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a<<=b;
printf("Result of a<<=b gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a>>=b;
printf("Result of a>>=b gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a&=b;
printf("Result of a&=b gives a is %d,b is %d\n",a,b);
a=4,b=2;
printf("a=%d,b=%d\n",a,b);
a^=b;
printf("Result of a^=b gives a is %d,b is %d\n",a,b);
getch();
}

Output (1)

a=4,b=2
Result of a=4 and b=2 gives a is 4,b is 2
a=4,b=2
Result of a+=b gives a is 6,b is 2
a=4,b=2
Result of a-=b gives a is 2,b is 2
a=4,b=2
Result of a*=b gives a is 8,b is 2
a=4,b=2
Result of a/=b gives a is 2,b is 2
a=4,b=2
Result of a%=b gives a is %d,b is %d
a=4,b=2
Result of a<<=b gives a is 16,b is 2
a=4,b=2
Result of a>>=b gives a is 1,b is 2
a=4,b=2
Result of a&=b gives a is 0,b is 2
a=4,b=2
Result of a^=b gives a is 6,b is 2

C – Conditional Operator:

Conditional operators( ? : ) also known as ternary operator because it uses three expressions. It generally replaces the if-else statement and makes the program shorter. The general form (syntax) is:

(condition? Expression1:Expression2);

If condition evaluated to true, then Expression 1 executes and if condition evaluates to false then Expression 2 executed.

Example:

Program (1): To find the largest number between two numbers using the Conditional Operator.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a,b,lar;
printf("Enter two numbers\n");
scanf("%d %d",&a,&b);
//conditional operator
lar=((a>b)?a:b);
printf("The largest number is %d",lar);
getch();
}

Output (1)

Enter two numbers
3
6
The largest number is 6

C – Bitwise Operators:

Bitwise operators use to perform operations on bits. A bit is either 0 or 1.

Example:

Program (1): To demonstrate the use of Bitwise operators.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int c,a=0,b=1;
printf("a=%d,b=%d\n",a,b);
if(a&b)
printf("a&b result 1\n");
if(a|b)
printf("a|b result 1\n");
if(a^b)
printf("a^b result 1\n");
c=~a;
printf("~a = %d\n",c);
a=10;
printf("a=%d\n",a);
c=a<<2;
printf("c=a<<2 gives a=%d c=%d\n",a,c);
a=10;
printf("a=%d\n",a);
c=a>>2;
printf("c=a>>2 gives a=%d c=%d\n",a,c);
getch();
}

Output (1)

a=0,b=1
a|b result 1
a^b result 1
~a = -1
a=10
c=a<<2 gives a=10 c=40
a=10
c=a>>2 gives a=10 c=2

C – Special Operators:

These operators are used to perform the particular type of operation and they are:

  • Comma operator (,).
  • Size of operator.

Comma operator (,):

This operator uses to join two expressions and make the program more compact.

For example:

Let us consider two statements:

int a=5;
int b=10;

The above two statements are written using comma (,) operator as:

int a=5, b=10;

Size of operator:

This operator returns the size in bytes occupied by operand in memory. The operand may be a variable, a constant or a data qualifier.

The general form is:

sizeof(operand);

Program (1): To demonstrate the size of operator .

#include<stdio.h>
#include<conio.h>
void main(void)
{
printf("Size of integer is %d\n",sizeof(int));
printf("Size of float is %d\n",sizeof(float));
getch();
}

Output (1)

Size of integer is 4
Size of float is 4

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

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