Chapter 2: Instruction and operators


                         Instruction and Operators 


A  c program is a set  of instructions. just like a recepie-Which contains instructions to prepare a particular Dish.

Types of instruction

 1.Type declaration Instruction

2. Arithmetic Instruction

3. Control Instruction

Type declaration Instruction

int a;

float b;

Other varities:

int i=10; int j=i ; int a=2

int ji=a+j-i ;

float b=a+3 ; float =1.1***ERROR ! as we are trying to use before defining it ***

int abcd ; 

a=b=c=d=30 ;*** value of abcd will be 30 each***


Airtmetic Instruction 

int i=(3*2) +1

"*" "+"             **** these are operators***

"3,2,1"               ***these are operands***

operands can be int /float etc.

+,-,*,/ are arthmetic operators

int b=2, c=3;

intz; Z=b*c;              ***legal***

intZ; b*c=z;              ***Illegeal***



% --->Modular division operator 

%---.> Returns the reminder 

%---> Cannot be applied on float

%---> Sign is same as of numerator

5%2=1    -5%2=-1


NOTE:-

No operators is assumed to be present


int i =ab --->Invalid

int i =a*b ---> Valid

There is no operator to perform exponentiation in c

However we can use pow(x,y) from<math.h>

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

2/5 --->0    2.0/5--->0.4

Note 

int =3.5***In this case 3.5 (float) will be demoted to 3(int) because a is not able to float***

float a=8; *** a will store 8.0 8->8.0 (promotion to float)

operator precedence:-

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

In c language simple mathematical rules is BODMAS, no longer applies

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

operator precedence:- The following table lets the operator priority in c

Priority          operators

1                        */ %

2                        + -

3                         =

operator of higher priority are evaluated first in the absence of parenthesis 

Operators Associativity:- when operators of equal priority are present in an expression, the tie is taken care of by associativity 

x*y/z--->(x*y)/z

x/y*z---> (x/y)*z

*,/ follow left to right associativity

Control Instructions:-

determines the flow of control in a program four types of control instructions in C are :-


1. Sequence Control Instruction 

2. Decision Control Instruction 

3. Loop Control instruction 

4. Case Control Instruction 



You can also learn c programming from youtube :

Tutorial of c programming



Comments

Popular posts from this blog

Privacy Policy