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:
- 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
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
#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
#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
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
#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
C other Operators
Operator 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
Suggested for you