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:
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
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
Priority | Operators |
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
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
Priority | Operators |
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?
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:
- Integer
- Floating number
- Character