Operators in C Programming
C Programming

Operators in C Programming

Operators in C Programming

In this tutorial we will learn about Operators in C Programming. Various types of operators are explained with example.

This tutorial will be useful for the students learning C Programming.

Let’s start with introduction of Operators in C.

Frequently Asked Questions

Some frequently asked question from operators in C tutorial are given below. After reading this  C Tutorial students can answer all these questions correctly.

  • What is Operator in C ?
  • What is Conditional Operator in C ?
  • Discuss Bitwise Operators with example.
  • Explain Logical operators in C.
  • What are different Relational Operators in C ?
  • Discuss Unary Operator in C Programming.
  • What is Operator Precedence ?
  • What is Operator associativity ?
  • What is sizeof operator in C ?

What is Operator in C ? 

  • An operator is a symbol that tells the compiler to perform specific mathematical, relational, conditional , bitwise or logical functions .
  • The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.
  • Usually, operators take part in a program for manipulating data and variables and form a part of the mathematical, conditional, or logical expressions.
  • C has many operators that almost perform all types of operations. These operators are really useful and can be used to perform every operation.

Types of Operator in C Programming

Various types of operators in C Programming are discussed and explained in this section. There are following types of operators in C Programming

1. Arithmetic Operators

  • Arithmetic operators in C Programming are used to perform mathematical operations such as addition, subtraction, multiplication and division on numerical values (variables and constants).
  • Various arithmetic operators are +, -,*,/ and %

2.  Increment and Decrement Operators

  • C Programming has two very useful operators increment (++) and decrement ( –).
  • Increment operators are used to increase the value of variable by one and decrement operators are used to decrease the value of the variable by one in C Programs.
  • These operators are also known as unary operators.
  • Increment and decrement operators are used in two ways post increment and pre increment. In same way post increment and pre decrement.

Meaning of each is given in following table.

unary operator

Program for increment and decrement Operator

#include <stdio.h>
int main()
{
int a = 10, b = 100; float c = 10.5, d = 100.5;
printf(“++a = %d \n”, ++a);
printf(“–b = %d \n”, –b);
printf(“++c = %f \n”, ++c);
printf(“–d = %f \n”, –d);
return 0;
}
Output 

++a = 11

–b = 99

++c = 11.500000

–d = 99.500000

3. Assignment Operator

  • An assignment operator is used for assigning a value to a variable.
  • The most common assignment operator is =
  • Assignment operator is also used with other operator such as arithmetic and relational operators.
  • Various use of assignment operator is shown in following table.

assignment operator

4. Relational Operators

  • 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 == ,<,>,<=,>= and !=

Example of relational operator is given below

#include <stdio.h>

void main() {

int a = 5, c;

c = a;

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

c += a;

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

c *= a;

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

}

Output :

C=5

C=10

C=25

5. Logical Operators

  • Logical operators are commonly used in decision making.
  • An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.
  • Logical Operators are used when we have to take decision on the basis of combination of more than one condition.
  • && is logical AND and it return true when all the conditions are satisfied.
  • || is logical OR operator and it return true when any one of the condition is true.
  • ! is logical not and it invert the true to false and false to true.

6. Conditional Operator in C

The conditional operator in c is similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.

Conditional operator is represented as  (condition) ? Exepression1 : Expression2

if the condition is true then expression 1 will execute otherwise expression 2 will execute.

Program for conditional operator in C

#include <stdio.h>

int main()
{
int m = 5, n = 4;

(m > n) ? printf(“m is greater than n that is %d > %d“, m, n ) : printf(“n is greater than m that is %d > %d“ ,n, m);

return 0;
}

Output

m is greater than n that is 5 > 4

7. Bitwise Operators

  • Bitwise operators are used in C programming to perform bit-level operations
  • During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.
  • Various bitwise operators are as shown in following table.

bitwise operator

8. Sizeof Operator

  • The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).

Program for sizeof operator is given below

#include <stdio.h>
int main()
{
int a;
char d;
printf(“Size of int=%lu bytes\n”,sizeof(a));
printf(“Size of char=%lu byte\n”,sizeof(d));
return 0 ;
}

Output:

Size of int = 2

Size of char = 1

Here when we passed variables a  and d to sizeof( ) operator then it displaying the size of variable a and d according to their data type.

Conclusion and Summary

In this Operators in C Programming tutorial we have explained different types of operators in c including bitwise operators , logical operators , relational operators and conditional operator in c with example.

I hope this tutorial will be beneficial for students.

Leave a Reply

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