The C language programming. Operators & Expression.

INTRODUCTION

                    C supports a rich set of operators. We have already used several of them. such as. +. -. *. & and < An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation Operators, are used in programs to manipulate data and variables. They usually form a part of the mathematical of logical expressions.
C operators can be classified into several categories. 

They include:
1. Arithmetic operators.
2. Relational operators.
3. Logical operators,
4. Assignment operators.
5. Increment and decrement operators.
6. Conditional operators.
7. Bitwise operators.
8. Special operators.


1.ARITHMETIC OPERATORS

                    C provides all the basic arithmetic operators. The operators +,-,*, and / all work the same way as they do in other languages. These can operate on any built-in data type allowed in C. The unary minus operator, in effect, multiplies its single operand by -1. Therefore, a number preceded by a minus sign changes its sign.

 Operator   Meaning
 + Addition or unary plus 
 - Subtraction or unary minus 
 * Multiplication 
 / Division 
 % Modulo division

a - b        a + b 
a * b        a / b
a % b        - a * b

                    Here a and b are variables and are known as operands. The modulo division operator cannot be used on floating-point data.
                    Note that C does not have an operator for exponentiation. Older versions of C do not support unary plus but ANSI C supports it.

1.1. Integer arithmetic operators 

                    When both the operands in a single arithmetic expression such as a+b are integers, the expression is called an integer expression, and the operation is called integer arithmetic Integer arithmetic always yields an integer value. The largest integer value depends on the machine, a pointed out earlier In the above examples, if a and b are integers, then for a = 15 and b = 9 we have the following results
a = 15 and b = 9 
                a - b = 6
                a +  b = 24
                a * b = 135
                a / b = 1
                a % b = 6
                    During modulo division, the sign of the result is always the sign of the first permanent arithmetic oper (the dividend.) That is may assume values rounded to the num the correct result
                -14 % 3 = -2
                -14 % -3 = -2 
                14 % -3 = 2

1.2. Real Arithmetic

                    An arithmetic operation involving only real operands is called real arithmetic. A real operand may assume values either in decimal or exponential notation. Since floating-point values are rounded to the number of significant digits permissible, the final value is an approximation of the correct result. If x, y, and z are floats, then we will have:
                x = 6.0/ 700857143
                y = 1.0/3.0 = 0333333
                Z = -2.0/30 = - 0666667
The operator % cannot be used with real operands.

2.RELATIONAL OPERATORS

                    We often compare two quantities and depending on their relation, take certain decisions. For example, we may compare the age of two persons, or the price of two items, and so on. These comparisons can be done with the help of relational operators. We have already used the symbol <, meaning less than'. An expression such as < b or 1< 20 containing a relational operator is termed as a relational expression. The value of a relational expression is either one or zero. It is one of the specified relations that is true and zeroes the relation is false.
                9<11 ture 
but 
                9>11 false 
 Operators Meaning
 <is less than 
 <=is less than or equal to 
 >is Greater than
 >=is Greater than or equal to 
 = =is Equal to
 !=is Equal to

                del-1 relational operator del-2
del-1and del-2 are arithmetic expressions, which may be simple constants, variables, or combos ton of them.

3.LOGICAL OPERATORS

                        In addition to the relational operators, C has the following three logical operators. 
&& meaning logical AND. 
|| meaning logical OR. 
! meaning logical NOT.
                        The logical operators && and || are used when we want to test more than one condition and make decisions. An example is:
                a > b && a == 10
                a > b || b == 9 
                        An expression of this kind which combines two or more relational expressions is termed as a logical expression or a compound relational expression. Like the simple relational expressions, a logical expression also yields a value of one or zero, according to the truth table. The logical expression given above is true only if a > b is true and x == 10 is true. If either (or both of them are false, the expression is false.

Truth table
 input 1input 2 output (&&) output (||) 
 1
 1


5.ASSIGNMENT OPERATORS

                    Assignment operators are used to assigning the result of an expression to a variable. We have seen the usual assignment operator, '= '. Also, C has a set of 'shorthand assignment operators of the form.
                x ao= exp;
                    when x is a variable, exp is an expression and ao is a C binary arithmetic operator. The operator ao is known as the shorthand assignment operator.
  
The assignment statement
                x op= exp
                            can also be written as
                x = x ao (exp)
for example 
                x=x+y;
                    can be written as 
                x+=y;
 A statement with the simple assignment operator A statement with shorthand operator
 a = a + 1 a += 1
 a = a - 1 a -= 1
 a = a * (n+1) a *= n+1
 a = a/ (n+1) a = n+1
 a = a % b a %= b

                    The use of shorthand assignment operators has three advantages:
1 What appears on the left-hand side need not be repeated and therefore it becomes easier to write
2. The statement is more concise and easier to read.
3. The statement is more efficient. 

These advantages may be appreciated if we consider a slightly more involved statement,
                value(15*x-9)=value(15*x-9) + del;
With the help of the + operator, this can be written as follows:
                value(15*x-9) += del;

5.INCREMENT AND DECREMENT OPERATORS

                    C has two very useful operators not generally found in other languages. These are the increment and decrement operators: ++ and --
The operator ++ adds 1 to the operand while -- subtracts 1. Both are unary operators and take the following form:
                ++x; or x++;
                --x; or x--; 
++x; is equivalent to x = x+1: (or x += 1)
--x; is equivalent to x = x-1: (or x -= 1:) 

                    We use the increment and decrement statements in for and while loops extensively. While ++x and x++ mean the same thing when they form statements independently, they behave differently when they are used in expressions on the right-hand side of an assignment statement.
                    
                    Post-increment and pre-increment 
post-increment: store incremented value and then print the value 
pre-increment: print the value and then store incremented value

x = 5 
print for 1st time 
x++ : 6 to store and 6 to print  
++x: 6 to store and 5 to print 

print for 2nd time
x++ : 7 to store and 7 to print  
++x: 7 to store and 6 to print 

6.CONDITIONAL OPERATOR

                    A ternary operator pair "?:" is available in C to construct conditional expressions of the form:
                exp1 ? exp2=exp3;
                    where exp1, exp2, and exp3 are expressions. The operator ?: works as follows: expl is evaluated first. If it is nonzero (true), then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note that only one of the expressions (either exp2 or exp3) is evaluated. 
For example, consider the following statements.
                a = 15; 
                b = 9; 
                x = (a > b) ? a: b;
                output a = 15

7.BITWISE OPERATORS

                    C has a distinction of supporting special operators known as bitwise operators for manipulation of data at a bit level. These operators are used for testing the bits or shifting them right or left. Bitwise operators may not be applied to float or double. Table 3.5 lists the bitwise operators and their meanings.

Operator  Meaning
 & (Shift + 7) bitwise AND
 ! (Shift + 1) bitwise OR
 ^ (Shift + 6) bitwise exclusive OR
 <<shift left
 >>  shift right
 ~ (Shift + first key before 1) One's complement

8.SPECIAL OPERATORS

                    C supports some special operators of interest such as comma operator, sizeof operator, pointer operators (& and *), and member selection operators (and ->). The comma and sizeof operators are discussed in this section while the pointer operators and others would be discussed on another blog

8.1 The Comma Operator

                    The comma operator can be used to link the related expressions together. A comma-linked list of expressions is evaluated left to right and the value of the right-most expression is the value of the combined expression.

8.2 The sizeof Operator

                    The sizeof is a compile-time operator and, when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier

ARITHMETIC EXPRESSIONS

                    An arithmetic expression is a combination of variables, constants, and operators arranged as per the syntax of the language. We have used several simple expressions in the examples discussed so far. C can handle any complex mathematical expressions. Remember that C does not have an operator for exponentiation

EVALUATION OF EXPRESSIONS

                    Expressions are evaluated using an assignment statement of the form
                variable = expression,
                    Variable is any valid C variable name. When the statement is encountered, the expression evaluated first and the result then replaces the previous value of the variable on the left side. AM variables used in the expression must be assigned values before evaluation attempted. Examples of evaluation statements are
                x = a *b - C;
                y = b/c *a;
                z = a - b* c + d;
                    The blank space around an operator is optional and adds only to improve readability. When these statements are used in a program, the variables a, b, c, and d must be defined before being used in the expressions.

PRECEDENCE OF ARITHMETIC OPERATORS

                    An arithmetic expression without parentheses will be evaluated from left to right using the rules of precedence of operators. There are two distinct priority levels of arithmetic operation in C

Priority table.


 Operators Description Associativity Priority rank
 ()

[]

 Function call

Array element reference

 Left to Right 1
 +

-

++

--

!

-

*

&

Sizeof

(type)

 Unary plus

Unary minus

Increment

Decrement

Logical negation

Ones complement

Pointer reference (indirection)

Address

Size of an object

Typecast (conversion)

 Right to left 2
 *

/

%

 Multiplication 

Division

Modulus

 Left to Right 3
 +

-

 Addition 

Subtraction

 Left to Right 4
 << 

>> 

 Left shift

Right shift

 Left to Right 5
 < 

<=

> 

>=

 Less than

Less than or equal to

Greater than

Greater than or equal to

 Left to Right 6
 ==

!=

 Equality

Inequality

 Left to Right 7
 & Bitwise AND Left to Right 8
 ^ Bitwise XOR Left to Right 9
 | Bitwise OR Left to Right 10
 && Logical AND Left to Right 11
 || Logical OR Left to Right 12
 ?: Conditional expression Right to left 13
 =

*, /, %

+, -, &

^, |

<<, >>

Assignment operators Right to left 14
 , Comma operator Left to Right 15


Hope this founding your curiosity level and for much more stay tuned and subscribe for latest updates

~Aaditya Rasala.

Comments

Post a Comment

Popular Posts