Chapter 2
Chapter 2
Type :-
1. Type Declaration Instruction
2. Arithmetic Instruction
3. Control Instruction
VALID
int a = 22;
int b = a;
int c = b + 1;
int d = 1 , e;
int a , b , c;
a = b = c = 1;
Here we have to declare the variable first before using it. Otherwise compiler will
give a error there.
For example -
INVALID
int a = 22;
int b = a;
int c = b + 2;
int d = 2, e;
int a,b,c = 1; here a,b,c is not defined earlier directly given value.
Arithmetic Instructions
VALID
a = b + c
a = b * c
a = b / c
Invalid
b + c = a
a = bc
a = b^c
For arithmetic operation we do need to add " #include<math.h> in our code. so that
it can operate easily. It has already some of the elements in it which can be of
our use.
for ex.-
#include<stdio.h>
#include<math.h>
int main() {
int b, c;
b = c = 1;
int a = b + c;
int power = pow( b , c );
printf("%d" , power );
}
Bitwise XOR ( ^ ) operator will take two equal lenght binary sequence and perform
bitwise XOR operation on each pair of a bit sequence. XOR operator will return 1,
if both bits are different. If bits are same, it will return 0.
for ex-
#include<stdio.h>
#include<math.h>
int main() {
int b, c;
b = c = 1;
int a = b + c;
int power = b^c;
printf("%d" , power );
return 0;
}
here output will be "0" , as value of b and c is same. If bits are same, it will
return 0. [ XOR Operator].
Modular Operator %
Returns remainder for int
3 % 2 = 1
-3 % 2 = -1
12 % 10 = 2
for ex-
#include<stdio.h>
#include<math.h>
int main() {
printf("%d" , 16%10);
return 0;
}
here output will be 6.
for ex-
#include<stdio.h>
#include<math.h>
int main() {
printf("%d \n" , 2/3);
return 0;
}
here output will be 0 as we have given command for int values so it will not take
float values.
ex-
#include<stdio.h>
#include<math.h>
int main() {
printf("%f \n , 2.0/3);
return 0;
}
here output will be 0.666667
#include<stdio.h>
#include<math.h>
int main() {
int a = (int) 1.99999;
pritnf("%d \n", a);
return 0;
}
here output will be 1 becuase compiler will not round off in arithmetic operation
in C.
* , / , % -----> + , - -----> =
Relational Operator
Logical Operators
Assignment Operator
=
+= here we can write a += b instead of a = a + b , as here both 'a' are same.
-=
*=
%=
for ex. -
#include<stdio.h>
#include<math.h>
int main() {
int a = 1;
int b = 4;
a -=b; //a = a + b
printf("%d \n", a);
return 0;
}
#include<stdio.h>
#include<math.h>
int main() {
int x;
printf("enter a number : ");
scanf("%d", &x);
printf("%d", x % 2 == 0);
return 0;
}
#include<stdio.h>'
#include<math.h>
int main() {
//even -> 1
//odd -> 0
int x;
printf("enter a number : ");
scanf("%d", &x);
printf("%d", x% 2 == 0);
return 0;
}