Tutorial references

c
c++
c#

Tutorial references

chapters

Operators in C

What is operator?

Operator is symbol which is for mathematical operation and logical manipulation.

Syntax

left-operand operator right-operand

example:

5 + 9

or

a + b

Arithmetic Operator

+, -, *, /, %

+

It is called "plus".

+ is used to add two values(variable).

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 int sum;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n Enter value of b: ");
 scanf("%d",&b);
 sum = a + b;
 printf("\n %d + %d = %d",a,b,sum);
 getch();
}

Run program

printf("%d + %d = %d",a,b,sum); in this statement a,b and sum respectively display on %d places.


-

It is called "minus".

- is used to subtract two values(variable).

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 int sub;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n Enter value of b: ");
 scanf("%d",&b);
 sub = a - b;
 printf("\n %d - %d = %d",a,b,sub);
 getch();
}

Run program

*

It is called "multiply".

* is used to multiply two values(variable).

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 int mul;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n Enter value of b: ");
 scanf("%d",&b);
 mul = a * b;
 printf("\n %d * %d = %d",a,b,mul);
 getch();
}

Run program

/

It is called "divide".

/ is used to divide two values(variable).

Example

#include <stdio.h>
#include <conio.h>
void main(){
 float a;
 float b;
 float div;
 clrscr();
 printf("Enter value of a: ");
 scanf("%f",&a);
 printf("\n Enter value of b: ");
 scanf("%f",&b);
 div = a / b;
 printf("\n %f / %f = %f",a,b,div);
 getch();
}

Run program

%

It is called "modulus".

% is used to get remainder.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 int rem;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n Enter value of b: ");
 scanf("%d",&b);
 rem = a % b;
 printf("\n remainder = %d",rem);
 getch();
}

Run program

Increment Operator

There are two types of Increment operator:

(1) Postfix Increment operator

(2) Prefix Increment operator

Postfix Increment operator(++)

It is use to increment value of variable one by one.

syntax

variableName++

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 b = a++;
 printf("\n value of b = %d",b);
 printf("\n value of a = %d",a);
 getch();
}

Run program

In above program you have read b = a++;.In this statement value of a first assign to b then increment a.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 int c;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n Enter value of b: ");
 scanf("%d",&b);
 c = a++ + b++;
 printf("\n value of a = %d",a);
 printf("\n value of b = %d",b);
 printf("\n value of c = %d",c);
 getch();
}

Run program

In above program you have read c = a++ + b++;.In this statement value of a and value of b first add then result assign to c after assignment value of a and value of b will be increment.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n value of a = %d",a++);
 printf("\n after increment value of a : %d",a);
 getch();
}

Run program

In above program you have read printf("value of a = %d",a++);. In this statement first value of a will be display on output screen then value of a will be increment.


prefix increment operator(++)

It is also used to increment value of variable one by one.

syntax:

++variableName;

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 b = ++a;
 printf("\n value of b = %d",b);
 printf("\n value of a = %d",a);
 getch();
}

Run program

In above program you have read b = ++a;.In this statement value of a first increment then assign to b.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 int c;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n Enter value of b: ");
 scanf("%d",&b);
 c = ++a + ++b;
 printf("\n value of a = %d",a);
 printf("\n value of b = %d",b);
 printf("\n value of c = %d",c);
 getch();
}

Run program

In above program you have read c = ++a + ++b;.In this statement value of a and value of b first increment then add. After addition result will be assign to c.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n value of a = %d",++a);
 printf("\n after increment value of a : %d",a);
 getch();
}

Run program

In above program you have read printf("value of a = %d",++a);. In this statement first value of a will be increment then display on output screen.


Decrement operator

There are two types of Decrement Operator:

(1) Postfix decrement operator

(2) Prefix decrement operator

Postfix Decrement operator(--)

It is use to decrement value of variable one by one.

syntax

variableName--

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 b = a--;
 printf("\n value of b = %d",b);
 printf("\n value of a = %d",a);
 getch();
}

Run program

In above program you have read b = a--;.In this statement value of a first assign to b then decrement a.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 int c;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n Enter value of b: ");
 scanf("%d",&b);
 c = a-- + b--;
 printf("\n value of a = %d",a);
 printf("\n value of b = %d",b);
 printf("\n value of c = %d",c);
 getch();
}

Run program

In above program you have read c = a-- + b--;.In this statement value of a and value of b first add then result assign to c after assignment value of a and value of b will be decrement.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n value of a = %d",a--);
 printf("\n after decrement value of a : %d",a);
 getch();
}

Run program

In above program you have read printf("value of a = %d",a--);. In this statement first value of a will be display on output screen then value of a will be decrement.


prefix decrement operator(--)

It is also used to decrement value of variable one by one.

syntax:

--variableName;

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 b = --a;
 printf("\n value of b = %d",b);
 printf("\n value of a = %d",a);
 getch();
}

Run program

In above program you have read b = --a;. In this statement value of a first decrement then assign to b.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 int b;
 int c;  clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n Enter value of b: ");
 scanf("%d",&b);
 c = --a + --b;
 printf("\n value of a = %d",a);
 printf("\n value of b = %d",b);
 printf("\n value of c = %d",c);
 getch();
}

Run program

In above program you have read c = --a + --b;.In this statement value of a and value of b first decrement then add. After addition result will be assign to c.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int a;
 clrscr();
 printf("Enter value of a: ");
 scanf("%d",&a);
 printf("\n value of a = %d",--a);
 printf("\n after decrement value of a : %d",a);
 getch();
}

Run program

In above program you have read printf("value of a = %d",--a);. In this statement first value of a will be decrement then display on output screen.


Assignment Operator

= is used to assign a value into a variable.Example int m = 100;

+= is used to add two value and assign into variable. Example a += 100; It means a = a + 100;

-= is used to subtract two value and assign into variable. Example a -= 100; It means a = a - 100;

*= is used to multiply two value and assign into variable. Example a *= 100; It means a = a * 100;

/= is used to divide two value and assign into variable. Example a /= 100;. It means a = a / 100;

%= is used to get remainder and assign into variable. Example a %= 100;. It means a = a % 100;


Relational operator

<, >, <=, >=, ==, !=

It is also called comparison operator

It is used to compare two variable or values.

It is used to make conditions.

Relational operators always return true or false.

In output screen you can see 1 for true and 0 for false.

NOTE: turbo c++ does not support bool data type therefore you can use int data type to hold 0 or 1.

syntax:

left-operan operator right-operand

<

It is called less than operator.

It compare two variable or values.

Example:

p = m < n;

In this example, If m is less than n then return true and assign to p otherwise it return false and assign to p.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 p = m < n; /* in program output we can see 1 for true and 0 for false */
 printf("\n value of m = %d",m);
 printf("\n value of n = %d",n);
 printf("\n value of p = %d",p);
 getch();
}

Run program

>

It is called greater than operator.

It compare two variable or values.

Example:

p = m > n;

In this example, If m is greater than n then return true and assign to p otherwise it return false and assign to p.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 p = m > n;
 printf("\n value of m = %d",m);
 printf("\n value of n = %d",n);
 printf("\n value of p = %d",p);
 getch();
}

Run program

<=

It is called less than or equal operator.

It compare two variable or values.

Example:

p = m <= n;

In this example, If m is less than or equal to n then return true and assign to p otherwise it return false and assign to p.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 p = m <= n;
 printf("\n value of m = %d",m);
 printf("\n value of n = %d",n);
 printf("\n value of p = %d",p);
 getch();
}

Run program

>=

It is called greater than or equal operator.

It compare two variable or values.

Example:

p = m >= n;

In this example, If m is greater than or equal to n then return true and assign to p otherwise it return false and assign to p.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 p = m >= n;
 printf("\n value of m = %d",m);
 printf("\n value of n = %d",n);
 printf("\n value of p = %d",p);
 getch();
}

Run program

==

It is called equal to operator.

It compare two variable or values.

Example:

p = m == n;

In this example, If m is equal to n then return true and assign to p otherwise it return false and assign to p.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 p = m == n;
 printf("\n value of m = %d",m);
 printf("\n value of n = %d",n);
 printf("\n value of p = %d",p);
 getch();
}

Run program

!=

It is called not equal to operator.

It compare two variable or values.

Example:

p = m != n;

In this example, If m is not equal to n then return true and assign to p otherwise it return false and assign to p.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 p = m != n;
 printf("\n value of m = %d",m);
 printf("\n value of n = %d",n);
 printf("\n value of p = %d",p);
 getch();
}

Run program

Logical Operators

&&, ||, !

Logical operators are used to perform logical operations.

It always return true or false.

&&

It is called logical AND.

See bellow truth table.

left-oprand operator right-operand output
true && true true
true && false false
false && true false
false && false false

You can understand from bellow example:

m = 10;
n = 20;
p = 30;
q = 40;
res = (a < b) && (p > q);
           true           false
                  false

It means above example output is false

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 int q;
 int res;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 printf("Enter value of p: ");
 scanf("%d",&p);
 printf("\n Enter value of q: ");
 scanf("%d",&q);
 res = (m < n) && (p > q);
 printf("\n value of res = %d",res);
 getch();
}

Run program

||

It is called logical OR.

See bellow truth table.

left-oprand operator right-operand output
true || true true
true || false true
false || true true
false || false false

You can understand from bellow example:

m = 10;
n = 20;
p = 30;
q = 40;
res = (a < b) || (p > q);
           true           false
                  true

It means above example output is true

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 int q;
 int res;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 printf("Enter value of p: ");
 scanf("%d",&p);
 printf("\n Enter value of q: ");
 scanf("%d",&q);
 res = (m < n) || (p > q);
 printf("\n value of res = %d",res);
 getch();
}

Run program

!

It is called logical NOT

See bellow truth table

operator operand output
! true false
! false true

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int res;
 clrscr();
 printf("Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 res = !(m < n);
 printf("\n value of res = %d",res);
 getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018