0% found this document useful (0 votes)
19 views5 pages

Chapter 2

The document discusses different types of instructions in C programming including type declaration, arithmetic, and control instructions. It provides examples of valid and invalid variable declarations and arithmetic expressions. It also covers operator precedence and associativity, relational operators, logical operators, and control flow statements.

Uploaded by

KESHAV SINGH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Chapter 2

The document discusses different types of instructions in C programming including type declaration, arithmetic, and control instructions. It provides examples of valid and invalid variable declarations and arithmetic expressions. It also covers operator precedence and associativity, relational operators, logical operators, and control flow statements.

Uploaded by

KESHAV SINGH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter - 2 ( Instructions and Operators)

Instructions - These are statements in a program.

Type :-
1. Type Declaration Instruction
2. Arithmetic Instruction
3. Control Instruction

Type Declaration Instructions --> Declare variable before using it.

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

a + b here a -> Operand 1


+ -> Operator
b -> Operand 2

Note - single variable on the LHS.

VALID
a = b + c
a = b * c
a = b / c

Invalid
b + c = a
a = bc
a = b^c

Note - pow(x,y) stands for 'x' to the power 'y'.

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 );
}

here output will be "1".

If we print b ^ c , then output will be "0".

( ^ ) is known as xor operator we will learn about it later.

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.

Note - Modular Operator do not works on float values (decimal).


If we run float values in the modular operator it will give error.
Type Conversion

int op int --------> int


int op float --------> float
float op float --------> float

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

Conversion - 1. Implicit - Compiler do it by itself.

2. Explicit - As a programmer we have to do it.

For converting decimal values to int values type: (int) 1.999 .


here compiler will convert it into integer forcefully.

For ex.- Convert 1.99999 to int values

#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.

Operator Precedence ( Priority Order )

* , / , % -----> + , - -----> =

Associativity ( for same precedence )

Calculate from left to right

Types of Control Instructions


Used to determine flow of program
a. Sequence Control
b. Decision Control
c. Loop Control
d. Case Control

Relational Operator

== check whether variables are equal or not


>, >=
<, <=
!= (not equal to)

Note : here 1 stands for true and 0 stands for false.

Logical Operators

&& AND ---> all statements should be true.


|| OR ---> atleast one statement should be true.
! NOT ---> used to flip the expression from true to false and vice-versa.

Precedence Order 1:45

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;
}

Q. Write a program to check if a number is divisible by 2 or not.

#include<stdio.h>
#include<math.h>

int main() {
int x;
printf("enter a number : ");
scanf("%d", &x);
printf("%d", x % 2 == 0);
return 0;
}

Write a program to check if a number is odd or even.

#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;
}

Q. Are the following valid or not?


a. int a = 8^8
b. int x; int y = x;
c. int x, y = x;
d. char stars = '**';

Q. Print 1(true) or 0(false) for the following statements:


a. if it's sunday & it's snowing -> true
b. if it's monday or it's raining -> true
c. if a number is greater than 9 & less than 100 -> true
( 2 digit number)

Q. Write a program to print the average of 3 numbers.

Q. Write a program to check if given character is digit or not.

Q. Write a program to print the smallest number.

You might also like