Operators

Operator in C programming language with example

Operator in C programming language with example

In this tutorial, we discuss Operator in C programming language with the example.

C programming operators

C language provides a rich set of operators to manipulate variables. We can divide all the C programming operators into the following groups:

Operator in C programming
  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Unary Operators
  • Logical Operator
  • Bitwise operator

 

C Arithmetic Operators

Arithmetic operators are used to calculating mathematical expressions in the same way that they are used in algebra. The following table list the arithmetic operators

[wp_table id=6914/]

How  works arithmetic  in C language

Example 1

#include <stdio.h>
#include <stdlib.h>

int main()
{ //+,-*,./%
    //printf("Hello world!\n");
    int a,b;
    int c,d,e,f,g;
    printf("Enter a valua to a\n");
    scanf("%d",&a);
    printf("Enter a valua to b\n");
    scanf("%d",&b);
    c=a+b,
    d=b-a;
    e=a*b;
    f=b/a;
    g=b%a;
    printf("a+b value is :%d \n",c);
    printf("a-b value is :%d \n",d);
    printf("a*b value is :%d \n",e);
    printf("a/b value is :%d \n",f);
    printf("a%b value is :%d \n",g);
    getch();
    return 0;
}

 

When the above code compiled and executed, it produces the following result

Enter a value to a

24
Enter a value to b
12
a+b value is  :36
a-b value is: 12
a*b value is: 288
a/b value is  :2
a%b value is  :0

C Assignment Operator

In C Programming, the assignment operator is used for assigning a significant value to a variable. Equal to(=) is a mostly known assignment ope, in algebra.

The following table list the assignment operators in C language

[wp_table id=6919/]
How  works assignment operator in C language
Example
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,x,y,z,m,n,p,q;
    printf("Enter a valua to a\n");
    scanf("%d",&a);
    printf("Enter a valua to b\n");
    scanf("%d",&b);
    x=a+=b;
    printf("a+=b value is :%d \n",x);
    y=a-=b;
    printf("a-=b value is :%d \n",y);
    z=a*=b;
    printf("a*=b value is :%d \n",z);
    m=a/=b;
    printf("a/=b value is :%d \n",m);
    n=a%=b;
    printf("a%=b value is :%d \n",n);
    p=a&=b;
    printf("a&=b value is :%d \n",p);
     q=a^=b;
    printf("a^=b value is :%d \n",q);

    return 0;
}

 

When the above code compiled and executed, it produced the following result

Enter a value to a
Enter a value to b
36
Enter a value to b
12
a+=b value is : 48
a-=b value is :24
a*=b value is :288
a/=b value is :24
a&=b value is :0
a^=b value is :12

 

C Relational Operators

A relational operator compares the relationship between two variables(operands). relational operators perform based on boolean expression and return either true(1) or false(0).

The following table list the relational operators in C language

[wp_table id=6923/]
How  works the relational operator in C language
Program 1
#include <stdlib.h>
int main()
{
    int x=10,y=10,z=20;
    printf("x is equal to y :%d\n",x==y); //true
    printf("x is equal to z :%d\n",x==z); //false
    printf("y is equal to z :%d\n\n",y==z); //false

    printf("x is greater than to y :%d\n",x>y); //false
    printf("x is greater than to z :%d\n",x>z); //false
    printf("y is greater than to z :%d\n\n",y>z); //false

    printf("x is less than to y :%d\n",x<y); //false
    printf("x is less than to z :%d\n",x<z); //true
    printf("y is less than to z :%d\n\n",y<z); //true

    printf("x is less than or equal to y :%d\n",x<=y); //true
    printf("x is less than or equal to z :%d\n",x<=z); //true
    printf("y is less than or equal to z :%d\n\n",y<=z); //true

    printf("x is greater than or equal to y :%d\n",x>=y); //true
    printf("x is greater than or equal to z :%d\n",x>=z); //false
    printf("y is greater than or equal to z :%d\n\n",y>=z); //false

    printf("x is not equal to y :%d\n",x!=y); //false
    printf("x is not equal than to z :%d\n",x!=z); //true
    printf("y is not equal to z :%d\n\n",y!=z); //true

    printf("End the program");
    getch();
    return 0;
}

 

When the above code compiled and executed, it produces the following result

x is equal to y: 1
x is equal to z :0
y is equal to z :0

x is greater than to y :0
x is greater than to z :0
y is greater than to z :0

x is less than to y :0
x is less than to z: 1
y is less than to z: 1

x is less than or equal to y: 1
x is less than or equal to z: 1
y is less than or equal to z: 1

x is greater than or equal to y: 1
x is greater than or equal to z :0
y is greater than or equal to z :0

x is not equal to y :0
x is not equal than to z: 1
y is not equal to z: 1

End the program

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{//== != < > <= >=
    int a,b;
    a=20,b=15;
    if(a<b){
            printf("a less than b\n");
    }else{
    printf("a greater than or equal to b\n");
    }
    if(a>b){
            printf("a greater than b\n");
    }else{
    printf("a less than or equal to b\n");
    }
    if(a<=b){
            printf("a less than or equal to b\n");
    }else{
    printf("a greater than b\n");
    }
    if(a>=b){
            printf("a greater than or equal to b\n");
    }else{
    printf("a less than b");
    }
    if(a==b){
            printf("a equal to b\n");
    }else{
    printf("a not equal to b\n");
    }
    if(a!=b){
            printf("a not equal to b\n");
    }else{
    printf("a equal to b\n");
    }
    //printf("Hello world!\n");
    return 0;
}

When the above code compiled and executed, it produces the following result

a less than or equal to b
a greater than b
a greater than b
a greater than or equal to b
a not equal to b
a not equal to b

 

C unary Operator

The following table list the unary operators in C language

[wp_table id=6924/]
[wp_table id=6926/]

 

How  works unary operator in C language

Program

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x=10,y=5,z;
    z=+x;
    printf("+x value is :%d\n",z);//Unary plus

    z=-y;
     printf("-y value is :%d\n",z);//Unary minus

     z=++x;
     printf("++x value is :%d\n",z);//prefix increment

     z=y++;
     printf("y++ value is :%d\n",z);//postfix increment

     z=--x;
     printf("--x value is :%d\n",z);//prefix decrement

     z=y--;
     printf("y-- value is :%d\n",z);//postfix decrement

    printf("End the program\n");
    getch();
    return 0;
}

 

When the above code compiled and executed, it produces the following result

+x value is :10
-y value is :5
++x value is :11
y++ value is :6
--x value is 10
y-- value is :6
End the program

 

C Logical Operators

The logical operator performs based on boolean expression, return either true or false.

The following table list the logical operators in C language

[wp_table id=6943/]
How  works logical operator in C language
Example
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age=20;
    if(age<=18 && age >=13){
    printf("you are a teenager\n");
    }else{
    printf("you are old or child\n");
    }
    if(age>=18 || age <=13){
    printf("you are not a teenager\n");
    }else{
    printf("you are a yanger\n");
    }
    return 0;
}

When the above code compiled and executed, it produces the following result

you are a teen ager
you are not teenager

 

C bitwise Operator

In C language.the bitwise operator is an operator used to performs bit or binary pattern operation(bitwise operation).

The following table list the bitwise operators in C language

[wp_table id=6945/]
[wp_table id=6948/]
Learn more bitwise operator in C

C other Operators

[wp_table id=6951/]

Operator precedence

The following table list out the precedence of C operators. The operators are listed top to bottom, in descending order precedence.
Operators are in the top of the list, that gets higher precedence. Operators are in the bottom of the list, that gets lower precedence

The following table list the operator precedence

[wp_table id=6956/]

 

 

Suggested for you

Math function in C

Operator in Java language

Operator in C++ language

Operator in Python language

Data type and variable in Java

Data type and variable in C

Data type and variable in C++

Data type and variable in Python

Operator in Python
Operator in Cpp programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago