Arithmetic Operators:: Assignment Operator
Arithmetic Operators:: Assignment Operator
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
*, /, 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
Casting
(type) expression Example: (float) x/2 If int x = 3, result is 1.500000
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
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.
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.
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