Friday, September 17, 2010

Operators of the C Language

Table Operators of the C Language.

Primary Relational
()      Parentheses

x[y]   value of yth element of array x

x.y     value of y field of structure x

Unary
x<y (<,<=,>=)   x less than y,
                                        etc. 0 if false, 1
                                       otherwise
Equality
x==y (x!=y)           x equal (not equal) y

Bitwise (in order of precedence)
!x             not x; !x=0 if x is
                nonzero,1
                otherwise
˜x              1s complement of x.
                 0s become
                 1s and 1s become 0s.
++x(--x)    x is incremented
                 (decremented)
                 before use
x++(x--)    x is incremented
                 (decremented)
                 after use
-x              arithmetic negation of x
*x             value at address x
&x            address of x
sizeof x     # bytes in x

Multiplication
x&y     bitwise and of x and y, 1&1=1,
                   else 0
x^y      bitwise xor of x and y, 1^0=0^1=1,
            else 0
x | y     bitwise or of x and y,0|0=0, else 1


Logical (in order of precedence)
x&&y  1 if both x and y are nonzero, else 0
x| |y     1 if x or y are nonzero, else 0


Conditional
x?y:z      y if x is nonzero, z otherwise

Assignmment
x*y          product (quotient) of x and y
x%y        x MOD y

Addition
x=y        x gets the value of y
xop=y    x gets the value of xopy,
              where op 
              may be +, -, *, /, %, >>, <<, &, ^,
              or |.




Comma
x+y (x-y)  sum (difference) of x
                and y
Shift
x<>y)  x gets left (right) shifted y
                       places
x,y         x, then y, are evaluated,
             expression
             gets value of y
©Hendro Sinaga www.4tatc.co.cc

C has an if  and an if....else statement, as well as repeat, while, do...while...., switch, and for statements.
An example of a C for statement is:

for (i=0;  i<5; i++)  x=i;

The first expression gives a 0 start for the loop, it terminates when i==5, and i is incremented by 1 after it is used (i++). x will be assigned successively: 0, 1, 2, 3, and 4.

No comments:

Post a Comment