1 c programming. 2 operators 3 operators in c an operator is a symbol that tells the computer to...

28
1 C Programming

Upload: cody-lewis

Post on 26-Mar-2015

237 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

1

C Programming

Page 2: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

2

Operators

Page 3: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

3

Operators in C

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

Page 4: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

4

Operands

The data item that operators act upon are called Operands.

a + b

Operands

Operator

Page 5: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

5

Types of Operators

On the basis of number of operands, operators are divided into three types:-

Unary Operators – Acts upon one operand Binary Operators – Acts upon two operands Ternary Operators – Acts upon three operands.

Page 6: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

6

Types of Operators:-

Arithmetic Operators Relational Operators Logical Operators Assignment Operators Increment or Decrement Operator Conditional Operators Bitwise Operators Special Operators

Page 7: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

7

Arithmetic Operators

Operator Symbol Action Example

Addition + Adds operands x + y

Subtraction - Subs second from first x - y

Negation - Negates operand -x

Multiplication * Multiplies operands x * y

Division / Divides first by second x / y

(integer quotient)

Modulus % Remainder of divide op x % y

Page 8: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

8

Relational Operators

Comparisons can be done with the help of relational operators These consist of:-

Operator Meaning

< less than

<= less than equal to

> greater than

>= greater than equal to

= = Equal to

!= Not equal to

Page 9: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

9

Logical Operators

Expression which combines two or more relational expression is termed as a logical or compound relational expression

The result of these operators is either TRUE or FALSE.

Logical expressions are:-

&& logical AND

|| Logical OR

! Logical NOT

Ex- if (age>55 && salary <1000)

Page 10: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

10

Assignment Operator

Used to assign the result of an expression to a variable.

ex- int i=20;

1. Simple Assignment e.g. =

2. Compound Assignment e.g. +=, -=, *=, /=, &=

3. Expression Assignment e.g. a=5+(b=8 + (c=2)) - 4

Page 11: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

11

Operators continued……..

x + = y+1;

is same as x = x + (y+1)

i.e. += means ‘add y+1 to x’

Similarly……..

a=a-1 a - = 1;

a=a*(n+1) a *= n+1;

a=a/(n+1) a/= n+1;

Page 12: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

12

Increment and Decrement

C has two very useful operators not generally found in other languages. These are the increment and decrement operators.

i.e ++ and --

++ operator adds 1 to the operand and -- operator subtracts 1 to the operand . Both these operators are unary operators and take the following form.

++m (prefix operator) ; or m++ (postfix operator);

--m; or m--;

++m = m+1; --m= m-1;

Page 13: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

13

Ex…

int m = 5 , y ;

y = ++m ;

printf ( “ %d%d " y,m );

Result???

int m = 5 , y ;

y = m++ ;

printf ( “ %d%d " y,m );

Result?????

Page 14: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

14

Conditional Operator..

A ternary operator pair “?:” is available in C to construct conditional expressions of the form.

exp1 ? exp2 : exp3;

Operator ?: works as follows: exp1 is evaluated first . If it is nonzero (true) ,then the expression exp2 is evaluated and becomes the value of the expression otherwise vice versa.

Page 15: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

15

Example….

a=10 ;

b=15;

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

It can be written as …

if ( a > b )

x = a;

else

x=b;

Page 16: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

16

Bitwise Operator….

C has distinction of supporting special operators known as Bitwise Operators for manipulation of data at bit level. These are used to test bits.

Operators Meaning

& bitwise AND

| bitwise OR

^ bitwise exclusive OR (Ex-OR)

<< shift left

>> shift right

~ One’s complement

Page 17: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

17

Bitwise Operator….

Examples - int a = 4, b = 3

a = 0000 0100

b = 0000 0011

--------------

a & b = 0000 0000

a | b = 0000 0111

a ^ b = 0000 0111

In Ex-OR (if both bits are same : 0) (if both bits are diff : 1)

a = 10

b = ~ a => ~ (1010) => 0101

Page 18: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

18

Bitwise operators The shift operator:

x << n Shifts the bits in x n positions to the left, shifting in zeros on

the right. If x = 1111 1111 1111 00002

x << 1 equals 1111 1111 1110 00002

x >> n Shifts the bits in x n positions right.

shifts in 0 if it is an unsigned integer

x >> 1 is 0111 1111 1111 10002 (unsigned)

Page 19: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

19

Operator Precedence

Operator Precedence Associativity

( ) 1 L to R

~, ++, --, unary - 2 R to L*, /, % 3 L to R+, - 4 L to R<<, >> 5 L to R<, <=, >, >= 6 L to R==, != 7 L to R& 8 L to R

^ 9 L to R ! 10 L to R

&& 11 L to R|| 12 L to R

? : 13 R to L=, +=, -=, etc. 14 R to L

Page 20: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

20

Special Operators….

C supports some special types of operators….

such as comma operators, sizeof operator, pointer operators

( & and * ) and member selection operators ( . and -> ).

Comma Operator:- The comma operator can be used to link the related expressions together. A comma-linked list of expressions are evaluated left to right and the value of right-most .

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

Page 21: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

Problems

21

Q1. a = 2 * 3 % 4 + 4 / 4 + 8 – (-2) + 5 / 8

Q2. kk = 3 / 2 * 4 % 3 + 3 / 8 + 3

Q3. int i = 4, j = -1, k = 0, y, z ; y = i + 5 && j + 1 || k + 2 ; z = i + 5 || j + 1 && k + 2 ; printf ( "\ny = %d z = %d", y, z ) ;

Page 22: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

Problems

22

Q5. int z , x = 5, y = - 10 , a = 4, b = 2z = x ++ - - y * b / a

Q4. int i = 4, j = -1, k = 0, w, x, y, z ; w = i || j || k ; x = i && j && k ; y = i || j && k ; z = i && j || k ; printf ( "\nw = %d x = %d y = %d z = %d", w, x, y,

z ) ;

Page 23: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

Problems

23

Q6. int a = 10, b b = a++ + ++ a; Print a , b

Q7. int a = 4; printf(“%d%d”, a++ + ++a, a++);

Q8. int i = 5;printf(“%d”, i = ++i = = 6);

Page 24: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

Problems

24

Q9. int a = 3, b b = ++a + ++ a + ++a; Print a , b

Q10. int a = 4, b ; b = a++ + a++ + a++; Print a , b

Q11. int i = 5, j ;j = ++i + i++ + ++i; Print i , j

Page 25: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

Problems

25

Q12. Point out the errors, if any—

(a) int 314.562 * 150;

(b) name = ’Ajay’ ;

(c) Varchar = ’3’ ;

(d) 3.14 * r * r * h = vol_of_cyl ;

(e) area = 3.14 * r ** 2;

(f) a = b = 3 = 4 ;

Page 26: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

Problems

26

Q13. void main(){ float a = 5, b = 2; int c; c = a % b; printf(”%d”, c);

Q14. int c=0,d=5,e=10,a; a=c>1?d>1||e>1?10:20:30; printf(“a=%d”,a);

Page 27: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

Problems

27

Q15. int x, y, z ; x=y=z=1; z= ++x || ++y && ++z; printf(“%d%d%d”,x,y,z);

Q16. #define x 5+2 void main( )

{ int a; a = x * x * x; printf(“%d”, a); }

Page 28: 1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation

Problems

28

Q17. void main( ){     int a;

    a=sizeof( 5.6 );    printf("%d",a); }

Q18. int a=0,b=10;    if(a=0){          printf("true");    }    else{         printf("false");    }