top of page
Writer's pictureApurba Paul

C Operators & Expressions_1: Arithmetic operators, relational and logical operators

The symbols which are used to perform logical and mathematical operations in a C program are called C operators. These C operators join individual constants and variables to form expressions. Operators, functions, constants, and variables are combined together to form expressions. Consider the expression A + B * 5. where, +, * are operators, A, B  are variables, 5 is constant and A + B * 5 is an expression.

TYPES OF C OPERATORS:

C language offers many types of operators. They are,

Arithmetic operators

Assignment operators

Relational operators

Logical operators

Bitwise operators

Conditional operators (ternary operators)

Increment/decrement operators

Special operators


ARITHMETIC OPERATORS IN C:

C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus in C programs.

EXAMPLE PROGRAM FOR C ARITHMETIC OPERATORS:

In this example program, two values “40” and “20” are used to perform arithmetic operations such as addition, subtraction, multiplication, division, modulus and output is displayed for each operation.

#include <stdio.h>


int main()

{

int a=40,b=20, add,sub,mul,div,mod;

add = a+b;

sub = a-b;

mul = a*b;

div = a/b;

mod = a%b;

printf("Addition of a, b is : %d\n", add);

printf("Subtraction of a, b is : %d\n", sub);

printf("Multiplication of a, b is : %d\n", mul);

printf("Division of a, b is : %d\n", div);

printf("Modulus of a, b is : %d\n", mod);

}


ASSIGNMENT OPERATORS IN C:

In C programs, values for the variables are assigned using assignment operators.For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;” There are 2 categories of assignment operators in C language. They are, 1. The simple assignment operator ( Example: = ) 2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )

EXAMPLE PROGRAM FOR C ASSIGNMENT OPERATORS:

In this program, values from 0 – 9 are summed up and total “45” is displayed as output. Assignment operators such as “=” and “+=” are used in this program to assign the values and to sum up the values.

# include <stdio.h>


int main()

{

int Total=0,i;

for(i=0;i<10;i++)

{

Total+=i; // This is same as Total = Toatal+i

}

printf("Total = %d", Total);

}


RELATIONAL OPERATORS IN C:

Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.

EXAMPLE PROGRAM FOR RELATIONAL OPERATORS IN C:

In this program, relational operator (==) is used to compare 2 values whether they are equal are not.If both values are equal, output is displayed as ” values are equal”. Else, output is displayed as “values are not equal”.Note : double equal sign (==) should be used to compare 2 values. We should not single equal sign (=).

#include <stdio.h>


int main()

{

int m=40,n=20;

if (m == n)

{

printf("m and n are equal");

}

else

{

printf("m and n are not equal");

}

}

LOGICAL OPERATORS IN C:

These operators are used to perform logical operations on the given expressions.There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).

EXAMPLE PROGRAM FOR LOGICAL OPERATORS IN C:


#include <stdio.h>


int main()

{

int m=40,n=20;

int o=20,p=30;

if (m>n && m !=0)

{

printf("&& Operator : Both conditions are true\n");

}

if (o>p || p!=20)

{

printf("|| Operator : Only one condition is true\n");

}

if (!(m>n && m !=0))

{

printf("! Operator : Both conditions are true\n");

}

else

{

printf("! Operator : Both conditions are true. " \

"But, status is inverted as false\n");

}

}


In this program, operators (&&, || and !) are used to perform logical operations on the given expressions.&& operator – “if clause” becomes true only when both conditions (m>n and m! =0) is true. Else, it becomes false.|| Operator – “if clause” becomes true when any one of the condition (o>p || p!=20) is true. It becomes false when none of the condition is true.! Operator  – It is used to reverses the state of the operand.If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by “!” operator.So, “! (m>n and m! =0)” returns false (0).


BIT WISE OPERATORS IN C:

These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.Bit wise operators in C language are

& (bitwise AND),

| (bitwise OR),

~ (bitwise NOT),

^ (XOR),

<< (left shift)

and >> (right shift).






BELOW ARE THE BIT-WISE OPERATORS AND THEIR NAME IN C LANGUAGE.

& – Bitwise AND

| – Bitwise OR

~ – Bitwise NOT

^ – XOR

<< – Left Shift

>> – Right Shift

Consider x=40 and y=80. Binary form of these values are given below.

x = 00101000 y=  01010000

All bit wise operations for x and y are given below.

x&y = 00000000 (binary) = 0 (decimal)

x|y = 01111000 (binary) = 120 (decimal)

~x = 11111111111111111111111111 11111111111111111111111111111111010111 = -41 (decimal)

x^y = 01111000 (binary) = 120 (decimal)

x << 1 = 01010000 (binary) = 80 (decimal)

x >> 1 = 00010100 (binary) = 20 (decimal)


NOTE:

Bit wise NOT : Value of 40 in binary is 00000000000000000000000000000000 00000000000000000010100000000000. So, all 0’s are converted into 1’s in bit wise NOT operation.

Bit wise left shift and right shift : In left shift operation “x << 1 “, 1 means that the bits will be left shifted by one place. If we use it as “x << 2 “,  then, it means that the bits will be left shifted by 2 places.


EXAMPLE PROGRAM FOR BIT WISE OPERATORS IN C:

In this example program, bit wise operations are performed as shown above and output is displayed in decimal format.

#include <stdio.h>


int main()

{

int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;

AND_opr = (m&n);

OR_opr = (m|n);

NOT_opr = (~m);

XOR_opr = (m^n);

printf("AND_opr value = %d\n",AND_opr );

printf("OR_opr value = %d\n",OR_opr );

printf("NOT_opr value = %d\n",NOT_opr );

printf("XOR_opr value = %d\n",XOR_opr );

printf("left_shift value = %d\n", m << 1);

printf("right_shift value = %d\n", m >> 1);

}


CONDITIONAL OR TERNARY OPERATORS IN C:

Conditional operators return one value if the condition is true and returns another value is the condition is false.


This operator is also called a ternary operator.Syntax     :        (Condition? true_value: false_value);Example :         (A > 100  ?  0  :  1);


In the above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if-else conditional statements.


#include <stdio.h>


int main()

{

int x=1, y ;

y = ( x ==1 ? 2 : 0 ) ;

printf("x value is %d\n", x);

printf("y value is %d", y);

}


C – Increment/decrement Operators


Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.


Syntax: Increment operator: ++var_name; (or) var_name++; Decrement operator: – -var_name; (or) var_name – -;


Example: Increment operator :  ++ i ;    i ++ ; Decrement operator :  – – i ;   i – – ;


EXAMPLE PROGRAM FOR INCREMENT OPERATORS IN C:

In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.

//Example for increment operators


#include <stdio.h>

int main()

{

int i=1;

while(i<10)

{

printf("%d ",i);

i++;

}

}

EXAMPLE PROGRAM FOR DECREMENT OPERATORS IN C:

In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator and output is displayed as “20 19 18 17 16 15 14 13 12 11”.


//Example for decrement operators


#include <stdio.h>

int main()

{

int i=20;

while(i>10)

{

printf("%d ",i);

i--;

}

}

EXAMPLE PROGRAM FOR PRE – INCREMENT OPERATORS IN C:

//Example for increment operators


#include <stdio.h>

int main()

{

int i=0;

while(++i < 5 )

{

printf("%d ",i);

}

return 0;

}


EXAMPLE PROGRAM FOR POST – INCREMENT OPERATORS IN C:


#include <stdio.h>

int main()

{

int i=0;

while(i++ < 5 )

{

printf("%d ",i);

}

return 0;

}

EXAMPLE PROGRAM FOR PRE – DECREMENT OPERATORS IN C:


#include <stdio.h>

int main()

{

int i=10;

while(--i > 5 )

{

printf("%d ",i);

}

return 0;

}


EXAMPLE PROGRAM FOR POST – DECREMENT OPERATORS IN C:


#include <stdio.h>

int main()

{

int i=10;

while(i-- > 5 )

{

printf("%d ",i);

}

return 0;

}


SPECIAL OPERATORS IN C:

Below are some of the special operators that the C programming language offers.

EXAMPLE PROGRAM FOR & AND * OPERATORS IN C:

In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get the value of the variable that the pointer is pointing to. Please refer C – pointer topic to know more about pointers.

#include <stdio.h>

int main()

{

int *ptr, q;

q = 50;

/* address of q is assigned to ptr */

ptr = &q;

/* display q's value using ptr variable */

printf("%d", *ptr);

return 0;

}


EXAMPLE PROGRAM FOR SIZEOF() OPERATOR IN C:

sizeof() operator is used to find the memory space allocated for each C data types.

#include <stdio.h>

#include <limits.h>


int main()

{

int a;

char b;

float c;

double d;

printf("Storage size for int data type:%d \n",sizeof(a));

printf("Storage size for char data type:%d \n",sizeof(b));

printf("Storage size for float data type:%d \n",sizeof(c));

printf("Storage size for double data type:%d\n",sizeof(d));

return 0;

}


Questions on Arithmetic Operators

1. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int i = -3;
       int k = i % 2;
       printf("%d\n", k);
   }

a) Compile time error b) -1 c) 1 d) Implementation defined

2. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int i = 3;
       int l = i / -2;
       int k = i % -2;
       printf("%d %d\n", l, k);
       return 0;
   }

a) Compile time error b) -1 1 c) 1 -1 d) Implementation defined

3. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int i = 5;
       i = i / 3;
       printf("%d\n", i);
       return 0;
   }

a) Implementation defined b) 1 c) 3 d) Compile time error

4. What will be the output of the following C code?

  #include <stdio.h>
   int main()
   {
       int i = -5;
       i = i / 3;
       printf("%d\n", i);
       return 0;
   }

a) Implementation defined b) -1 c) -3 d) Compile time error

5. What will be the final value of x in the following C code?

   #include <stdio.h>
   void main()
   {
       int x = 5 * 9 / 3 + 9;
   }

a) 3.75 b) Depends on compiler c) 24 d) 3

6. What will be the output of the following C code?

   #include <stdio.h>
   void main()
   {
       int x = 5.3 % 2;
       printf("Value of x is %d", x);
   }

a) Value of x is 2.3 b) Value of x is 1 c) Value of x is 0.3 d) Compile time error

7. What will be the output of the following C code?

   #include <stdio.h>
   void main()
   {
       int y = 3;
       int x = 5 % 2 * 3 / 2;
       printf("Value of x is %d", x);
   }

a) Value of x is 1 b) Value of x is 2 c) Value of x is 3 d) Compile time error


1. What will be the output of the following C code?

   #include <stdio.h>
   void main()
   {
       int a = 3;
       int b = ++a + a++ + --a;
       printf("Value of b is %d", b);
   }

a) Value of x is 12 b) Value of x is 13 c) Value of x is 10 d) Undefined behaviour

2. What is the precedence of arithmetic operators (from highest to lowest)? a) %, *, /, +, – b) %, +, /, *, – c) +, -, %, *, / d) %, +, -, *, /

3. Which of the following is not an arithmetic operation? a) a * = 10; b) a / = 10; c) a ! = 10; d) a % = 10;

4. Which of the following data type will throw an error on modulus operation(%)? a) char b) short c) int d) float

5. Which among the following are the fundamental arithmetic operators, i.e, performing the desired operation can be done using that operator only? a) +, – b) +, -, % c) +, -, *, / d) +, -, *, /, %

6. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int a = 10;
       double b = 5.6;
       int c;
       c = a + b;
       printf("%d", c);
   }

a) 15 b) 16 c) 15.6 d) 10

7. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int a = 10, b = 5, c = 5;
       int d;
       d = a == (b + c);
       printf("%d", d);
   }

a) Syntax error b) 1 c) 10 d) 5


1. What will be the output of the following C code?

   #include <stdio.h>
   void main()
   {
       int x = 1, y = 0, z = 5;
       int a = x && y || z++;
       printf("%d", z);
   }

a) 6 b) 5 c) 0 d) Varies

2. What will be the output of the following C code?

   #include <stdio.h>
   void main()
   {
       int x = 1, y = 0, z = 5;
       int a = x && y && z++;
       printf("%d", z);
   }

a) 6 b) 5 c) 0 d) Varies

3. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int x = 1, y = 0, z = 3;
       x > y ? printf("%d", z) : return z;
   }

a) 3 b) 1 c) Compile time error d) Run time error

4. What will be the output of the following C code?

   #include <stdio.h>
   void main()
   {
       int x = 1, z = 3;
       int y = x << 3;
       printf(" %d\n", y);
   }

a) -2147483648 b) -1 c) Run time error d) 8

5. What will be the output of the following C code?

   #include <stdio.h>
   void main()
   {
       int x = 0, y = 2, z = 3;
       int a = x & y | z;
       printf("%d", a);
   }

a) 3 b) 0 c) 2 d) Run time error

6. What will be the final value of j in the following C code?

   #include <stdio.h>
   int main()
   {
       int i = 0, j = 0;
       if (i && (j = i + 10))
           //do something
           ;
   }

a) 0 b) 10 c) Depends on the compiler d) Depends on language standard

7. What will be the final value of j in the following C code?

   #include <stdio.h>
   int main()
   {
       int i = 10, j = 0;
       if (i || (j = i + 10))
           //do something
           ;
   }

a) 0 b) 20 c) Compile time error d) Depends on language standard

8. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int i = 1;
       if (i++ && (i == 1))
           printf("Yes\n");
       else
           printf("No\n");
   }

a) Yes b) No c) Depends on the compiler d) Depends on the standard


1. Are logical operator sequence points? a) True b) False c) Depends on the compiler d) Depends on the standard

2. Do logical operators in the C language are evaluated with the short circuit? a) True b) False c) Depends on the compiler d) Depends on the standard

3. What is the result of logical or relational expression in C? a) True or False b) 0 or 1 c) 0 if an expression is false and any positive number if an expression is true d) None of the mentioned

4. What will be the final value of d in the following C code?

   #include <stdio.h>
   int main()
   {
       int a = 10, b = 5, c = 5;
       int d;
       d = b + c == a;
       printf("%d", d);
   }

a) Syntax error b) 1 c) 5 d) 10

5. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int a = 10, b = 5, c = 3;
       b != !a;
       c = !!a;
       printf("%d\t%d", b, c);
   }

a) 5 1 b) 0 3 c) 5 3 d) 1 1

6. Which among the following is NOT a logical or relational operator? a) != b) == c) || d) =

7. What will be the output of the following C code?

   #include <stdio.h>
   int main()
   {
       int a = 10;
       if (a == a--)
           printf("TRUE 1\t");
       a = 10;
       if (a == --a)
           printf("TRUE 2\t");
   }

a) TRUE 1 b) TRUE 2 c) TRUE 1  TRUE 2 d) Compiler Dependent

8. Relational operators cannot be used on ____________ a) structure b) long c) strings d) float


133 views2 comments

Recent Posts

See All

2 comentarios


Subrata Midya
Subrata Midya
12 feb 2020

Thanl you sir for this..😌😌

Me gusta

ROHAN DAS
ROHAN DAS
11 feb 2020

Got it sir

Me gusta
bottom of page