Operators and Expressions

Operators and Expressions

Learn everything about operators and expressions in programming. Explore types of operators—arithmetic, logical, comparison, and more—along with detailed examples and usage to boost your coding skills.

Operators

An operator is a symbol that helps the user to command the computer to do certain mathematical or logical manipulations. Operators are used in C language programs to operate on data and variables. C has a rich set of operators which can be classified as:

  1. Arithmetic operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Unary Operators
  6. Conditional Operators
  7. Bitwise Operators
  8. Special Operators

1. Arithmetic Operators

All the basic arithmetic operations can be carried out in C programming. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language. Unary operations operate on a single operand, therefore the number 5 when operated by unary – will have the value –5.

Operator Meaning
+ Addition or Unary Plus
Subtraction or Unary Minus
* Multiplication
/ Division
% Modulus Operator

Examples of arithmetic operators are

x + y
x – y
-x + y
a * b + c
-a * b

etc., here a, b, c, x, y are known as operands. The modulus operator is a special operator in C language
which evaluates the remainder of the operands after division.

Example

#include<stdio.h>
void main(){
int numb1, num2, sum, sub, mul, div, mod;
scanf (“%d %d”, &num1, &num2);
//addition of numbers and storing in sum.
sum = num1 + num2;
printf(“\n Thu sum is = %d”, sum);
//subtraction of numbers and storing in sub.
sub = num1 – num2;
printf(“\n Thu difference is = %d”, sub); //display the output
//multiplication of numbers and storing in mul.
mul = num1 * num2;
printf(“\n Thu product is = %d”, mul);
//division of numbers and storing in div.
div = num1 / num2;
printf(“\n Thu division is = %d”, div);
//modulus of numbers and storing in mod.
mod = num1 % num2;
printf(“\n Thu modulus is = %d”, mod);
}

Integer Arithmetic

When an arithmetic operation is performed on two whole numbers or integers then such an operation is called integer arithmetic. It always gives an integer as the result. Let x = 27 and y = 5 be 2 integer numbers. Then the integer operation leads to the following results.

x + y = 32
x – y = 22
x * y = 115
x % y = 2
x / y = 5

In integer division, the fractional part is truncated.

Floating Point Arithmetic

When an arithmetic operation is preformed on two real numbers or fraction numbers such an operation is called floating point arithmetic. The floating point results can be truncated according to the properties requirement. The remainder operator is not applicable for floating point arithmetic operands.

Let x = 14.0 and y = 4.0 then
x + y = 18.0
x – y = 10.0
x * y = 56.0
x / y = 3.50

Mixed mode arithmetic

When one of the operands is real and the other is an integer and if the arithmetic operation is carried out on these 2 operands then it is called mixed mode arithmetic. If any operand is of real type then the result will always be real thus 15/10.0 = 1.5

2. Relational Operators

Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator comes into the picture. C supports the following relational operators.

Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to

It is required to compare the marks of 2 students, and the salary of 2 persons, we can compare them using relational operators.

A simple relational expression contains only one relational operator and takes the following form.

exp1 relational operator exp2

Where exp1 and exp2 are expressions, which may be simple constants, variables, or a combination of them. Given below is a list of examples of relational expressions and evaluated values.

6.5 <= 25 TRUE
-65 > 0 FALSE
10 < 7 + 5 TRUE

Relational expressions are used in decision-making statements of C language such as if, while, and for
statements to decide the course of action of a running program.

3. Logical Operators

C has the following logical operators, they compare or evaluate logical and relational expressions.

Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

Logical AND (&&)

This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true.

Example

a > b && x = = 10

The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10.

Logical OR (||)

The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2
expressions are true.

Example

a < m || a < n

The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true
if a is less than either m or n and when a is less than both m and n.

Logical NOT (!)

The logical not operator takes a single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words, it just reverses the value of the expression.

For example

! (x >= y) the NOT expression evaluates to true only if the value of x is neither greater than or equal to y

4. Assignment Operators

The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the
value or variable on the left of the expression.

Example

x = a + b

Here the value of a + b is evaluated and substituted to the variable x. In addition, C has a set of shorthand assignment operators of the form.

var oper = exp;

Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as a shorthand assignment operator.

Example

x + = 1 is same as x = x + 1

The commonly used shorthand assignment operators are as follows Shorthand assignment operators.

Statement with simple assignment operator 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

 

Example for using a shorthand assignment operator

#include<stdio.h>
#define N 100
#define A 2
void main(){
int a;
a = A;
while (a < N)
{
printf(“%d \n”, a);
//shorthand form of a = a * a
a *= a;
}
}

Output

2
4
16

5. Unary Operators

The increment and decrement operators are one of the unary operators which are very useful in C language. They are extensively used in for and while loops. The syntax of the operators is given below

  1. ++ variable name
  2. variable name++
  3. – –variable name
  4. variable name– –

The increment operator ++ adds the value 1 to the current value of the operand and the decrement operator – – subtracts the value 1 from the current value of the operand. ++variable name and variable name++ mean the same thing when they form statements independently, they behave differently when they are used in expression on the right-hand side of an assignment statement.

Consider the following

m = 5;
y = ++m; (prefix)

In this case, the value of y and m would be 6

Suppose we rewrite the above statement as:

m = 5;
y = m++; (post fix)

Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left. On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand.

6. Conditional or Ternary Operator

The conditional operator consists of 2 symbols the question mark (?) and the colon (:)

The syntax for a ternary operator is as follows

exp1 ? exp2 : exp3

The ternary operator works as follows

exp1 is evaluated first. If the expression is true then exp2 is evaluated & its value 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 is evaluated.

For example

a = 10;
b = 15;
x = (a > b) ? a: b

Here x will be assigned to the value of b. The condition follows that the expression is false therefore b
is assigned to x.

#include<stdio.h>
void main(){
int i,j,larger;
printf (“Input 2 integers : “);
scanf(“%d %d”,&i, &j);
larger = i > j ? i : j;
printf(“The largest of two numbers is %d \n”, larger);
}

Output

Input 2 integers: 34 45
The largest of the two numbers is 45

7. Bitwise Operators

C has a distinction of supporting special operators known as bitwise operators for the manipulation of data at the bit level. A bitwise operator operates on each bit of data. Those operators are used for testing,
complementing, or shifting bits to the right or left. Bitwise operators may not be applied to a float or
double.

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive
<< Shift left
>> Shift right

8. Special Operators

C supports some special operators of interest such as comma operator, size of operator, pointer operators (& and *), and member selection operators (. and ->). The size of and the comma operators are discussed here. The remaining operators are discussed in forthcoming chapters.

The Comma Operator

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

For example the statement

value = (x = 10, y = 5, x + y);

First assigns 10 to x and 5 to y and finally assigns 15 to value. Since comma has the lowest precedence
in operators the parenthesis is necessary. Some examples of comma operators are

In for loops:

for (n=1, m=10; n <=m; n++,m++)

In while loops

While (c=getchar(), c != ‘10)

Exchanging values

t = x, x = y, y = t;

The size of Operator

The operator size gives the size of the data type or variable in terms of bytes occupied in the
memory. The operand may be a variable, a constant, or a data type qualifier.

Example

m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);

The size of an operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables
during the execution of the program.

Example program that employs different kinds of operators. The results of their evaluation are also shown in comparison

#include<stdio.h>
void main()
{
int a, b, c, d;
a = 15; b = 10; c = ++a-b;
printf (“a = %d, b = %d, c = %d\n”, a,b,c);
d=b++ + a;
printf (“a = %d, b = %d, d = %d\n, a,b,d);
printf (“a / b = %d\n, a / b);
printf (“a %% b = %d\n, a % b);
printf (“a *= b = %d\n, a *= b);
printf (“%d\n, (c > d) ? 1 : 0 );
printf (“%d\n, (c < d) ? 1 : 0 );
}

Notice the way the increment operator ++ works when used in an expression. In the statement c = ++a –
b; new value a = 16 is used thus giving value 6 to C. That is a is incremented by 1 before using in
expression.

However in the statement d = b++ + a; The old value b = 10 is used in the expression. Here b is
incremented after it is used in the expression.

We can print the character % by placing it immediately after another % character in the control string.
This is illustrated by the statement.

printf(“a %% b = %d\n”, a%b);

This program also illustrates that the expression

c > d ? 1 : 0

Assumes the value 0 when c is less than d and 1 when c is greater than d.

 Expressions

Arithmetic Expressions

An expression is a combination of variables constants and operators written according to the syntax of C language. In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Some examples of C expressions are shown in the table given below.

Algebraic Expression C 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

Evaluation of Expressions

Expressions are evaluated using an assignment statement of the form

Variable = expression;

A variable is any valid C variable name. When the statement is encountered, the expression is evaluated
first and then replaces the previous value of the variable on the left-hand side. All variables used in the
expression must be assigned values before evaluation is attempted.

Examples of evaluation statements are

x = a * b – c
y = b / c * a
z = a – b / c + d;

The following program illustrates the effect of the presence of parenthesis in expressions.

main ()
{
float a, b, c x, y, z;
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 21;
y = a – b / (3 + c) * (21);
z = a – ( b / (3 + c) * 2)1;
printf (“x = %fn”,x);
printf (“y = %fn”,y);
printf (“z = %fn”,z);
}

output

x = 10.00
y = 7.00
z = 4.00

Precedence in Arithmetic Operators

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

High priority * / %
Low priority + –

Rules for evaluation of expression

  • First, parenthesized sub-expressions left to right are evaluated.
  • If the parenthesis is nested, the evaluation begins with the innermost sub-expression.
  • The precedence rule is applied in determining the order of application of operators in evaluating sub-expressions.
  • The associability rule is applied when two or more operators of the same precedence level appear in
    the sub-expression.
  • Arithmetic expressions are evaluated from left to right using the rules of precedence.
  • When Parenthesis is used, the expressions within parenthesis assume the highest priority.

Operator precedence and associativity

Each operator in C has a precedence associated with it. The precedence is used to determine how an
expression involving more than one operator is evaluated. There are distinct levels of precedence and an
operator may belong to one of these levels. The operators of higher precedence are evaluated first.

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

The table given below gives the precedence of each operator.

Category Operator Associativity
Postfix () [] -> . ++ – – Left to right
Unary + – ! ~ ++ – – (type) * & sizeof Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right

 

Operators and expressions are the heart of any programming language, allowing you to manipulate data and control program flow with precision. By mastering how operators interact and how expressions are evaluated, you’re equipped to write clean, efficient, and effective code. Whether you’re solving complex problems or optimizing everyday tasks, understanding these concepts opens up endless possibilities in your programming journey. Keep experimenting, keep learning—this is just the beginning!”

Leave a Reply

Your email address will not be published. Required fields are marked *

Featured Posts: