# Related Contents

- C Programming for Beginners Day 1
- C Programming for Beginners Day 2
- C Programming for Beginners Day 3
- C Programming for Beginners Day 4
- C programming for Beginners Day 5
- C Programming for Beginners Day 6
- C Programming for Beginners Day 7
- C Programming for beginners Day 8
- C Programming for beginners Day 9
- C Programming for beginners Day 10
- C Programming for beginners Day 11
- C Programming for Beginners Day 12

# C programming for Beginners Day 5

### Operators in C Language

Operators can be used in a C program to manipulate variables or values. C Operators can be used to perform arithmetic or logical operations on operands.

**C language has built-in operators and they may be classified as below −**

- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Conditional operators
- Special operators

#### Arithmetic operators

These can be used to perform arithmetic/mathematical operations on operands. The following list shows all the basic arithmetic operators with example.

**Addition:** The ‘+’ operator adds two operands. For example, x+y.

**Subtraction:** The ‘-‘ operator subtracts two operands. For example, x-y.

**Multiplication:** The ‘*’ operator multiplies two operands. For example, x*y.

**Division:** The ‘/’ operator divides the left operand by the right operand. For example, x/y.

**Modulus:** The ‘%’ operator returns the remainder by dividing left operand by the right operand. For example, x%y.

**Increment operator:** The ++ increases the integer value by one. For example, x++

**Decrement operator:** The — decreases the integer value by one. For example, x–-

Example program to demonstrate working of binary arithmetic operators

- // C example program to demonstrate the working of binary arithmetic operators
- #include <stdio.h>
- int main()
- {
- int x = 10, y = 4, res;
- //printing x and y
- printf(“value of x is %d and y is %d\n”, x, y);
- res = x+y; //addition
- printf(“value of x+y is %d\n”, res);
- res = x-y; //subtraction
- printf(“value of x -y is %d\n”, res);
- res = x*y; //multiplication
- printf(“value of x X y is %d\n”, res);
- res = x/y; //division
- printf(“value of x/y is %d\n”, res);
- res = a%b; //modulus
- printf(“value of x%%y is %d\n”, res);
- return 0;
- }

Output:

- value of x is 10 and y is 4
- value of x+y is 14
- value of x-y is 6
- value of xXy is 40
- value of x/y is 2
- value of x%y is 2

#### Increment and Decrement operators may be of two types as pre and post.

In the Pre-Increment/pre-decrement, value is first incremented/ decremented and then used inside the expression. In the Post-Increment/ post-decrement, value is first used in a expression and then incremented/ decremented.

- // C example program to demonstrate the working of Unary arithmetic operators
- #include <stdio.h>
- int main()
- {
- int a = 10, b = 4, res;
- // post-increment example:
- // res is assigned 10 only, a is not updated yet
- res = a++;
- printf(“a is %d and res is %d\n”, a, res); //a becomes 11 now
- // post-decrement example:
- // res is assigned 11 only, a is not updated yet
- res = a–;
- printf(“a is %d and res is %d\n”, a, res); //a becomes 10 now
- // pre-increment example:
- // res is assigned 11 now since a is updated here itself
- res = ++a;
- // a and res have same values = 11
- printf(“a is %d and res is %d\n”, a, res);
- // pre-decrement example:
- // res is assigned 10 only since a is updated here itself
- res = —a;
- // a and res have same values = 10
- printf(“a is %d and res is %d\n”,a,res);
- return 0;
- }

Output :

- a is 11 and res is 10
- a is 10 and res is 11
- a is 11 and res is 11
- a is 10 and res is 10

#### Relational Operators

The following list shows all the relational operators supported by C programming language.

**‘==’**operator can be used to check whether the two given operands are equal or not. If equal, it returns true. Otherwise it returns false. For example,**5==5**will return true.**‘!=’**operator can be used to check whether the two given operands are equal or not. If not equal, it returns true. Otherwise it returns false.**‘>’**operator can be used to check whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example,**6>5**will return true.**‘<‘**operator can be used to check whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example,**6<5**will return false.**‘>=’**operator can be used to check whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example,**5>=5**will return true.**‘<=’**operator can be used to check whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example,**5<=5**will also return true.

- // C example program to demonstrate working of relational operators
- #include <stdio.h>
- int main()
- {
- int a=10, b=4;
- // relational operators
- // greater than example
- if (a > b)
- printf(“a is greater than b\n”);
- else printf(“a is less than or equal to b\n”);
- // greater than equal to
- if (a >= b)
- printf(“a is greater than or equal to b\n”);
- else printf(“a is lesser than b\n”);
- // less than example
- if (a < b)
- printf(“a is less than b\n”);
- else printf(“a is greater than or equal to b\n”);
- // lesser than equal to
- if (a <= b)
- printf(“a is lesser than or equal to b\n”);
- else printf(“a is greater than b\n”);
- // equal to
- if (a == b)
- printf(“a is equal to b\n”);
- else printf(“a and b are not equal\n”);
- // not equal to
- if (a != b)
- printf(“a is not equal to b\n”);
- else printf(“a is equal b\n”);
- return 0;
- }

Output:

- a is greater than b
- a is greater than or equal to b
- a is greater than or equal to b
- a is greater than b
- a and b are not equal
- a is not equal to b

#### Logical operators

C language supports the following 3 logical operators.

Logical AND: The ‘&&’ operator returns true when conditions on both side is true or values on both sides in non zero.

Logical OR: The ‘||’ operator returns true when one (or both) of the conditions on both side is true or values one (or both) of the values is non zero.

Logical NOT: The ‘!’ operator returns true when the condition is not satisfied or false or the value is zero. Otherwise it returns false.

- // C program to demonstrate working of logical operators
- #include <stdio.h>
- int main()
- {
- int a=12, b=8, c = 12, d = 23;
- // logical operators
- // logical AND example
- if (a>b && c==d)
- printf(“a is greater than b AND c is equal to d\n”);
- else printf(“AND condition is not satisfied\n”);
- // logical AND example
- if (a>b || c==d)
- printf(“a is greater than b OR c is equal to d\n”);
- else printf(“Neither a is greater than b nor c is equal to d\n”);
- // logical NOT example
- if (!a)
- printf(“value of a is zero\n”);
- else printf(“value of a is not zero”);
- return 0;
- }

Output:

- AND condition not satisfied
- a is greater than b OR c is equal to d
- a is not zero

**Short-Circuiting in Logical Operators:**

In case of logical AND the second operand or expression is not evaluated if first operand is false. In case of logical OR the second operand or expression is not evaluated if first operand is true.

- // below program will not print second expression as first expression of logical AND is false
- #include <stdio.h>
- #include <stdbool.h>
- int main()
- {
- int a=12, b=14;
- bool res = ((a == b) && printf(“A is less then B”));
- return 0;
- }

.

- // below program will not print second expression as first expression of logical OR is false
- #include <stdio.h>
- #include <stdbool.h>
- int main()
- {
- int a=12, b=14;
- bool res = ((a != b) || printf(“A is not equal to B”));
- return 0;
- }

**Bitwise operators**

Bitwise operators works at bit level. Bitwise operators can not be applied to float or double data type. There are mainly 6 types of bitwise operators.

**bitwise or binary AND (&):** & Takes two operands and operate on each bit of both operands. Results 1 only if both bits are 1.

**bitwise or binary OR ( | ):** | Takes two operands and operate on every bit of two numbers. The result of bitwise OR is 1 if any of the two bits is 1.

**bitwise or binary XOR ( ^ ):** ^Takes two operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

**bitwise or binary left shift (<<):** << takes two operands. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted towards left. Both operands have the same precedence.

**bitwise or binary right shift (>>):** >> takes two operands. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted towards right. Both operands have the same precedence.

**~ (bitwise NOT): ** ~ Takes one operand and inverts all bits of it.

- /* C Program to demonstrate use of bitwise operators */
- #include <stdio.h>
- int main()
- {
- unsigned char a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
- printf(“a = %d, b = %d\n”, a, b);
- printf(“a&b = %d\n”, a&b); // The result is 00000001
- printf(“a|b = %d\n”, a|b); // The result is 00001101
- printf(“a^b = %d\n”, a^b); // The result is 00001100
- printf(“~a = %d\n”, a = ~a); // The result is 11111010
- printf(“b<<1 = %d\n”, b<<1); // The result is 00010010 printf(“b>>1 = %d\n”, b>>1); // The result is 00000100
- return 0;
- }

Output:

- a = 5, b = 9
- a&b = 1
- a|b = 13
- a^b = 12
- ~a = 250
- b<<1 = 18 b>>1 = 4

**Assignment operators**

The following list of assignment operators are supported by C language.

Simple assignment operator(=) : This operator assigns the value on right side operand to the variable on left side operand.

Add and assignment operator (+=) : This operator first adds the value of the left operand to the value on right operand and then assigns the result to the left operand. For example: a+=b is same as a = a + b.

Subtract and assignment operator (-=): This operator first subtracts the value of the left operand from the value of the right operand and then assigns the result to the left operand. For example: a -= b is same as a = a – b.

Multiply and assignment operator (*=): This operator first multiplies the value of the left operand to the value of the right operand and then assigns the result to the left operand. For example: a*= b is same as a = a * b.

Divide and assignment operator (/=): This operator first divides the value of the left operand to the value of the right operand and then assigns the result to the left operand. For example: a /= b is same as a = a / b.

Modulus and assignment operator (%=): This operator finds modulus using both operands then assigns the result to left operand. For example: a %= b is same as a = a % b.

Left shift and assignment operator (<<=): For example a <<= b is same as a = a << b

Right shift and assignment operator (>>=): For example a >>= b is same as a = a >> b

Bitwise AND and assignment operator ( &= ): For example a &= b is same as a = a & b

Bitwise OR and assignment operator ( |= ): For example a |= b is same as a = a | b

Bitwise XOR and assignment operator ( ^= ) : For example a ^= b is same as a = a ^ b

**Conditional operators**

The conditional operator in C language can also be called as ternary operator.

Ternary operator is the simplest operator form of if condition statement.

- if(expression){
- statement to be executed if expression is true
- }else{
- statement to be executed if expression is false
- }

now using ternary operator it can be written as below:

`(expression) ? (statement to be executed if expression is true) : (statement to be executed if expression is false)`

Example:

- #include <stdio.h>
- void main()
- {
- int a=5, b=6, c=7, large;
- large = a>b ? (a>c?a:c) : (b>c?b:c);
- printf(“The large number is %d”, large);
- }

Output:

- The large number is 7

**Special operators**

The other special operators used in C are as below:

sizeof() : returns the size of the variable. For example sizeof(a) will return 4 if a is an integer.

& : returns the address of the variable. For example &a returns the address of variable a.

* : pointer to a variable. For example *a will be pointer to variable a

. (dot) : used to select member via object name

, (comma) :

- Comma Operator has Lowest Precedence i.e it is having lowest priority so it is evaluated at last.
- Comma operator returns the value of the rightmost operand when multiple comma operators are used inside an expression.
- Comma Operator Can acts as –
- Operator : In the Expression
- Separator : Declaring Variable , In Function Call Parameter List

For Life time valid video tutorials of “C Programming For Beginners” course with quiz and certification : Click Here

Downloadable video tutorials of “C Programming For Beginners” for TTRC Player : Click Here

You must log in to post a comment.