Expression & Operators in C

Unit 4. Expression & Operators


C Operators

1.Arithmetic Operators  (+,-,/,*,%)

2.Assignment Operators (=)

3.Bit-Wise Operator (&,|,^,>>,<<,~)

4.Increment and Decrement Operator (++,--)

5.Logical Operators (&&,||,!)

6.Relational Operators (>,<,>>,<<,=,!=)

7.Ternary Operator (? :)

8.Special Operator (,sizeof(),pointer *,->)

1.       Arithmetic Operators

C programming language supports almost common arithmetic operator such as +,-,* and modulus operator %. Modulus operator (%) returns the remainder of integer division calculation. The operators have precedence rules which are the same rule in math.

2.        Assignment Operators

C assignment operators are used to assign the value of a variable or expression to a variable. The syntax of assignment operators is:
var = expression;
var = var;

Each assignment operator has a priority and they are evaluated from right to left based on its priority. Here is assignment operator and its priority: =, +=, -=, *=, /=, %=

var +=expression; //means
2.var = var + expression;

3.        Bit-wise Operators

Bitwise operators interpret operands as strings of bits. Bit operations are performed on this data to get the bit strings. These bit strings are then interpreted according to data type. There are six bit operators: 
bitwise AND(&), 
bitwise OR(|), 
bitwise XOR(^), 
bitwise complement(~), 
left shift(<<),
 and right shift(>>). 

We use bitwise operators in some programming contexts because bitwise operations are faster than (+) and (-) operations and significantly faster than (*) and (/) operations

4.       Increment and decrement Operators

We can use increment operator to increase or decrease the value of variable. C increment operators support in both prefix and postfix form. Here are syntax of of increment operators:
++m; or m++; ++m; is equivalent to m=m+1 or m+=1;
--m; or m--; --m; is equivalent to m=m-1 or m-=1;
++m and m++ have the same meaning when they are use independently but behave differently when used in expressions on the right hand side of an assignment statement.
                Example:             m=5;
                                                Y=++m;
                                                Here the value of y and m would be 6
                                                m=5;
                                                y=m++;
                                                Here the value of y would be 5 and m would be 6

5.       Logical Operators

Logical operators allow us to combine relational operators or logical operators into one Boolean result. C programming language supports the 
Not (!), 
logical AND (&&), 
and logical OR (||).

6.       Relational Operators

Relational operators in C programming language are as follows: <, <=, >, >=, ==, != . They are used in Boolean conditions or expression and returns true or false.
like a>=b,a==b,a!=b

7.       Ternary Operator (Conditional Operator)

C Ternary Operator
C ternary operator is shorthand of combination of if and returns statement. Syntax of ternary operator is as follows:

(condition ) ? expression2: expression3

c=a>b?a:b  is same as

If(a>b)
{
c=a;
}
else
{
c=b;
        }
the above statement will check a is greater than b, if it is result will be b or a.

     8.       Special Operator
C supports some special operators like comma operator, sizeof() operator, pointer operators, and member selection operators (. And ->).
Comma operator (use of comma in for loop)
                for(n=1,m=10;n<=m;n++;m++)
sizeof() operator
                It is a compile time operator and when we use with an operand it number of bytes operand occupies. The operand may be number, constant or array.
                int a;
                float b;
                printf(“Size of integer:%d”,sizeof(a));
                printf(“Size of float:%d”,sizeof(b));
               
                output:
                Size of integer:2
                Size of integer:4


Expression

An expression is a combination of variables constants and operators written according to the syntax of C language.Expression is ended with the semicolon (;).
Some of the examples of expression are
a=b;
sum=a+b;
printf("hello");

Arithmetic Expression
Arithmetic expression consists of variable name , constants and arithmetic operators like +,-,/,* and the assignment operator =

Algebraic Expression

Arithmetic Expression

a x b – c
a * b – c
(m + n) (x + y)
(m + n) * (x + y)
(ab / c)
a * b / c
3x2 +2x + 1
3*x*x+2*x+1
(x / y) + c
x / y + c

Example: Area of circle
area=3.14*r*;
here in above arithmetic expression variables, constant, arithmetic operator * and assignment operator are used.
in above expression Variables and Constants together are called operands.
int a=10; 
is used to store the value in variable or constants.

Associativity of Operator
The operators of same precedence are evaluated from right to left or from left to right depending on the level. This is known as associativity property of an operator.

Associativity can be of two type
Right to Left 
Left to Right

Example Right to Left
a=b=10;

Example Left to Right
c=a+b;

Evaluation of Expression
Assignment operator is used to evaluate the expression

variable=(expression);

computer evaluates the arithmetic expression and assign the value to the variable.
Assignment operator = have right to left associativity 
examples:
sum=a+b;
z=a+b/c;
x=a+b/c*d;
where computer will first evaluate the expression on right side of = and then assign the value to the variable.

Precedence of Arithmetic Operators
Each operator in C has a precedence associated with it. Arithmetic expression will be evaluated from left to right.

high priority * / %
low priority + -

but parenthesis () can effect the result of the expression as evaluation begins from the parenthesis, if it is there.

If two operators have same priority like the statement
c=a*b/d;
where  / and * have same priority, it will be evaluated left to right.



RULES FOR EVALUATION OF EXPRESSION 

·     First parenthesized( ), sub expression from left to right are evaluated.
If parenthesis are nested(( )), the evaluation begins with the innermost sub expression. 
Arithmetic expressions are evaluated from left to right using the rules of precedence. 
When Parenthesis are used, the expressions within parenthesis assume highest priority.







0 comments:

Post a Comment