Offers
Thanks for visiting www.technoexam.com. Stay connected with us for more latest programming updates...

You are here : Home >> Tutorials >> C

Go
Basic C Tutorial

Contents
What is C ?
History
Structure of C Program
Variables & Keywords
Constants
Data Types
Operators
Operators Precedence
Conditional Statements
if Statement
if else Statement
nested if else
switch case
Looping Statements
While loop
Do While loop
For loop
Break
Continue
Goto
Functions
Passing Value
Returning Value
Passing & Returning
Recursion
Storage Class
Array
Structure
Pointer
Union
Strings
Header File
Programs Library
Projects Library
Learn More+

Contact Us

+91-8806583119

Operators in C

"Operator is a symbol that is used to perform mathematical operations."

We can use operators in c to perform mathematical operations, to check conditions, value assignment to variable, changing the value of a variable.

Followings are the most commonly used operators in C language.

Operator Name Operators
Assignment =
Arithmetic +, -, *, /, %
Logical &&, ||, !
Relational <, >, <=, >=, ==, !=
Shorthand +=, -=, *=, /=, %=
Unary ++, --
Conditional ()?:;
Bitwise &, |, ^, <<, >>, ~

1. Assignment Operator

It is used to assign a value to variable.

// Program to demonstrate assignment operator.
#include <stdio.h>
#include <conio.h>
int main(void)
{
	int a,b;
	clrscr();
	a = 53;
	printf("\n\t Value of A : %d",a);       // 53
	b = a;	// Interchange of value using assignment
	printf("\n\n\t Value of B : %d",b);    // 53
	getch();
	return 0;
}

2. Arithmetic Operators

It is also called as 'Binary operators'. It is used to perform arithmetical operations. These operators operate on two operands.

// Program to demonstrate arithmetic operators.
#include <stdio.h>
#include <conio.h>
int main(void)
{
	int a,b,c,d,e,f,g;
	clrscr();
	printf("\n\t Enter First Number :");    // 5
	scanf("%d",&a);
	printf("\n\t Enter Second Number :");   // 2
	scanf("%d",&b);
	c = a + b;
	printf("\n\n\t Addition is : %d",c);   // 7
	d = a - b;
	printf("\n\n\t Subtraction is : %d",d);  // 3
	e = a * b;
	printf("\n\n\t Multiplication is : %d",e);  // 10
	f = a / b;
	printf("\n\n\t Division is : %d",f); // 2
	g = a % b;
	printf("\n\n\t Modulus is : %d",g); // 1
	getch();
	return 0;
}

3. Logical Operators

Sometimes, we have to check more than one condition at a time then it is operator which is primarily used to check more than two conditions. This operator returns 1 if condition is true otherwise 0.

// Program to demonstrate logical operators.
#include <stdio.h>
#include <conio.h>
int main(void)
{
	int no1=2, no2=5;
	clrscr();
	printf("\n\n %d",(no1 && no2));   // returns 1
	printf("\n\n %d",(no1 || no2));   // returns 1
	getch();
	return 0;
}

4. Relational Operators

It is also used to check conditions. These operators return 1 if condition is true otherwise 0.

// Program to demonstrate Relational operators.
#include <stdio.h>
#include <conio.h>
int main(void)
{
	int a=6, b=2;
	clrscr();
	printf("\n\n A<=B : %d",(a<=b)); // 0 - False
	printf("\n\n A>B : %d",(a>b)); // 1 - True
	printf("\n\n A!=B : %d",(a!=b)); // 1 - True
	getch();
	return 0;
}

5. Shorthand Operators

It is used to perform mathematical operations at which the result or output can affect on operands.

// Program to demonstrate shorthand operators.
#include <stdio.h>
#include <conio.h>
int main(void)
{
	int a,b;
	clrscr();
	a = 18;
	b = 4;
	printf("\n\t Value of A : %d",a);  // 18
	printf("\n\t Using of B : %d",b);  // 4
	b += a ;  // b = b + a
	printf("\n\n\t Using += (i.e b=b+a): %d",b); // 22
	// Change the operator as -=, *=, /=, %=
	getch();
	return 0;
}

6. Unary Operators

It operates on a single operand. Therefore, this operator is called as 'unary operator.' It is used to increase or decrease the value of variable by 1.

// Program to demonstrate Unary / Ternary operators.
#include <stdio.h>
#include <conio.h>
int main(void)
{
	int a=4, b;
	clrscr();
	printf("\n\n Value of A : %d",a); // 4
	a++; // Post
	printf("\n\n Value of A : %d",a); // 5
	++a; // Pre
	printf("\n\n Value of A : %d",a); // 6
	b=--a;
	printf("\n\n Value of A : %d",a); // 5
	printf("\n\n Value of B : %d",b); // 5
	b=a++;
	printf("\n\n Value of A : %d",a); // 6
	printf("\n\n Value of B : %d",b); // 5
	b++;
	printf("\n\n Value of B : %d",b); // 6
	getch();
	return 0;
}

7. Conditional Operator

Conditional operator is also called as 'ternary operator.' It is widely used to execute condition in true part or in false part. It operates on three operands. The logical or relational operator can be used to check conditions.

// Program to demonstrate conditional operator.
#include <stdio.h>
#include <conio.h>
int main(void)
{
	int a, b=3;
	clrscr();
	a = 5;
	printf("\n\n A is less than B ? ");
	(a<b)? printf("Yes"): printf("No"); // No
	getch();
	return 0;
}

8. Bitwise Operators

Bitwise operators are used for manipulation of data at a bit level. They can be directly applied to char, short int and long.

// Program to demonstrate bitwise operators 

------ Not available -------


Download PDF
Published on: 12th Nov 2010, Last Updated on: 18th Dec 2014

Mr. Dinesh Kudache
AUTHOR: Mr. Dinesh Kudache

Mr. Dinesh is the Founder and CEO of technoexam.com. He had 6+ years experience in Application Programming, System Programming, Web Design and Development, Google Expertise. He had written 5000+ various articles on programming. Browse more articles by this author.

Link this post on your Blog/Website :