Search This Blog

Chapter 2 Instructions and Operators:

 

Chapter 2: Instructions and Operators:

A C-program is a set of instructions. Just like a recipe - which contains instructions to prepare a particular dish.

Types of instructions:

1.Type declaration instruction

2. Arithmetic instruction

3.Control instruction

Type of declaration instruction:
int a;
float b;

other variations:

int i = 10; int j = i, int a = 2;
int j1 = a + j - i;

float b = a+3; float a = 1.1;       ==>Error! As we are trying to use a before defining it. 

int a,b,c,d;

a=b=c=d=30;                         ==> Value of a,b,c & d will be 30 each. 
Arithmetic Instructions

Note:

1.No operator is assumed to be present

    int i=ab  ( Invalid )

    int i=a*b  ( valid )

2.There is no operator to perform exponentiation in c however we can use pow(x,y) from <math.h>(More later).

Type conversion
An Arithmetic operation between

int and int     ==> int
int and float   ==> float
float and float ==> float

5/2 --> 2               5.0/2 --> 2.5 //IMPORTANT!!
2/5 --> 0               2.0/5 --> 0.4 //IMPORTANT!!

NOTE:
int a = 3.5; //In this case, 3.5 (float) will be denoted to a 3 (int) because a cannot store floats.

float a = 8; // a will store 8.0 [8-->8.0(Promotion to float)]

Quick Quiz:

Question- int k=3.0/9 value of k? and why?

Solution- 3.0/9=0.333, but since k is an int, it cannot store floats & value 0.33 is demoted to 0.

Operator Precedence in C

3*x-8y  is (3x)-(8y) or  3(x-8y)?

In the c language, simple mathematical rules like BODMAS no longer apply.

The answer to the above question is provided by operator precedence & associativity.

Operator precedence

The following table list the operator priority in C

PriorityOperators
1st  * / %
2nd+   -
3rd=

Operators of higher priority are evaluated first in the absence of parenthesis.

Note:

1.No operator is assumed to be present

    int i=ab  ( Invalid )

    int i=a*b  ( valid )

2.There is no operator to perform exponentiation in c however we can use pow(x,y) from <math.h>(More later).

Type conversion
An Arithmetic operation between

int and int     ==> int
int and float   ==> float
float and float ==> float

5/2 --> 2               5.0/2 --> 2.5 //IMPORTANT!!
2/5 --> 0               2.0/5 --> 0.4 //IMPORTANT!!

NOTE:
int a = 3.5; //In this case, 3.5 (float) will be denoted to a 3 (int) because a cannot store floats.

float a = 8; // a will store 8.0 [8-->8.0(Promotion to float)]

Quick Quiz:

Question- int k=3.0/9 value of k? and why?

Solution- 3.0/9=0.333, but since k is an int, it cannot store floats & value 0.33 is demoted to 0.

Operator Precedence in C

3*x-8y  is (3x)-(8y) or  3(x-8y)?

In the c language, simple mathematical rules like BODMAS no longer apply.

The answer to the above question is provided by operator precedence & associativity.

Operator precedence

The following table list the operator priority in C

PriorityOperators
1st  * / %
2nd+   -
3rd=

Operators of higher priority are evaluated first in the absence of parenthesis.




Chapter 2: Practice Set

Q1. Which of the following is invalid in c?

1. int a; b=a;

2. int v=3^3;

3. char dt= '21 Dec 2020' ;

Q2. What data type will 3.0/8 – 2 return?

Q3. Write a program to check whether a number is divisible  97 or not.

Q4. Explain step by step evaluation of 3*x/y-z +k

Where x=2, y=3, z=3 and k=1

Q5. 3.0+1 will be:

  1. Integer
  2. Floating number
  3. Character