Home » » what is operator in c

what is operator in c

what is operator in c

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. Operators, functions, constants and variables are combined together to form expressions. Consider the expression A + B * 5 where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression. C language is rich in built-in operators and provides the following types of operators:

Types of Operators

Arithmetic operators: These are used to perform mathematical calculations like addition,

subtraction, multiplication, division and modulus

Assignment operators: These are used to assign the values for the variables in C programs.

Relational operators: These operators are used to compare the value of two variables.

Logical operators : These operators are used to perform logical operations on the given two variables.

Bit wise operators : These operators are used to perform bit operations on given two variables.

Conditional (ternary) operators:  Conditional operators return one value if condition is true and returns another value is condition is false.

Increment/decrement operators: These operators are used to either increase or decrease the value of the variable by one.

Special operators : &, *, sizeof( ) and ternary operators.


ARITHMETIC OPERATORS

Computer programs are widely used for mathematical calculations. In C language we have the usual arithmetic operators for addition, subtraction, multiplication and division. C also provides a special arithmetic operator which is called modulus. All these operators are binary operators which means they operate on two operands. So we need two values for addition, subtraction, multiplication, division and modulus. The following table lists down a few of the important arithmetic operators available in C programming language. 

Addition, subtraction and multiplication are same as we use in algebra. There is one thing to note in division that when we use integer division (i.e. both operands are integers) yields an integer result. This means that if, for example, you are dividing 5 by 2 (5 / 2) it will give integer result as 2 instead of actual result 2.5. Thus in integer division the result is truncated to the whole number, the fractional part (after decimal) is ignored. If we want to get the correct result, then we should use float data type. 


The modulus

operator returns the remainder after division. This operator can only be used with integer operands. The expression x % y returns the remainder after x is divided by y. For example, the result of 5 % 2 will be 1, 23 % 5 will be 3 and 107%10 will be 7.


INCREMENT, DECREMENT OPERATORS (++, --)

Increment and decrement are common arithmetic operation. C provides two short cuts for the same.

Postfix- will return the current value and then increment/ decrement.

x++ is a short cut for x=x+1; will return the current value and then increment.

x-- is a short cut for x=x-1; will return the current value and then decrement.


Prefix:

++x is a short cut for x=x+1; will increment x and then return the value (so it will return one greater than the original value)

--x is a short cut for x=x-1; will decrement a and then return the value (so it will return one less than the original value)


PRECEDENCE OF OPERATORS

The arithmetic operators in an expression are evaluated according to their precedence. The precedence means which operator will be evaluated first and which will be evaluated after that and so on. In an expression, the parentheses ( ) are used to force the evaluation order. The operators in the parentheses ( ) are evaluated first. If there are nested parentheses then the inner most is evaluated first. The expressions are always evaluated from left to right. The operators *, / and % have the highest precedence after parentheses. These operators are evaluated before + and – operators. Thus + and – operators has the lowest precedence. It means that if there are * and + operators in an expression then first the * will be evaluated and then its result will be added to other operand. If there are * and / operators in an expression then the operator which occurs first from left will be evaluated first and then the next, except you force any operator to evaluate by putting parentheses around it. 


Let’s look some examples. What is the result of 10 + 10 * 5? The answer is 60 not 100. As * has higher precedence than + so 10 * 5 is evaluated first and then the answer 50 is added to 10 and we get the result 60. The answer will be 100 if we force the addition operation to be done first by putting 10+10 in parentheses. Thus the same expression rewritten as (10 + 10) * 5 will give the result 100. Note that how the parentheses affect the evaluation of an expression. Similarly the expression 5 * 3 + 6 / 3 gives the answer 17, and not 7. The evaluation of this expression can be clarified by writing it with the use of parentheses as (5 * 3) + (6 /3) which gives 15 + 2 = 17. Thus you should be careful while writing arithmetic expressions.


1. Suppose a, b and c are integer variables that have been assigned the values a = 8, b= 3 and c = -5. Determine the value of each of the following arithmetic expressions.


a) a +b + c

b) 2 * b + 3 * (a-c)

c) a / b

d) a % b

e) a * b / c

f) a * (b / c)

g) (a * c) % b

h) a * (c % b).


2. Is 2 + 5 * 2 equal to (2 + 5) * 2?


3. Does 7 % 2 produce the same result as 4 % 3?

0 comments:

Post a Comment

Contact form

Name

Email *

Message *