09 - Operators in C' - PPTX

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

10/19/2011

Comparison operators/Relational operators


Operator name Syntax a == b a != b a<b a>b

Operators in C
Nihar Ranjan Roy

Equal to

Not equal to Less than

Greater than Greater than or equal to Less than or equal to

a <= b

a >= b

Nihar Ranjan Roy

Arithmetic Operators
Operator name Syntax a=b a+b a-b +a -a Operator name

Logical operators
Syntax a && b a || b !a

Basic assignment Addition Subtraction

Logical negation (NOT) Logical AND

Unary plus (integer promotion) Unary minus (additive inverse) Multiplication Division

a*b

int i=5; printf("%d",++i); printf("%d",i++); printf("%d",i);

Logical OR

Write a program for checking weather a given year is a leap year or not.

Modulo (remainder) Prefix Increment Suffix Prefix Decrement Suffix

a%b ++a a++ --a a-Nihar Ranjan Roy

a/b

int yr; puts ("please enter an year"); scanf("%d",&yr); if((yr%4==0 && yr%100!=0)|| yr%400==0) puts("its a leap year"); else puts("its not a leap year");

6 6 7

Nihar Ranjan Roy

10/19/2011

Bitwise operators
Operator name Syntax ~a a&b a|b a^b a << b a >> b Operator name

Other Operators
C first evaluates the condition. Based on the Function call a(a1, a2) return value of the condition the second or Comma a, b third operand is evaluated: Ternary/ conditional a?b:c If the condition is evaluated to true (1), only sizeof(a) the second operand (expression 1) is Size-of sizeof(type) evaluated. Then the operator return the value of the expression 1. Cast (type) a If the condition is evaluated to false (0), only the third operand (expression 2) is The ternary operator can be rewritten as evaluated. The value of the expression 2 is the if-else and return statement as follows: returned. if(condition) return expression1; int a=2,b=3,c; else c=(a>b)?a:b; //biggest of a & b return expression2; Syntax

condition ? expression 1 : expression 2

Bitwise Bitwise Bitwise Bitwise Bitwise Bitwise

a=72, b=184 C=a&b;

NOT AND OR XOR left shift right shift

72 0 1 0 0 1 0 0 0 184 1 0 1 1 1 0 0 0 8 0 0 0 0 1 0 0 0

int i=16; i=i<<1; printf("%d",i);

Original

0 0 0 1 0 0 0 0

after shift 0 0 1 0 0 0 0 0
Nihar Ranjan Roy 5

printf("%d",c);

Nihar Ranjan Roy

Member and pointer operators


Array subscript Indirection ("object pointed to by a") Reference ("address of a") Member b of object a Member b of object pointed to by a Operator name Syntax a[b] *a &a

Operator Precedence
The precedence determines the order of binding in chained expressions, when it is not expressly specified by parentheses. For example,

a->b a.b

++x*3

It is ambiguous without some precedence rule(s). The precedence table tells us that: x is 'bound' more tightly to ++ than to *, so that whatever ++ does (now or latersee below), it does it ONLY to x (and not to x*3); it is equivalent to (++x, x*3).

Nihar Ranjan Roy

Nihar Ranjan Roy

10/19/2011

Operator Precedence
Precedence Operator ++ -() [] . ++ -+ ! ~ (type) & sizeof -> Description Suffix increment Suffix decrement Function call Array subscripting Element selection by reference Element selection through pointer Prefix increment Prefix decrement Unary plus Unary minus Logical NOT Logical bitwise NOT Type cast Address-of Size-of Associativity Precedence Operator

Operator Precedence..
Description Associativity

15

Left-to-right

13
Right-to-left

15

?: = += -= *= /= %= <<= >>= &= ^= |= ,

Ternary conditional Direct assignment Assignment by sum Assignment by difference Assignment by product Assignment by quotient Assignment by remainder Assignment by bitwise left shift Assignment by bitwise right shift Assignment by bitwise AND Assignment by bitwise XOR Assignment by bitwise OR Comma

Right-to-left

Left-to-right

Nihar Ranjan Roy

Nihar Ranjan Roy

11

Operator Precedence..
Precedence 3 4 5 6 7 8 9 10 11 12 Operator * / % + << >> < <= > >= == != & ^ | && || Description Multiplication division modulus (remainder) Addition subtraction Bitwise left shift Bitwise right shift For relational operators < respectively For relational operators respectively For relational operators > respectively For relational operators respectively For relational = respectively Bitwise AND Bitwise XOR (exclusive or) Bitwise OR (inclusive or) Logical AND Logical OR Nihar Ranjan Roy 10 Associativity

Evaluate
Evaluate

3*x++,

where though the post-fix ++ is designed to act AFTER the entire expression is evaluated, the precedence table makes it clear that ONLY x gets incremented (and NOT3*x); it is functionally equivalent to something like (tmp=3*x, ++x, tmp) with tmp being a temporary value.

Left-to-right

Nihar Ranjan Roy

12

10/19/2011

Casting (Implicit & Explicit)


Implicit Casting (automatic transformation) works in a way that a variable (operand) of data type that is smaller in length (than data type of second variable) (operand), transforming internally to variable of data type with longer number length.

Overflow
char a=255; a=a+1 printf("%d",a); Output? 0

Short int

int

Unsigned int

Long int

Unsigned long int

float

double

Long double

float a; int b=1,c=2; a=b+c;

Nihar Ranjan Roy

13

Nihar Ranjan Roy

15

Casting (Implicit & Explicit)


Explicit Casting When one data types has higher priority then automatic transformation This is not done automatically, the programmer has to do it explicitly Syntax (data_type) operand Float a; int b=1,c=2; a=b/c;

//output is 0.00 //output is 0.5000

a=(float) b/c;

Nihar Ranjan Roy

14

You might also like