# C Programming Operators9 min read

Table of Contents

## Introduction

**C Programming****Operators**consist of Variables, Constants, or array elements.- The operators could be of type integer, float, double, string, relational or logical. Relational and logical expressions return a Boolean (True or False) value while arithmetic expressions return either an integer expression, floating-point expression, or type double expression which is termed as an arithmetic expression.
- If you want to learn more about c programming language from a beginner’s level click here to read more.

C programming language is very rich in **built-in** operators. An **operator** is a symbol that tells the compiler the kind of operation it has to perform.

## Types of Operators

**Assignment Operator****Arithmetic Operators****Increment and Decrement Operators****Relational Operators****Logical Operators****Conditional/****Ternary**Operator**Bitwise Operators****Special Operators**

## 1. Assignment operators

A value can be assigned to the variable later on after declaration) Assigning value to variable can be performed through assignment operator (= )

In the assignment statement, there must be only one variable name that can appear on the left-hand side of the = sign and can hold only one value at a time expression may be constant, variable, or an expression. The expression on the **Right-hand side (RHS)** is evaluated, and the resulting value is assigned to the variable **LHS**.

**Ex:. int a, b, c;a = 6;b = a;c = a + b;**

**Note:** The left-hand variable is called Lvalue. The Right-hand operand is called **Rvalue**. No assignment statement is complete unless a single **Lvalue** is given.

**(a) Multiple Assignments:** More than one variable can be assigned with a single value or expression. It is also called a **chained assignment**.

**Ex. 1) int a, e, i, o, u, vowel;a=e=i=o=u= 0;**

In the above statement the variable a, e, i, 0, and u is initialized with the value 0 (zero) in a single statement using multiple assignment statement instead of writing five different statements for assigning the value separately,

**a = 0;e = 0;i= 0;o = 0;u= 0;**

## 2. Arithmetic operators

It is used to perform some arithmetic calculation on operand (it is nothing but data which is operated on) For example, consider the following expression **x + y**. Here x and y are the two variables that hold the data and are called **operands**.

In C programming the arithmetic operators are classified as,

Arithmetic Operators (+), Unary minus (-), Increment (++), Decrement (–), Addition (+), Minus (-), Multiplication (*), Division (/) Modulus (%)

**a) Unary Operators:** An operator who works on only one operand is known as a unary operator. Following are the unary operator supported by the ‘C’ language. a) Unary Minus (-): It is used to negate the value of the operand. Ex.: int a = 5, b; b -a;

**b) Increment Operator (++):** It is used to increment the operand value by 1.

**c) Decrement Operator (–):** It is used to decrement the operand value by 1.

**Binary Operators:** An operator who works on two operands is known as a Binary operator. Following are the binary operators.

**Division Operator:** If we divide an integer value by another integer value then the resultant value is also an integer and is known as Integer Division. And if one of the operands i.e. numerator or denominator is of floating type then the result will be floating-point type.

**Modulo Operator:** It gives a remainder after the numerator is divided by the denominator. When the numerator or denominator value is negative then the resultant remainder contains the sign of the numerator otherwise it is non-negative.

the expression takes on is Wal value for the desired result. It is used during an arithmetic operation called division.

**Syntax :- ( type_casting data type) expression**

Ex. :- 1) int a = 3, b = 2;

2) int a = 3, b = 2;

## 3. Increment/decrement operators

- The operation of adding 1 to a variable or subtraction of 1 from a variable frequency in programming and can be done in a better manner by means of (post or pre) and decrement (post or pre) operator 2.
- lt is a special form of a statement. We can increment or decrement the value of the variable only and not expression, This operator also helps to reduce the number of corresponding equivalents The ++ and –operators are left-associative.

For example, Instead of writing k=k+1 one can write it using increment operator ++k; similarly, k = k -1;

## 4. Relational operators

It is used to compare two values, either both numbers or expressions or characters. The associativity of the relational operators is from left to right. The relational operators are as follows,

Operator Meaning Less than Less than Equal to Greater than Greater than Equal to Equality Not equal (In-equality operator) !=

Relational Expression: A relational expression is made up of two operands connected with the relational operator. Relational expression returns zero when the condition is evaluated as false otherwise it returns a non-zero value.

**Syntax : ** **Variable/Expression Relational operator Variable/Expression/constant value**

## 5. Logical operators

Logical operators are used to combining two or more relational expressions Logical operator performs logical, i.e. Boolean, operation on values. They return zero when the condition is evaluated as false otherwise it returns a non-zero value.

The most general logical operator and hierarchy among them are,

1) **Not (!):** It returns true when its operand result is false and returns false when its operand result is true.

2) **And (&&):** It returns true when both operands are true otherwise it returns a false value.

3) **Or (ID):** It returns true when one of the values of the operand is true and false when the operands are false.

**Truth table for logical operators**

**Ex. 1) int a = 5, b = 7; int c = a && b; 2) int a = 5, b = 0; int c = a && b; 3) int a = 5, b = -5; int c = a && b; 4) int a = 5, b = 0; int c = a || b;**

**On Execution **

c = 1 (True) c = 0 (False) c = 1 (True) c = 1 (True)

## 6. Conditional operators

It is also known as **shorthand or ternary operator** or condensed if-then-else. 2 It is called ternary because it uses three expressions. The two symbols that are used to denote this operator are the question mark (?) and colon (;) & Three expressions are placed as given below,

i. The first expression is placed before.

ii. The second expression is placed between? and: Here the value of expression2 may be a constant value or variable.

iii. The third and final expression is placed after the symbol: Here the value of expression2 may be a constant value or variable.

**Syntax : Condition or Expression1_to_evaluate ? Expression2: Expression3;**

If the expression being evaluated is true then expression 2 will be performed otherwise expression 3 will be performed.

Ex. 1) To find the largest of two numbers

Large a > b? a : b;

If the value of variable a; is greater than b then value of a; is assigned to the variable large otherwise the value of b will be assigned.

2) To check the number of days in the month of February

days = ( year % 4 0) ? 29: 28;

3) Ternary operator can be nested (One within another). For example, consider the following example to find the largest of the three numbers

Large = (( a > b && a>c) ? a :((b>c) ? b : c)); Here, expression 3 itself evaluate another condition.

## 7. Bitwise operators

- The ‘C’ programming language provides facilities similar to assembly language to manipulate at the bit level in addition to operation at the byte (Group of 8 bit) level.
- Bit-level manipulations are used in setting a particular bit or group of bits to 1 or 0.
- Bit operations can be performed on any type of integer value in C, but cannot be performed on floating-point values.

C programming supports the following operators for bit manipulation:

## 8. Special operators

There are various special operators used in C programming to perform some particular type of operation. We will discuss some of them in detail later on. The following are some special operators,

**sizeof Operator :** Unary operator sizeof is used to find the number of bytes occupied by the operand. It is generally used in structure and union to know the number of bytes occupied by structure and union instead of calculating the length manually. It will help during the portability of code when data types vary in length from compiles to compiles.

**Syntax : sizeof(operand)**

Where operand may be basic or user-defined data type or constant value.

Ex: 1) c=sizeof(int);

On execution, the value of c contains 2, an integer data type required two bytes to store the value.

2) c=sizeof(float);

On execution, the value of c contains 4, as float data type required two bytes to store value.

3) c = sizeof(67);

On execution, the value contains 2, an integer constant that required two bytes to store value.

4) c = sizeof(6.7);

On execution the value of c contains 8, as value 6.7 by default is considered as double rather than float data type and hence it occupies eight bytes to store value.

5) c = sizeof(“abcde”)

O/p : 5 (It returns the number of characters enclosed within).