You are on page 1of 15

Operators and Expressions

An operator is simply a symbol that is used to perform operations. There can be


many types of operations like arithmetic, logical, bitwise, etc.

There are following types of operators to perform different types of operations in


C language.

• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Conditional Operators
• Shift Operators
• Special Operators

Arithmetic Operators:

An arithmetic operator performs mathematical operations such as addition,


subtraction, multiplication, division etc on numerical values (constants and
variables).
For Example:

Output:
The operators +, - and * computes addition, subtraction, and multiplication
respectively.

In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.

It is because both the variables a and b are integers. Hence, the output is also an
integer. The compiler neglects the term after the decimal point and shows answer
2 instead of 2.25.

The modulo operator % computes the remainder. When a=9 is divided by b=4, the
remainder is 1. The % operator can only be used with integers.

Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,

Increment and Decrement Operators:

C programming has two operators increment ++ and decrement -- to change the


value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the value


by 1. These two operators are unary operators, meaning they only operate on a
single operand.

For Example:
Output:

Here, the operators ++ and -- are used as prefixes. These two operators can also
be used as postfixes like a++ and a--.

Assignment Operators:

An assignment operator is used for assigning a value to a variable. The most


common assignment operator is =.
For Example:
Output:

Relational Operators:

A relational operator checks the relationship between two operands. If the


relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.

For Example:
Output:

Logical Operators:
An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false. Logical operators are commonly used in
decision making in C programming.

For Example:
Output:

Bitwise Operators:
During computation, mathematical operations like: addition, subtraction,
multiplication, division, etc are converted to bit-level which makes processing
faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

In arithmetic-logic unit (which is within the CPU), mathematical operations like:


addition, subtraction, multiplication and division are done in bit-level. To perform
bit-level operations in C programming, bitwise operators are used.

Bitwise AND Operator:

The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If


either bit of an operand is 0, the result of corresponding bit is evaluated to 0.

Let us suppose the bitwise AND operation of two integers 12 and 25.
For Example:

Output:

Bitwise OR Operator:

The output of bitwise OR is 1 if at least one corresponding bit of two operands is


1. In C Programming, bitwise OR operator is denoted by |.

For Example:
Output:

Bitwise XOR(exclusive OR Operator):

The result of bitwise XOR operator is 1 if the corresponding bits of two operands
are opposite. It is denoted by ^.

For Example:

Output:
Conditional Operators:

The conditional operator is also known as a ternary operator. The conditional


statements are the decision-making statements which depends upon the output
of the expression. It is represented by two symbols, i.e., '?' and ':'.

As conditional operator works on three operands, so it is also known as the


ternary operator.

Syntax of a conditional operator:

Expression1? expression2: expression3;

• In the above syntax, the expression1 is a Boolean condition that can be


either true or false value.
• If the expression1 results into a true value, then the expression2 will
execute.
• The expression2 is said to be true only when it returns a non-zero value.
• If the expression1 returns false value then the expression3 will execute.

For Example:
In the above code, we are taking input as the 'age' of the user. After taking input,
we have applied the condition by using a conditional operator. In this condition,
we are checking the age of the user. If the age of the user is greater than or equal
to 18, then the statement1 will execute, i.e., (printf("eligible for voting"))
otherwise, statement2 will execute, i.e., (printf("not eligible for voting")).

Outputs:

As we can observe from the above two outputs that if the condition is true, then
the statement1 is executed; otherwise, statement2 will be executed.

Another Example:
In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5
value to the 'a' variable. After the declaration, we are assigning value to the 'b'
variable by using the conditional operator. If the value of 'a' is equal to 5 then 'b'
is assigned with a 3 value otherwise 2.

Output:

You might also like