DEV Community

VYSHNAVI
VYSHNAVI

Posted on

Operators and Expressions

Operators form expressions by joining individual constants, variables, array elements as discussed in previous lesson. C includes a large number of operators which fall into different categories. In this lesson we will see how arithmetic operators, unary operators, relational and logical operators, assignment operators and the conditional operators are used to form expressions.
The data items on which operators act upon are called operands. Some operators require two operands while others require only one operand. Most operators allow the individual operands to be expressions. A few operators permit only single variable as operand.
OPERATORS AND EXPRESSIONS IN ‘C’ OBJECTIVES
After going through this learn you will be able to
•recognize arithmetic operators
•explain unary operators
•define relational, logical, assignment & conditional operators
•explain library functions.
OPERATORS AND EXPRESSIONS IN ‘C’ - Index
•ARITHMETIC OPERATORS
•UNARY OPERATORS
•RELATIONAL, LOGICAL, ASSIGNMENT, CONDITIONAL OPERATORS
•LIBRARY FUCNTIONS
•Important Questions

Operator precedence:
Expressions are normally evaluated left to right. Complex expressions are evaluated one at a time. The order in which the expressions are evaluated is determined by the precedence of the operators used. The standard C ordering is followed.
negation (-) unary
power
multiplication, division and modulo
addition and subtraction
If an expression contains two or more operators with the same precedence, the operator to the left is evaluated first. For example, 10 / 2 * 5 will be evaluated as (10 / 2) and the result multiplied by 5.

When a lower precedence operation should be processed first, it should be enclosed within parentheses. For example, 30 / 2 + 8. This is normally evaluated as 30 divided by 2 then 8 added to the result. If you want to divide by 2 + 8, it should be written as 30 / (2 + 8).

Parentheses can be nested within expressions. Innermost parenthetical expressions are evaluated first.
Operator precedence
Expressions are normally evaluated left to right. Complex expressions are evaluated one at a time. The order in which the expressions are evaluated is determined by the precedence of the operators used. The standard C ordering is followed.
negation (-) unary
power
multiplication, division and modulo
addition and subtraction
If an expression contains two or more operators with the same precedence, the operator to the left is evaluated first. For example, 10 / 2 * 5 will be evaluated as (10 / 2) and the result multiplied by 5.

When a lower precedence operation should be processed first, it should be enclosed within parentheses. For example, 30 / 2 + 8. This is normally evaluated as 30 divided by 2 then 8 added to the result. If you want to divide by 2 + 8, it should be written as 30 / (2 + 8).

Parentheses can be nested within expressions. Innermost parenthetical expressions are evaluated first.

Expressions perform specific actions, based on an operator, with one or two operands. An operand can be a constant, a variable or a function result. Operators are arithmetic, logical, and relational. As with C, some operators vary in functionality according to the data type of the operands specified in the expression.

Arithmetic operators ( +, -, , /, *, % )
Arithmetic operators perform mathematical operations such as addition and subtraction with operands. There are two types of mathematical operators: unary and binary. Unary operators perform an action with a single operand. Binary operators perform actions with two operands. In a complex expression, (two or more operands) the order of evaluation depends on precedence rules.
Unary arithmetic operators
Unary operators are arithmetic operators that perform an action on a single operand. The script language recognizes the unary operator negative (-).
The negative unary operator reverses the sign of an expression from positive to negative or vice-versa. The net effect is that of multiplying the number by -1.

Top comments (0)