Wednesday, June 6, 2012

Operators in c



C Language have rich set of Operators.
Operator: it is a special symbol used to perform a task on the operand.
Operand: it is a variable or data item
Expression: it is a combination of Operands and operators.
Ex: c= a+b
where c,a,b are operands
+,= are operators.

Arithmetic Operators

: It is used to perform a general mathematical operations.
OperatorUsage
+ Addition
- Subtraction
* Mutliplication
/ Division
% Remainder

Assignment Operators

It is used to assign the values from right hand side to the left hand side. Right hand side may be a variable / Expression / Value constant, But Left hand side must be a variable. To assign the values we use "=" symbol.

tr> tr> tr> tr>
-=
OperatorUsage
= Assign the value from R.H.S to L.H.S
Eg: a=b means b variable value is assigned to a variable
Eg: a=5 means 5 integer constant is stored in a variable
Eg: c= a+b means a and b variable values are added and it is strored in c variable
+= eg: A +=5
means A= A+5

5 value constant is added to A variable the sum value is stored in A again
-= eg: A -=5
means A= A-5

5 value constant is subtracted from A variable the difference value is stored in A again
*= eg: A *=5
means A= A*5

5 value constant is multiply to A variable the product is stored in A again
*= eg: A /=5
means A= A/5

5 value constant is division with A variable and the quotient value is stored in A
/= eg: A /=5
means A= A/5
5 value constant is division with A variable and the quiotent value is stored in A
%=eg: A %=5
means A=A%5
5 value constant is division with A variable and the remainder value is stored in A

+=,-=,*=,/=,%= are called as shorthand notations.

Relational Operators

It is used to check the simple conditions in the clanguage

Operator

Usage

<

greater than

<=

greater than or equal to

>

less than 

>=

less than or equal to

= =

comparision (equal to)

!=

Not Equal to

Logical Operators

It is used to combine more than one conditions into a single conditions and all the conditions are treat as single condition.
We have two operators

Operator

Usage

&& (and)

it is used to combine more than one condition as a single condition. if all the conditions are true than total condition is true other wise if one condition is false than total condition is false.

|| (or) 

it is used to combine more than one condition as a single condition. if any one  condition is  true than total condition is true 

Terinary Operators

it is used to check the condition if the condition is true it executes true set of statements other wise it executes false set of statements.
Syntax: (cond)? true_statement:false_statement;
Example: Write a C Program accept a number and check whether it is even or odd
#include<stdio.h>
main()
{
 int a;
printf("\n Enter a Number: ");
scanf("%d",&a); /* accepting a number */
(a%2= =0)? /*  checking the condition */
    printf("\n %d is an even no",a): printf("\n %d is an odd no",a);
}
Explanation: In the above program i accepted a number in the runtime and checking whether it is even or odd for this program first of all i must accept a number from the run time(out put screen) and the value should not be floating value( decimal values) for this reason i declare a variable datatype should be int and it stores the value from  -32768 to 32767  and it occupies two bytes memory. By using the scanf i accepted and %d is format specifier for the integer datatype (click here to see the format specifiers). Main task of the program is to check whether it is even or odd means if any number is divisible by 2 and remainder is 0 we say as even number other wise it is odd number here i required two operators 1. %  and 2. = = ( it is used to check the output value is equal to zero or not).


OUTPUT:  Enter a number: 5
                      5 is an odd no

No comments:

Post a Comment