Functions of operators in C language

Operators in C language are symbols that operates one or more than one value or a variable. For example, +, -, *, / are some of the operators.

If you write 5 + 7 = ?

Then the + operator will perform its function with 5 and 7 then we will get 12 as the result. That is what operators do.

There are several types of operators we use in C programming language. Some of the types of operators are given here.

Types of operators in C programming language:

  • Arithmetic operators
    • Unary operators
    • Binary operators
  • Relational operators
  • Logical operators
  • Conditional or ternary operators
  • Assignment operators
  • Bitwise operators

Using arithmetic operators in C programs

Arithmetic operators in C are used to perform arithmetic operations with two or more then two operands or variables. +, -, *, /, % etc are some of the examples of arithmetic operators.

There are two types of arithmetic operators.

Unary operator : Unary operators can works with a single operands like x++, –a etc. Now see the code bellow about what an unary operator can do.

int main(){

int x = 5;
printf("%d\n", ++x);    // It will print 6
printf("%d\n", x++);    // It will print 6

x = 5;
printf("%d\n", x++);    // It will print 5
printf("%d\n", ++x);    // It will print 7

x = 5;
printf("%d\n", --x);    // It will print 4
printf("%d\n", x--);    // It will print 4

x = 5;
printf("%d\n", x--);    // It will print 5
printf("%d\n", --x);    // It will print 3

return 0;
}

Try to understand above code. ++x will print the value of x after incremented by 1 and x++ will print the current value of x but after printing the value of x will be incremented by 1.

Try to think same for the –x and x– also.

Binary operator : Binary operators need more than one operands or variables to perform mathematical functions. Examples of binary operators are +, -, *, /, %  etc. Now see the code bellow to understand binary operators.

#include <stdio.h>
int main(){

int x = 10;
int y = 5;

int addition = x + y;    // addition of two variables
int subtract = x - y;    // subtraction of two variables
int multiply = x * y;    // multiplication of two variables
int division = x / y;    // division of two variables
int reminder = x % y;    // reminder after division of two variables

printf("%d\n", addition);
printf("%d\n", subtract);
printf("%d\n", multiply);
printf("%d\n", division);
printf("%d\n", reminder);

return 0;
}

Keep in mind that if we get any fraction part after division then we will not get the fraction number on the output. Because, integer variables can’t store float data. For this we will get only the integer part after division. Lets see the code bellow to understand the fact;

#include <stdio.h>
int main(){

int x = 10;
int y = 3;

int division = x / y;    // we will get 3 instead of 3.33333...
int reminder = x % y;    // we will get the reminder 1 here.

printf("%d\n", division);
printf("%d\n", reminder);

return 0;
}

Relational operators and their use in C

Relational operators in C are used to compare the value of two operands or variables. They can check if the value of one variable is greater than another or less than another or they are equal. We use them to take decision that what should we do for what condition. Examples of relational operators are ==, <, <=, >, >= etc.

See the code bellow. We will know more about if else later in our control statement chapter. So, don’t be afraid if you don’t understand the code.

#include <stdio.h>
int main(){

int age;
printf("Enter your age : ");
scanf("%d", & age);    // it will take the input what you give

if(age < 20){
    printf("You should not marry.");
}else{
    printf("You should marry.");
}

return 0;
}

Using logical operators in C in a program

Logical operators in C give us only two results. It may be true or false. They can combine two or more conditions to gives us such Boolean results either true or false.

#include <stdio.h>
int main(){

int x = 10;
int y = 15;
int z = 12;

if (x > y && x > z){
    printf("%d is the biggest number.\n", x);
}else if (y > x && y > z){
    printf("%d is the biggest number.\n", y);
}else{
    printf("%d is the biggest number.\n", z);
}

return 0;
}

Now, just see the code and its result. You will know details about if else condition later in our control statement chapter. So, don’t worry about that now.

Conditional or ternary operators in C

Conditional operators are used to take a decision like if else statement of a program. Conditional operator uses two symbols i.e. ‘ ? ’ and ‘ : ’ to take a decision. They can work on three operands and are also said ternary operators.

Syntax for ternary operator is as follows.

Expression1? expression2: expression3;  

See the code bellow to understand more about ternary operators.

#include <stdio.h>
int main(){
    int age;
    printf("Enter your age here : ");
    scanf("%d", &age);

    (age <= 18)? (printf("Not eligible for voting")) : (printf("Eligible for voting"));

    return 0;
} 

If first expression is true for a ternary operator then the second expression will execute. Otherwise the third expression will execute.

Assignment operators in C and their use

Assignment operators are used to assign any value to a variable. Although most common assignment operator is =, but there are some other assignment operators too. Some common assignment operators are =, +=, -=, /=, %= etc.

#include <stdio.h>
int main(){
    int x = 10;
    int y = 15;

    y += x;    // means that "y = y + x"
    printf("%d\n", y);

    int z = 5;
    z *= x;    // means that "z = z * x"
    printf("%d\n", z);
}

Bitwise operators in C with a program

Bitwise operator works on bit level with a variable. We can works with bit level programming by bitwise operator. It needs separate discussion to understand.

We have discussed more about bitwise operator in the next page. So, just go there by clicking on next bellow.

Previous pageKeywords in C

Next pageBitwise operators in C