5/21/2012
Arithmetic operators:
C o p era tion Addition Subtraction Multiplication Division Modulus Arithm etic op era to r + * / % Alg eb ra ic exp ressio n f+7 pc bm x/y r mod s C exp ressio n f p b x r + * / % 7 c m y s
Rules of operator precedence:
Operator(s) () Operation(s) Parentheses Order of evaluation (precedence) Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right. Multiplication,Divi Evaluated second. If there are several, they are sion, Modulus evaluated left to right. Addition Evaluated last. If there are several, they are Subtraction evaluated left to right.
*, /, or % + or 1
ASSIGNMENT OPERATOR
The basic assignment operator is (=) Operand= Expression; Where the left operand gets the value of the expression on the rights. a=3; this is also an assignment operator x=x+3;
5/21/2012
Type Conversions in expressions
The set of implicit conversions is where the lower type is promoted to the higher type, where the order of the types is char < short int < int < long int < float < double < long double
Casting
(type) expression Example: (float) x/2 If int x = 3, result is 1.500000
X= (int)7.5; X = (int)21.3 X= (float)5/(float)2
Operators (Contd..)
Increment and Decrement
Increment operators(++) adds 1 to its operand Decrement operators (--) subtracts 1 from its operand. x = x + 1; same as x ++; or ++ x; ; x = x - 1; same as x --; or -- x; ; ; Difference between x ++ and ++ x
Example
x = 10; y = ++ x; printf( y = %d ,y); x = 10; y = x ++; printf(y = %d,y);
4
5/21/2012
INCREMENT and DECREMENT OPERATORS
a=10; b=5; a=a+1 ; ++a a=a-1:--a; b=b+1; sum=a+b; sum=a+(++b); sum++; ++sum; Prefix operator Postfix operator ++m and m++
Operators (Contd..)
Relational operators It performs tests on their operands. They return he Boolean value 1if the statement is successful (true) 0 otherwise
Example
a == b a != b a<b a<b a<=b a>=b
Name
Equal Not Equal less than greater than less than or equal to
Result
TRUE if a is equal to b. TRUE if a is not equal to b. TRUE if a is strictly less than b. TRUE if a is strictly greater than b. TRUE if a is less than or equal to b.
greater than or equal to
TRUE if a is greater than or equal to b.
6
5/21/2012
Operators (Contd)
LOGICAL OPERATORS
Example
!a a && b a || b
Name
Not And Or
Result
TRUE if a is not TRUE. TRUE if both a and b are TRUE.
TRUE if either a or b is TRUE.
a
0 0 1 1
b
0 1 0 1
a && b
0 0 0 1
a || b
0 1 1 1
Assignment Operators
5/21/2012
The ? operators
#include<stdio.h> #include<conio.h> void main() { int a,b; printf("\n Enter the value of athe variable\"a\":"); scanf("%d",&a); b= a>25 ? 100 : 99; printf("The value of the variable \"b\":%d",b); getch(); }
Example
#include<stdio.h> #include<conio.h> void main() { int a,b; printf("\n Enter the value of athe variable\"a\":"); scanf("%d",&a); b= ((a>25)&&(a<50)) ? 100 : 99; printf("The value of the variable \"b\":%d",b); getch(); }
10
5/21/2012
Example
#include<stdio.h> #include<conio.h> void main() { int a,b; printf("\n Enter the value of athe variable\"a\":"); scanf("%d",&a); b=(a!=10) ? 100 : 99; printf("The value of the variable \"b\":%d",b); getch(); }
11
The , operator
A comma-linked list of expressions are evaluated left to right and The value of the right-most expression is the value of the combined expression. P=(x=10,y=5,x+y);
12