operators in c language

18
1

Upload: amit-singh

Post on 22-May-2015

1.197 views

Category:

Engineering


14 download

TRANSCRIPT

Page 1: Operators in c language

1

Page 2: Operators in c language

An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations.

These operators are used in programs to manipulate data and variables.

2

Page 3: Operators in c language

1. Arithmetic operators

2. Relational operators

3. Logical operators

4. Assignment operators

5. Increment and decrement operators

6. Conditional operators (ternary operator)

7. Bitwise operators

8. Special operators

3

Page 4: Operators in c language

Arithmetic operators are used to perform numerical calculations among the values.

OPERATOR MEANING

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulo Division

4

Page 5: Operators in c language

#include<stdio.h>

Main()

{

int x=25;

int y=4;

printf(“%d+%d=%d\n,x,y,x+y);

printf(“%d-%d=%d\n,x,y,x-y);

printf(“%d*%d=%d\n,x,y,x*y);

printf(“%d/%d=%d\n,x,y,x/y);

printf(“%d%%%d=%d\n,x,y,x%y);

5

Page 6: Operators in c language

Relational operator are used to compare two operands.

Operands may be variable, constant or expression.

OperatorOperator MeaningMeaning

< Is less than

<= Is less than equal to

> Is greater than

>= Is greater than equal to

== Equal to

!= is not equal to

6

Page 7: Operators in c language

#include<stdio.h>

void main()

{

int a, b;

printf(“Enter values for a and b : ");

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

printf("\n The < value of a is %d", a<b);

printf("\n The <= value of a is %d", a<=b);

printf("\n The > value of a is %d", a>b);

printf("\n The >= value of a is %d", a>=b);

printf("\n The == value of a is %d", a==b);

printf("\n The != value of a is %d", a!=b);

}7

Page 8: Operators in c language

LOGICAL OPERATORS:

OPERATOR MEANING

&& Logical AND

|| Logical OR

! Logical NOT

These operators are used for testing more than one condition and making decisions. 'C' has three logical operators they are:

8

Page 9: Operators in c language

Example:-Example:-

#Include<stdio.h>void main()

{ int a, b; printf(“enter values for a and b : "); scanf(“%d %d", &a, &b); printf("\n %d",(a<b)&&(a!=B)); printf("\n %d",(a<b)||(b<a));

printf("\n %d",!(A==b));}

9

Page 10: Operators in c language

ASSIGNMENT OPERATORS

Assignment operator are used to assign the value or an expression or a variable to another variable. The syntax is

variablename = expression;

i.g. A=b; Operators:

== , += , -= , *= , /= , %= , >>= , <<= , &= ,

10

Page 11: Operators in c language

Increments & decrement operator is also called Unary . The increment operator (++++) adder on to the variable and decrement (- -- -) subtract one from the variable. There are following unary operator

INCREMENT & DECREMENT INCREMENT & DECREMENT OPERATORSOPERATORS

Operator Meaning

++x++x Pre increment

- -x- -x Pre decrement

x++x++ Post increment

X- -X- - Post decrement

11

Page 12: Operators in c language

INCREMENT & DECREMENT OPERATORS INCREMENT & DECREMENT OPERATORS EXAMPLEEXAMPLE #include<stdio.h>

void main()

{

int a,b,c;

printf("Enter the values for a and b :");

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

printf("\n The value of c is %d", c=++a);

printf("\n The value of c is %d", c=a++);

printf("\n The value of c is %d", c=--b);

printf("\n The value of c is %d", c=b--);

}

12

Page 13: Operators in c language

CONDITIONAL CONDITIONAL OPERATORSOPERATORSThese conditional operator are used to construct

conditional expressions of the form.

Syntax:

exp1?exp2:exp3.

where exp1,exp2,exp3 are expressions.

Operator: ?: (ternary operator)

13

Page 14: Operators in c language

Example:-Example:-#include<stdio.h>

void main()

{

int a, b, x;

printf("Enter the values of a add b : ");

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

x=(a>b)?a:b;

printf("Biggest Value is :%d",x);

}

14

Page 15: Operators in c language

Bitwise Operator:-Bitwise Operator:-Are used by the programmer to communicate directly with the hardware.These operator are used for designing bit or shifting them either right to left, left to right.

Example:-

OPERATOR MEANING

& Bitwise AND

| Bitwise OR

^ Bitwise Exclusive OR

<< Shift Left

>> Shift Right15

Page 16: Operators in c language

SPECIAL OPERATORSSPECIAL OPERATORS 'C' supports some special operators such as comma

operator, sizeof operator and pointer pointer operators.

Comma operator:Comma operator: the comma operator is used to combine related

expressions. A comma linked list of expressions are evaluated

left to right and the value of right most expression is the value of combined expression..

Example: value=(x=10, y=5, x+y);

16

Page 17: Operators in c language

Special Operators Special Operators Contd…Contd… Sizeof Operator:Sizeof Operator:

Sizeof is an operator used to return the number of bytes the operand occupies.

Syntax:

m=sizeof(sum);

k=sizeof(2351);

17

Page 18: Operators in c language

18