Operators in C

Operators:-

C has a lot of built-in operators.Operators are symbols, which are use to operate or manupulate operands.There are various types of operators:-

1.Arithmetic operators:- Arithmetic Operators are used for Arithmetical calculation.
There are five Arithmetic operators in C:
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer division

2.Relational operators:- Relational operators are used to compare two operands and to check whether they are equal, unequal, greater than and lesser than other.
There are 6 relational operators:
Operator Meaning
< Less than
> Greater than
<= Less than equal to
>= Greater than equal to
== Equal to
!= Not equal to
The value of the relational operator is either one or zero. If the relation is true, result is 1 otherwise it is 0.

3.Logical operators:- Logical operators are used to logically compare two or more relational expressions.
There are three logical operators:
Operator Meaning
&& Logical And
|| Logical or
! Logical not
The expression which combines two or more relational expressions is termed as logical expression or compound relational expression.
The result of a logical expression is either one or zero.
e.x.:-
a) if (age > 50 && weight < 80)
b) if (a < 0 || ch = = 'a')
c) if ( ! (a < 0))

4.Bitwise operators:- The smallest element in memory on which we are able to operate as yield is a byte,and we operate on it by use of the data type char Bitwise operator is used for manipulation of data at bit level.
These operators are used for testing the bits, shifting them right to left. Bitwise operator may not be applied to float or double data type.
This is a powerful feature of C to manipulate a bit. The programmer can access and manipulate individual bits within a piece of data.
Some of the bitwise operators in C are:
Operator Meaning
& Bitwise Logical AND
| Bitwise Logical OR
^ Bitwise Logical XOR
<< Left Shift
>> Right Shift
~ Once Compliment

5.Comma operator:- This operator is used to link the related expression together the expression is separated by the, operator.
e.x.:- int a,b,c;
c=(a=1,b=2,a+b); //Here the value of c will be 3.
Here firstly value 1 is assigned to a, followed by this 2 is assigned to b, and then the result of a+b is assigned to c.
The comma operator is often used in conjunction with a control statement called For.

6.Increment and decrement operator:- These types of operators operate on only one operand, therefore these operators are also called Unary operators.
These two powerful operators in C are + + (Increment), _ _ (Decrement). Operands must be declared as variables not a constant.
These operators may be used either after or before the operand.
When they are used before the operand, it is termed as Prefix while when they are used after the operand they are termed as Postfix.
In prefix operations the value of operator is incremented or decremented first and then the expression is evaluated. Prefix operators has the effect of Change then use.
In postfix operation the expression is evaluated first and then the value of operator is either incremented or decremented. Postfix operators has the effect of Use Then Change.
e.x.:- b=a++; this is postfix increment expression. In the expression firstly b=a; then a=a+1; will be executed ,
b=--a; this is prefix increment expression, firstly a = a-1; then b=a; will be executed.

7.Dot(.) and Arrow(->) operator:- These operators access individual elements of structures and unions. The dot operator is used when working with a structure or union directly. The Arrow operator is used with a pointer to a structure or union.
e.x.:- struct employee
{
char nm[50];
int id;
float wage;
}emp;
struct employee *p = &emp; //address of emp into p.
emp.wage=123.12; //we are assigning 123.12 to the wage member of structure variable emp using dot operator.
The same assignment using a pointer to emp will be:- p->wage = 123.12;
8.Conditional and ternary operator:- The conditional operator? and: are sometimes called ternary operators.
A ternary operator is one which contains three operands.The general form of ternary operator is:
"exp 1 ? exp 2 : exp 3"
The operator works as:- if exp 1 is evaluated first. If the result is true then exp 2 is executed, otherwise exp 3 is executed.
e.x.:-
int x,y;
y=(x>5?3:4) // here if value of x is greater than 5, than value of y will be 3. else value of y will be 4.

8.Assignment operators:- It is very basic operator, used in every programming language. It assigns some value to a variable. General syntex for assignment operator are:- "variable_name = expression;"
Here value of expression will be assigned to vaiable_name. Value of expression may be a simple value or complex value.

Some important function of assignment operators:-

i) Type casting (Conversion in assignment):- When variables of one data type are mixed with variables of another data type, a type conversion
will occour. this is called "type casting".In type casting, the value of the right side of the assignment is converted to the type of the left side.
e.g.:- int x;
char ch;
void fun(void)
{
ch=(char)x;
}

ii)Multiple assignments:- By using multiple assignment operator, we can assign same value to more than one variable in a single statement.
e.x.:- int a,b,c;
a=b=c=5; // here we are assigning 5 to each variable a,b&c.
iii)Compound assignments:- There is a variation on the assignment statement, called "compound assignment".It simplifies the coding of a certain type of asignment operator.
e.x.:- x=x+5;
x+=5; // both the statements are same.

iv)Type modifier:- The basic data types may have modifier preceding them to indicate special properties of the object being declared.
These modifiers change the meaning of the basic data types to suit the specific needs.
These modifiers are unsigned, signed, long and short. It is also possible to give these modifiers in combination, e.g., unsigned long int.
e.x.:- main()
{
char ch=291;
printf("%d\t%c\n",ch,ch);
}
Here the output will be :- 35
Here ch has been defined as a char ,and char cannot take a value bigger than +128.That is why assigned value of ch is 291 and is considered to be 35(291-128-128).

Comments

Popular posts from this blog

Components of C language

Decision making using if statement

How to make payment for final certificate, migration, provisional for the students of last semester of ptu?