Monday 26 August 2013

C Programming for beginners, operators used in C

Operators used in C



x=0; y=10; z=20;

Operator Usage Example Result
Arithmatic Operators
Addition of variables x = y + z  x=30
 - Subtraction of variables
alter variables +/- sign
x = y-z
x = -y 
x=-10
x= -20
 * Multiplication of variables  x = y*z  x=200
 /  Division of variables x = y/z where z>0 x=0 (integer)
x=0.500000(float)
 %  Modulus Operator  x = y%z x=10
 ++  Increment Operator
increase variable value by 1
 x++ x=1
 -- Decrement Operator
decrease variable value by 1
 x-- x=-1
Comparison/Relational Operators
 == Logical Equals to x==y FALSE or 0
 != Not Equal to x!=y TRUE or 1
  > Greater Than z>y
x>y
TRUE or 1
FALSE or 0
 < Less Than z<y
x<y
FALSE or 0
TRUE or 1
 >= Greater Than or Equals to z=10
z>=y
x>=y

TRUE or 1
FALSE or 0
 <= Less Than or Equals to y=0
z<=y
x<=y

FALSE or 0
TRUE or 1
Logical Operators
 ! Logical NOT
variable value zero than it returns 1
else returns 0(Zero)
!x
!y
1
0
 && Logical AND
returns 1 when both the values are same
else returns 0(Zero)
0 && 0
0 && 1
1 && 0
1 && 1
0
0
0
1
 || Logical OR
returns 0(Zero) when both the values are 0(Zero)
else returns 1
0 && 0
0 && 1
1 && 0
1 && 1
0
1
1
1
Bitwise Operators
 ~ Bitwise NOT x becomes binary 0000, so ~x  in bitwise 1111...

y=8 becomes binary 1000, so ~y becomes ...1110111
-1


-9
 & Bitwise AND x=5, y=6
z=x&y
x-0101
y-0110
-----------
z-0100 




4
 | Bitwise OR x=5, y=6
z=x&y
x-0101
y-0110
-----------
z- 0111




7
 

No comments:

Post a Comment

Contact Form

Name

Email *

Message *