Posted by Unknown at 3:58 AM
What is operators?
An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables.If you want to be a C programmer your conception on Operators should be must.C has a rich set of operators which can be classified as -
1 Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
What is arithmetic operators?
C provides all the basic arithmetic Operators. This operators + , - , * , % and / .+ For performing Addition
- For performing Subtraction
/ For performing Division
* For performing Multiplication
% Modulo for finding remainder in division operation
#include <stdio.h>
main()
{
int a;
int b;
int sum;
a = 50;
b = 60;
sum = a + b;
printf ("sum is : %d",sum) ;
}
It can be written int a,b,sum; instead of
#include <stdio.h>
main()
{
int a;
int b;
int sum;
This print statement - printf ("sum is : %d",sum) ; contains two argument.This first argument "%d" tells the compiler that the value of the second argument sum should be printed as a decimal integer.
Note : If you write this printf ("sum is : %d\n",sum) ; actually \n its mean newline character.
Look this program output :
Output |
a -b a + b
a * b a / b
a % b -a * b
a + b = 110
a – b = -10
a * b =3000
a / b = 0
if you write float a, b , sum ;then answer is - a + b =100.0
a - b = -10.0
a * b =3000.0
Note : when we like to use float then just need to change %d and have to write printf ("sum is : %f",sum);
What is relational operators?
We often compare two quantities and depending on their relation ,take certain decisions.For example , we may compare the age two persons.This comparisons can be done with the help of relational operators. C supports the following relational operators -
Operator
|
Meaning
|
<
|
is less than
|
<=
|
is less than or equal to
|
>
|
is greater than
|
>=
|
is greater than or equal to
|
==
|
is equal to
|
Here there are some example :
(7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
Suppose that a=2, b=3 and c=6
(a == 5) // evaluates to false since a is not equal to 5.
(a*b >= c) // evaluates to true since
(2*3 >= 6) is true. (b+4 > a*c) // evaluates to false since
(3+4 > 2*6) is false.
((b=2) == a) // evaluates to true.
What is logical operators?
In addition to the relational operators,C has the following three logical operators -
&&
|
Logical AND
|
||
|
Logical OR
|
!
|
Logical NOT
|
The logical operators && and || and used when we want to test more than one condition and make decision.An example is :
a > b && x==10
For example :
#include <stdio.h>
void main()
{
int n;
scanf("%d",&n);
if ((n%2)==0)
{
printf("Even");
}
else
{
printf("Odd");
}
}
Note : here if else is if else statement.next time discuss this if else.stay with us..........>>>>>>>>>>
The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.
An example is :
a < m || a < n
The
logical not operator takes single expression and evaluates to true if
the expression is false and evaluates to false if the expression is
true. In other words it just reverses the value of the expression.
What is assignment operators?
Assignment Operators are used to assign the result of an expression ta a variable.
example
x = a + b
Here the value of a + b is evaluated and substituted to the variable x.
In addition, C has a set of shorthand assignment operators of the form.
var oper = exp; In addition, C has a set of shorthand assignment operators of the form.
Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator.
x + = 1 is same as x = x + 1
The commonly used shorthand assignment operators are as follows
Shorthand assignment operators
Shorthand assignment operators
Statement with simple
assignment operator |
Statement with
shorthand operator |
a = a + 1
|
a += 1
|
a = a – 1
|
a -= 1
|
a = a * (n+1)
|
a *= (n+1)
|
a = a / (n+1)
|
a /= (n+1)
|
a = a % b
|
a %= b
|
What are increment and decrement Operators?
The
increment and decrement operators are one of the unary operators which
are very useful in C language. They are extensively used in for and
while loops. The syntax of the operators is given below
1. ++ variable name (prefix)Here ++ is used before variable name means 1 add before
2. variable name++ (post fix)
after ++ mean 1 add after run program
3. – –variable name(prefix)
4. variable name– –(post fix)
Example 1 | Example 2 |
---|---|
B=3; A=++B; // A contains 4, B contains 4 |
B=3; A=B++; // A contains 3, B contains 4 |
What is Conditional operators?
The conditional operator consists of 2 symbols the question mark (?) and the colon (:)The syntax for a ternary operator is as follows
exp1 ? exp2 : exp3
The ternary operator works as follows
exp1 is evaluated first. If the expression is true then exp2 is evaluated & its value becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note that only one of the expression is evaluated.
For example
a = 10;
b = 15;
x = (a > b) ? a : b
Here x will be assigned to the value of b. The condition follows that the expression is false therefore b is assigned to x.
#include<stdio.h>
main( )
{
int a , b , larger;
printf ("Input 2 integers : ");
scanf("%d %d",&a , &b);
larger = a > b ? a : b;
printf("The largest of two numbers is %d \n", larger);
}
if you type 2 integers 10 15 then output -
Bitwise Operators :
C has a distinction of supporting special operators known as bitwise operators for manipulation data at bit level.
C has a distinction of supporting special operators known as bitwise operators for manipulation data at bit level.
Operator
|
Meaning
|
&
|
Bitwise AND
|
|
|
Bitwise OR
|
^
|
Bitwise Exclusive
|
<<
|
Shift left
|
>>
|
Shift right
|
Feel free comment me in the comment box.