Back to Study material
PPS

UNIT-2Arithmetic expressions & Conditional Branching Q1) Explain operator precedence A1)

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

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

Example

Try the following example to understand operator precedence in C

#include <stdio.h>

 

main() {

 

   int a = 20;

   int b = 10;

   int c = 15;

   int d = 5;

   int e;

 

   e = (a + b) * c / d;      // ( 30 * 15 ) / 5

   printf("Value of (a + b) * c / d is : %d\n",  e );

 

   e = ((a + b) * c) / d;    // (30 * 15 ) / 5

   printf("Value of ((a + b) * c) / d is  : %d\n" ,  e );

 

   e = (a + b) * (c / d);   // (30) * (15/5)

   printf("Value of (a + b) * (c / d) is  : %d\n",  e );

 

   e = a + (b * c) / d;     //  20 + (150/5)

   printf("Value of a + (b * c) / d is  : %d\n" ,  e );

 

   return 0;

}

When you compile and execute the above program, it produces the following result

Value of (a + b) * c / d is : 90

Value of ((a + b) * c) / d is  : 90

Value of (a + b) * (c / d) is  : 90

Value of a + (b * c) / d is  : 50

 Q2) Explain arithmetic operator in detail with exampleA2)

Every expression in C language contains set of operators and operands. Operators are the special symbols which are used to indicate the type of operation whereas operands are the data member or variables operating as per operation specified by operator. One expression contains one or more set of operators and operands. So to avoid the ambiguity in execution of expression, C compiler fixed the precedence of operator. Depending on the operation, operators are classified into following category.

Arithmetic Operator: These are the operators which are useful in performing mathematical calculations. Following are the list of arithmetic operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.

Sr. No.

Operator

Description

Example

  1.  

+

Used to add two operands

Result of A+B is 15

2.      

-

Used to subtract two operands

Result of A-B is 5

3.      

*

Used to multiply two operands

Result of A*B is 50

4.      

/

Used to divide two operands

Result of A/B is 2

5.      

%

Find reminder of the division of two operand

Result of A%B is 0

 Example:

 #include<stdio.h>

 #include<conio.h>

 void main()

 {

  int a,b,c;

  clrscr();

  printf("Enter two numbers");

  scanf("%d %d",&a,&b);

  printf("\n Result of addition of  %d and  %d is %d",a,b,a+b);

  printf("\n Result of subtraction of %d and %d is %d",a,b,a-b);

  printf("\n Result of multiplication %d and %d is %d",a,b,a*b);

  printf("\n Result of division of %d and %d is %d",a,b,a/b);

  printf("\n Result of modulus of  %d and %d is %d",a,b,a%b);

  getch();

 }

 

 Output:

 Enter two numbers

 5

 3

 Result of addition of 5 and 3 is 8

 Result of subtraction of 5 and 3 is 2

 Result of multiplication of 5 and 3 is 15

 Result of division of 5 and 3 is 1

 Result of modulus of 5 and 3 is 2

 Q3) Explain relational operator in detail with exampleA3)

Relational Operators: Relational operator used to compare two operands. Relational operator produce result in terms of binary values. It returns 1 when the result is true and 0 when result is false. Following are the list of relational operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.

Sr. No.

Operator

Description

Example

1.

<

This is less than operator which is used to check whether the value of left operand is less than the value of right operand or not

Result of A<B is false

 

2.

>

This is greater than operator which is used to check whether the value of left operand is greater than the value of right operand or not

Result of A>B is true

3.

<=

This is less than or equal to operator

Result of A<=B is false

4.

>=

This is greater than or equal to operator

Result of A>=B is true

5.

==

This is equal to operator which is used to check value of both operands are equal or not

Result of A==B is false

6.

!=

This is not equal to operator which is used to check value of both operands are equal or not

Result of A!=B is true

Example:

 #include<stdio.h>

 #include<conio.h>

 void main()

 {

  int a,b,c;

  clrscr();

  printf("Enter two numbers");

  scanf("%d %d",&a,&b);

  printf("\n Result of less than operator of  %d and %d is %d",a,b,a<b);

  printf("\n Result of greater than operator of %d and %d is %d",a,b,a>b);

  printf("\n Result of leass than or equal to operator %d and %d is %d",a,b,a<=b);

 printf("\n Result of greater than or equal to operator of %d and %d is %d",               a,b,a>=b);

  printf("\n Result of double equal to operator of  %d and %d is %d",a,b,a==b);

  printf("\n Result of not equal to operator of  %d and %d is %d",a,b,a!=b);

  getch();

 }

Output:

 Enter two numbers

 5

 3

 Result of less than operator of 5 and 3 is 0

 Result of greater than operator of 5 and 3 is 1

 Result of less than or equal to operator of 5 and 3 is 0

 Result of greater than or equal to operator of 5 and 3 is 1

 Result of double equal to operator of 5 and 3 is 0

 Result of not equal to operator of 5 and 3 is 1

 Q4) Explain Mixed operands in detail with examplesA4)

In a statement or expression if one the operand is real (float) and another one is integer then expression is called as Mixed Mode Arithmetic Expression. If in an expression either operand is of real then output is always in real format. If both operands are real then output will be in real formats.

Example

Real operand and Integer operand = Real operand (Output)
Real operand and Real operand = Real operand (Output).

Types of Arithmetic Operators used in Mixed Mode Expression

"*" - Multiply use for Multiplication
"/"  - Divide use for division
"+" - Plus use for addition
"-" - Minus use for subtraction
"%" - Modulus use for getting a reminder
"**" - Square  use for getting square number

Rules for Evaluation Mixed Mode Arithmetic Expression

Rule 1

Evaluate Expressions always from Left to Right

For Example: 3 + 5 - 4 = 4

Rule 2
Priority of an operator is also considered while calculating an expression

Operator Priorities in descending order


1. (**) Known as Square operator which executes From Right To Left .
   
For example 5**2 is equal to 25

2. (*) Known as multiplication operator which executes From Left to Right
   
For example 5 * 2 is equal to 10

3. (/) Known as Division operator which executes From Left to Right
   
For example 6 / 2 is equal to 3

4. (+) Known as Plus or Addition operator which executes From Left to Right
   
For example 6 + 2 is equal to 8

5. (-) Known as Minus or Subtraction operator which executes From Left to Right
   
For example 6 - 2 is equal to 4

Mixed mode operator example
 
2 + 2 * 5 ** 3

==> 2 + 2 * (5**3)
==> 2 + 2 * 125
==> 2 + (2 * 125)
==> 2 + 250 = 252

Rule 3

If the operands of this operator are of the same type means either Integer or Real, compute the same for result in that operator only.

For Example

Both Real Numbers

double a = 2.0;
double b = 3.0;

Double add = 2.0 + 3.0 = 5.0 (Result in Real Number only)

Both Integer Number

int a = 2;
int b = 3;
int add = 2 + 3 = 5 (Result in Integer Number only)

Rule 4

If one of the operator is Integer and other one is real then convert integer number to real number and compute the result in real number only.

For Example

double a = 5.0;

int b = 5;

Result = 5.0 = 5 = 10.0 (Result in Real Number only).

 Q5) Explain Implicit type conversion in detail with exampleA5)

Also known as ‘automatic type conversion’.

  • Done by the compiler on its own, without any external trigger from the user.
  • Generally, takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid to loose data.
  • All the data types of the variables are upgraded to the data type of the variable with largest data type.

 

bool -> char -> short int -> int ->

       unsigned int -> long -> unsigned ->

       long long -> float -> double -> long double

 

It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long is implicitly converted to float).

 

Example of Type Implicit Conversion:

 

// An example of implicit conversion

#include<stdio.h>

int main()

{

    int x = 10;    // integer x

    char y = 'a';  // character c

  

    // y implicitly converted to int. ASCII 

    // value of 'a' is 97

    x = x + y;

     

    // x is implicitly converted to float

    float z = x + 1.0;

  

    printf("x = %d, z = %f", x, z);

    return 0;

}

 

Output:

 

x=107, z = 108.000000

 Q6) Explain Explicit Type Conversion with exampleA6)

This process is also called type casting and it is user defined. Here the user can type cast the result to make it of a particular data type.

The syntax in C:

(type) expression

Type indicated the data type to which the result is converted.

 

// C program to demonstrate explicit type casting

#include<stdio.h>

  

int main()

{

    double x = 1.2;

  

    // Explicit conversion from double to int

    int sum = (int)x + 1;

  

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

  

    return 0;

}

 

Output:

 

Sum =2

 

Advantages of type conversion:

 

This is done to take advantage of certain features of type hierarchies or type representations.

 

It helps us to compute the expression containing variables of different data types.

 

 

 Q7) Explain logical operators with exampleA7)

Logical Operators: Logical operators are used to compare logical values of two operands. Following are the list of logical operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.

Sr. No.

Operator

Description

Example

1.

&&

This is logical AND, it returns true when both the values are non zero

Result of (A&&B) is true

2.

||

This is logical OR, it returns true when any of two value is non zero

Result of (A||B) is true

3.

!

This is logical NOT operator, it is used to reverse the logical state of operand 

Result of !(A&&B) is false

Example:

#include<stdio.h>

#include<conio.h>

void main()

{

 int a,b,c;

 clrscr();

 printf("Enter two numbers");

 scanf("%d %d",&a,&b);

 printf("\n Result of logical and operator of  %d and  %d is %d",a,b,a&&b);

 printf("\n Result of logical or  operator of %d and %d is %d",a,b,a||b);

 printf("\n Result of logical not operator of %d and and %d is %d",a,b,!(a&&b));

 getch();

}

Output:

Enter two numbers

5

3

Result of logical and operator of 5 and 3 is 1

Result of logical or operator of 5 and 3 is 1

Result of logical not operator of 5 and 3 is 0

 Q8) Explain Bit operations with exampleA8)

These operators are used to work on the each bits of data. These operators can work on the binary data. If the data is non binary then computer first of all convert it into binary form and then perform the operation. Following are the list of logical operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5. So the binary values of

A = 0000 1010 and

B = 0000 0101

Sr. No.

Operator

Description

Example

1.

&

This is bitwise AND

Result of (A&B) is 0000 0000

2.

|

This is bitwise OR

Result of (A|B) is  0000 1111

3.

^

This is bitwise Exclusive

Result of (A^B) is 0000 1111

4.

<<

This is used to shift bit on left side

Result of (A<<B) is

5.

>>

This is used to shift bit on right side

Result of (A>>B) is

Example:

#include<stdio.h>

#include<conio.h>

void main()

{

 int a,b,c;

 clrscr();

 printf("Enter two numbers");

 scanf("%d %d",&a,&b);

 printf("\n Result of bitwise and operator of  %d and  %d is %d",a,b,a&b);

 printf("\n Result of bitwise or  operator of %d and %d is %d",a,b,a|b);

 printf("\n Result of bitwise exclusive operator of %d and %d is %d",a,b,a^b);

 printf("\n Result of bitwise left shift operator of %d and %d is %d",a,b,a<<b);

 printf("\n Result of bitwise right shift  operator of %d and %d is %d",a,b,a>>b);

 getch();

}

Output:

Enter two numbers

5

3

Result of bitwise and operator of 5 and 3 is 1

Result of bitwise or operator of 5 and 3 is 7

Result of bitwise exclusive operator of 5 and 3 is 6

Result of bitwise left shift operator of 5 and 3 is 40

Result of bitwise right shift operator of 5 and 3 is 0

 Q9) Explain assignment operator with exampleA9)

The following table lists the assignment operators supported by the C language

Operator

Description

Example

=

Simple assignment operator. Assigns values from right side operands to left side operand

C = A + B will assign the value of A + B to C

+=

Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.

C += A is equivalent to C = C + A

-=

Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.

C -= A is equivalent to C = C - A

*=

Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.

C %= A is equivalent to C = C % A

<<=

Left shift AND assignment operator.

C <<= 2 is same as C = C << 2

>>=

Right shift AND assignment operator.

C >>= 2 is same as C = C >> 2

&=

Bitwise AND assignment operator.

C &= 2 is same as C = C & 2

^=

Bitwise exclusive OR and assignment operator.

C ^= 2 is same as C = C ^ 2

|=

Bitwise inclusive OR and assignment operator.

C |= 2 is same as C = C | 2

Example

Try the following example to understand all the assignment operators available in C –

#include <stdio.h>

main() {

 

   int a = 21;

   int c ;

 

   c =  a;

   printf("Line 1 - =  Operator Example, Value of c = %d\n", c );

   c +=  a;

   printf("Line 2 - += Operator Example, Value of c = %d\n", c );

 

   c -=  a;

   printf("Line 3 - -= Operator Example, Value of c = %d\n", c );

 

   c *=  a;

   printf("Line 4 - *= Operator Example, Value of c = %d\n", c );

 

   c /=  a;

   printf("Line 5 - /= Operator Example, Value of c = %d\n", c );

 

   c  = 200;

   c %=  a;

   printf("Line 6 - %= Operator Example, Value of c = %d\n", c );

 

   c <<=  2;

   printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );

 

   c >>=  2;

   printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );

 

   c &=  2;

   printf("Line 9 - &= Operator Example, Value of c = %d\n", c );

 

   c ^=  2;

   printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );

 

   c |=  2;

   printf("Line 11 - |= Operator Example, Value of c = %d\n", c );

}

When you compile and execute the above program, it produces the following result

Line 1 - =  Operator Example, Value of c = 21

Line 2 - += Operator Example, Value of c = 42

Line 3 - -= Operator Example, Value of c = 21

Line 4 - *= Operator Example, Value of c = 441

Line 5 - /= Operator Example, Value of c = 21

Line 6 - %= Operator Example, Value of c = 11

Line 7 - <<= Operator Example, Value of c = 44

Line 8 - >>= Operator Example, Value of c = 11

Line 9 - &= Operator Example, Value of c = 2

Line 10 - ^= Operator Example, Value of c = 0

Line 11 - |= Operator Example, Value of c = 2

 

 Q10) Program to relate two integers using =, > or < symbolA10)

#include <stdio.h>

int main() {

    int number1, number2;

    printf("Enter two integers: ");

    scanf("%d %d", &number1, &number2);

 

    //checks if the two integers are equal.

    if(number1 == number2) {

        printf("Result: %d = %d",number1,number2);

    }

 

    //checks if number1 is greater than number2.

    else if (number1 > number2) {

        printf("Result: %d > %d", number1, number2);

    }

 

    //checks if both test expressions are false

    else {

        printf("Result: %d < %d",number1, number2);

    }

 

    return 0;

}