« Previous                                                                  Next Chapter »

Operators

An operator is a symbolic constant which won’t change during the execution of the program which affects the variables which are placed at the side of it.

We have two different types of operators in C language.  They are

1. Unary operator
2. Binary operator

1. Unary operator

If any operator is able to perform some operation on only one variable by residing either of the side such an operator can be called as unary operator.

C provides the fallowing unary operators

1. * indirection operator
3. ++ increment operator
4. – decrement operator

2. Binary operator

If any operator is able to perform some operations on more than one variable by residing between them such an operator can be called as binary operator. May be unary or binary we have 10 operators in C language. They are

1. Arithmetic operator

If any operator is useful to perform some operations on numeric type of data such an operator can be called as arithmetic operator.

They are + - * / %

2. Relational operator

If any operator is useful to find the relation between two variables such an operator can be called as relational operator.

They are   >  <  >=   <= = =

3. Logical operator

If any operator is useful to check more than one condition logically such an operator can be called as logical operator. We have the fallowing logical operators.

1. logical and ( && )

If we use logical and  between more than one condition we can perform some action if and only if all the conditions results true.

2. logical or  ( || )

If we use logical or between  more than one condition we can perform some action if any of the conditions results true.

3. logical not ( ! )

When ever user want to check the condition in negative mode logical not can be used.

4. Assignment operator

This operator is useful to assign some values to the variables. If we use this operator the right side value will be forcefully stored in to the left side variable.

The operator is =

Ex: int x=10;  After this statement the value of x will be 10.

5. Sizeof operator

This operator is useful to find out how memory was allotted for a variable or how much memory will be allotted for a particular data type.

The operator is sizeof

Ex: int x; char y; float z;

sizeof(x) results 2 bytes

sizeof(y) results 1 byte

sizeof(z) results 4 bytes.

Similarly sizeof(int) gives 2 bytes, sizeof(char) gives 1 byte etc...

6. Increment and decrement operator (++,--)

If any operator is useful to increase the already existed value in a variable by 1 such an operator can be called as incrementation operator. The operator is ++

Ex: int a=10;  a++ gives the a value as 11

If any operator is useful to decrease the already existed value in a variable by 1 such an operator can be called as decrementation operator. The operator is --

Ex: int a=10; a -- gives the a value as 9

7. Comma operator

This operator is useful to separate the variables of same data type. Like we can separate the expressions etc.

Ex: int a,b,c;

8. Ternary or conditional operator

The combination of ? and : can be called as ternary or conditional operators. Here ? stands for if and the : stands for else.

Syntax:  (exp1)?(exp2):(exp3);

If exp1 results true exp2 will be executed and the exp3 will be skipped from the execution.

If exp1 is false exp2 will be skipped from the execution and exp3 will be executed.

9. Bit wise operator

If any operator is useful to perform bit level manipulations such an operator can be called as bit wise operator. They are

1. Bit wise &

2. Bit wise or  |

3. Shift left <<

4. Shift right >>

5. Bit wise once complement ~

10. Special operator

If any operator is able to perform some operations differently based on the situation where it used such an operator can be called as special operator.

They are & * . (Dot) -> (Array)

More Examples

/* program to find sum */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

printf(" enter the values ");

scanf(" %d %d",&a,&b);

c=a+b;

printf(" the sum is %d",c);

/* program to find difference */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

printf(" enter the values ");

scanf(" %d %d",&a,&b);

c=a-b;

printf(" the difference is %d",c);

/* program to find product */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

printf(" enter the values ");

scanf(" %d %d",&a,&b);

c=a*b;

printf(" the product is %d",c);

/* program to find reminder */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

printf(" enter the values ");

scanf(" %d %d",&a,&b);

c=a%b;

printf(" the remainder is %d",c);

/* program to find quotient */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b;

float c;

clrscr();

printf(" enter the values ");

scanf(" %d %d",&a,&b);

c=(float)a/b;

printf(" the quotient is %f",c);

/* program to find arithmetic operations */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

printf(" enter the values ");

scanf(" %d %d",&a,&b);

c=a+b;

printf(" the sum is %d\n",c);

c=a-b;

printf(" the difference is %d\n",c);

c=a*b;

printf(" the product is %d\n",c);

c=a%b;

printf(" the remainder is %d\n",c);

c=a/b;

printf(" the quotient is %d\n",c);

/* program to find arithmetic operations(second method) */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b;

clrscr();

printf(" enter the values ");

scanf(" %d %d",&a,&b);

printf(" the sum is %d\n",a+b);

printf(" the difference is %d\n",a-b);

printf(" the product is %d\n",a*b);

printf(" the remainder is %d\n",a%b);

printf(" the quotient is %f\n",(float)a/b);

}

/* program to find area of the square */

#include<stdio.h>

#include<conio.h>

main()

{

int s,area;

clrscr();

printf(" enter the side of the square ");

scanf(" %d",&s);

area=s*s;

printf(" the are of the square is %d\n",area);

/* program to find perimeter of the square */

#include<stdio.h>

#include<conio.h>

main()

{

int s,perimeter;

clrscr();

printf(" enter the side of the square ");

scanf(" %d",&s);

perimeter=4*s;

printf(" the perimeter of the square is %d\n",perimeter);

/* program to find area of the rectangle */

#include<stdio.h>

#include<conio.h>

main()

{

int l,b,area;

clrscr();

printf(" enter the l,b of the rectangle ");

scanf(" %d %d",&l,&b);

area=l*b;

printf(" the area of the rectangle is %d\n",area);

}

/* program to find perimeter of the rectangle */

#include<stdio.h>

#include<conio.h>

main()

{

int l,b,perimeter;

clrscr();

printf(" enter the l,b of the rectangle ");

scanf(" %d %d",&l,&b);

perimeter=2*(l+b);

printf(" the perimeter of the rectangle is %d\n",perimeter);

/* program to find area of the circle */

#include<stdio.h>

#include<conio.h>

#define p 3.14

main()

{

int r;

float area;

clrscr();

printf(" enter the value of r");

scanf("%d",&r);

area=p*r*r;

printf("the area of the circle is %f",area);

/* program to find circumference of the circle */

#include<stdio.h>

#include<conio.h>

#define p 3.14

main()

{

int r;

float c;

clrscr();

printf(" enter the value of r");

scanf("%d",&r);

c=2*p*r;

printf("the circumference of the circle is %f",c);

/* program to find simple interest */

#include<stdio.h>

#include<conio.h>

main()

{

int p,n,r;

float si;

clrscr();

printf(" enter the value of p,n,r");

scanf("%d %d %d",&p,&n,&r);

si=(p*n*r)/100;

printf("the simple interest is %f",si);

/* program to find e=m*c*c */

#include<stdio.h>

#include<conio.h>

main()

{

int m,c;

float e;

clrscr();

printf(" enter the value of m,c");

scanf("%d %d",&m,&c);

e=m*c*c;

printf("the result is %f",e);

}

/* program to find a*a+b*b+2*a*b */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

printf(" enter the value of a,b");

scanf("%d %d",&a,&b);

c=(a*a)+(b*b)+(2*a*b);

printf("the result is %d",c);

/* program on assignment operators */

#include<stdio.h>

#include<conio.h>

main()

{

int a=10;

clrscr();

printf(" the value of a is %d\n",a);

//normal assignment

a=20;

printf(" the value of a is %d\n",a);

//arithmetic assignment

a+=2;

printf(" the value of a is %d\n",a);

/* program to print size of data types */

#include<stdio.h>

#include<conio.h>

main()

{

int x;

char y;

float z;

clrscr();

printf("the memory for short int is %d\n",sizeof(short int));

printf("the memory for int is %d\n",sizeof(int));

printf("the memory for long int is %d\n",sizeof(long int));

printf("the memory for float is %d\n",sizeof(float));

printf("the memory for double is %d\n",sizeof(double));

printf("the memory for long double is %d\n",sizeof(long double));

printf("the memory for char is %d\n",sizeof(char));

printf("the memory for x is %d\n",sizeof(x));

printf("the memory for y is %d\n",sizeof(y));

printf("the memory for z is %d\n",sizeof(z));

/* program on incrementation and decrementation */

#include<stdio.h>

#include<conio.h>

main()

{

int a;

clrscr();

printf("enter the value of a");

scanf("%d",&a);

printf("enter the value of a is %d\n",a);

printf("enter the value of a after preincrementation %d\n",++a);

printf("enter the value of a after postincrementation %d\n",a++);

printf("enter the value of a after predecrementation %d\n",--a);

printf("enter the value of a after postdecrementation %d\n",a--);

printf("enter the value of a is %d\n",a);

/* program on and,or operations */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

printf(" the values of a,b");

scanf("%d %d",&a,&b);

c=a&b;

printf(" the result is %d\n",c);

c=a|b;

printf(" the result is %d\n",c);

/* program on shift left and shift right operations */

#include<stdio.h>

#include<conio.h>

main()

{

int n;

clrscr();

printf(" enter the value of n ");

scanf("%d",&n);

printf(" after shift left is %d\n",n<<1);

printf(" after shift right is %d\n",n>>1);

/* program to find whether it is +ve or not */

#include<stdio.h>

#include<conio.h>

main()

{

int n;

clrscr();

printf(" enter the number ");

scanf("%d",&n);

(n>=0)?printf(" +ve value "):printf(" -ve value");

/* program to find whether it is even or odd */

#include<stdio.h>

#include<conio.h>

main()

{

int n;

clrscr();

printf(" enter the number ");

scanf("%d",&n);

(n%2==0)?printf(" even number "):printf(" odd number");

/* program to find whether student passed or fail */

#include<stdio.h>

#include<conio.h>

main()

{

int n;

clrscr();

printf(" enter the marks ");

scanf("%d",&n);

(n>=35)?printf(" passed "):printf(" fail ");

/* program to find whether year is leap year or not */

#include<stdio.h>

#include<conio.h>

main()

{

int n;

clrscr();

printf(" enter the year ");

scanf("%d",&n);

(n%4==0)?printf(" leap year "):printf(" not leap year ");

}

/* program to find whether number is digit or not */

#include<stdio.h>

#include<conio.h>

main()

{

int n;

clrscr();

printf(" enter the value ");

scanf("%d",&n);

((n>=0)&&(n<=9))?printf(" digit "):printf(" not digit ");

/* second method */

#include<stdio.h>

#include<conio.h>

main()

{

char x;

clrscr();

printf(" enter the character ");

scanf("%s",&x);

((x>='0')&&(x<='9'))?printf("digit"):printf("not a digit");

/* third method */

#include<stdio.h>

#include<conio.h>

main()

{

int x;

clrscr();

printf(" enter the character ");

scanf("%d",&x);

((x>=48)&&(x<=57))?printf("digit"):printf("not a digit");

/* fourth method */

#include<stdio.h>

#include<conio.h>

main()

{

char x;

clrscr();

printf("enter the character");

x=getchar();

((x>=48)&&(x<=57))?printf("digit "):printf("not a digit ");

putchar(x);

/* program to find whether it is capital letter or not */

#include<stdio.h>

#include<conio.h>

main()

{

char x;

clrscr();

printf("enter the character");

x=getchar();

((x>='A')&&(x<='Z'))?printf(" capital letter "):printf(" not a capital letter ");

putchar(x);

/* second method by using ASCII value */

#include<stdio.h>

#include<conio.h>

main()

{

char x;

clrscr();

printf(" enter the character ");

x=getchar();

((x>=65)&&(x<=90))?printf(" capital letter "):printf(" not a capital letter ");

putchar(x);

/* program to find whether it is space or not */

#include<stdio.h>

#include<conio.h>

main()

{

char x;

clrscr();

printf(" enter the character ");

x=getchar();

(x==' ')?printf(" space "):printf(" not a space ");

putchar(x);

/* second method using ASCII value */

#include<stdio.h>

#include<conio.h>

main()

{

char x;

clrscr();

printf(" enter the character ");

x=getchar();

(x==32)?printf(" space "):printf(" not a space ");

putchar(x);

/* program to find whether first value is big or not */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b;

clrscr();

printf(" enter the values of a,b ");

scanf("%d %d",&a,&b);

(a>b)?printf(" first value is big "):printf(" first value is not big ");

/* program to find relation between two values */

#include<stdio.h>

#include<conio.h>

main()

{

int a,b;

clrscr();

printf(" enter the values of a,b ");

scanf("%d %d",&a,&b);

(a>b)?printf(" a is big "):(b>a)?printf(" b is big "):printf(" both are equal");

}

« Previous                                                                  Next Chapter »