Instructions and Operators in C.

Instructions

Instructions are statements in a program.

Types of Instructions

1. Instruction for Declaration

Declare a variable before using it.

like:
int a = 33;
int b = a;
int c = b+1;
int d = 1, e;

int x, y, z; →→ Multiple variable declaration.
x = y = z = 1;

2. Arithmetic Instructions

a + b

a and b are Operand.
+ is an Operator.

a. Operator is a symbol that decides which type of operation is to be done.

operator can be (+, -, *, /, %)

NOTE - single variable on LHS.

Examples :

int sum = a + b;
int multiply = a * b;

NOTE - pow(a, b) for a to the power b.

int power = pow(b, c); // b to the power c

before using the power we need to use #include<math.h> which is a header file.

let's write a program to print the power.

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

int main()

{
int a,b;

int power = pow(a, b);
printf("%d", power);
return 0;
}

OutPut:

1

b. Modular Operator % or Modulo Operator

Returns remainder for int. (used to find the reminder)

this operator only works on integer types because integer types divided by integer types will get an integer reminder.

4%2→→2
7%3→→1

let's use the % operator in our program

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

int main()

{

int a, b;
a = 18;
b = 4;
printf("%d", a%b );
return 0;
}

OutPut:

2



Type Conversion

int operator int →→ int

int operator float →→ float

float operator float →→ float

NOTE - operator can be +, -, /, *, %.

in C conversion is of two types:

Implicit conversion is done by the compiler itself.

Explicit conversion is done by the user.

int can be converted to float or double but float and double can not be converted to int.

lets convert a = 2.333333 into int type.

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

int main()

{

int a = (int)2.333333;
printf("%d", a);
return 0;
}

OutPut:

2

Operator Precedence (priority of doing operations)

In c priority is given to *, /, %

2nd priority is given to +, -

and 3rd priority is given to the assignment operator (=).

let's take an example 

a = 8 - 10 * 12
a = 8 - 120
a = -112

let's check out the above example in the C program.

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

int main()

{

int a = 8-10*12;
printf("%d", a);
return 0;
}

Output :
-112

c. Associativity (Operations of the same precedence)

Perform operations from left to right if you same precedence.

a = 4 / 2 * 6 / 3
a = 2*6/3
a = 12/3
a = 4

let's check out the above example in the C program.

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

int main()

{

int a = 4/2*6/3;
printf("%d", a);
return 0;
}

Output : 

4

3. Control Instructions

Used to determine the flow of the program.

a. Sequence control

Executing the instructions one after another as they appear.

b. Decision control

c. Loop control

d. Case-control


Operators

1. Arithmetic operator →→ 

a + b

a and b are Operand.
is an Operator.

a. Operator is a symbol that decides which type of operation is to be done.

operator can be (+, -, *, /, %)

b. Modular Operator % or Modulo Operator

Returns remainder for int. (used to find the reminder)

this operator only works on integer types because integer type divided by integer type will get an integer reminder.

4%2→→2
7%3→→1

2. Relational operators

a. == (Equal to)

used to relate two operands

Example

a==b means we are questing whether a is equal to b or not.

let's try out this operator in our program

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

int main()

{

printf("%d", 2==2);
return 0;
}

Output: 1

in c, there is no true or false, there is 1 for true and 0 for false.

NOTE : any non-zero integer like -1, -2, -3, 1, 2, 3...  represents true.

b. >, >=

let's understand this with examples

1>2→0(False)

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

int main()

{

printf("%d", 1>2);
return 0;
}

Output: 0

2>1→1(True)

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

int main()

{

printf("%d", 2>1);
return 0;
}

Output: 1

2>2→0(False)

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

int main()

{

printf("%d", 2>2);
return 0;
}

Output: 0

2>=2→1(True)

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

int main()

{

printf("%d", 2>=2);
return 0;
}

Output: 1

c. <,<=

let's understand this with examples

1<2→1(True)

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

int main()

{

printf("%d", 1<2);
return 0;
}

Output: 1

2<1→0(False)

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

int main()

{

printf("%d", 2<1);
return 0;
}

Output: 0

2<2→0(False)

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

int main()

{

printf("%d", 2<2);
return 0;
}

Output: 0

2<=2→1(True)

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

int main()

{

printf("%d", 2<=2);
return 0;
}

Output: 1

d. != (not equal to)

It is the opposite of  == 

Example

1!=2→1(True)

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

int main()

{

printf("%d", 1!=2);
return 0;
}
Output: 1

2!=2→0(False)

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

int main()

{

printf("%d", 2!=2);
return 0;
}
Output: 0

3. Logical operator

a. && (AND)


The first and 2nd statements should be true then the output will be 1 (True) otherwise it will be 0 (False).

Example :

1<2→1(True) && 2<3→1(True) Output will be 1(True).

1<2→1(True) && 2>3→1(False) Output will be 0(False).

let's implement this in our program.

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

int main()

{

printf("%d\n", 1<2 && 2<3);
printf("%d\n", 1<2 && 2>3);
return 0;
}

Output: 1
              0

b. || (or) 

If one of the two or both statements is True, then the output will  be 1(True)

Example

1<2→1(True) || 2<3→1(True) Output will be 1(True).

1<2→1(True) || 2>3→1(False) Output will be 1(True).

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

int main()

{

printf("%d\n", 1<2 || 2<3);
printf("%d\n", 1<2 || 2>3);
return 0;
}

Output:
1

c. ! (NOT)

This operator reverses the result of any given expression.

let's understand with an example 

3<1 → 0 (false) but if we use not operator it will become true like this !(3<1) → 1 (true)

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

int main()

{

printf("%d\n",!(3<1));
printf("%d\n",!(3<1) && (1<2));
return 0;
}

Output :
1
1

Operator precedence

Priority  →→→→  Operator

1 →→→→→→→→ !

2 →→→→→→→→ *, /, %

3 →→→→→→→→ +, -

4 →→→→→→→→ <, <=, >, >=

5 →→→→→→→→ ==, !=

6 →→→→→→→→ &&

7 →→→→→→→→ ||

8 →→→→→→→→ =


4. Bitwise operator


5. Assignment operators

a. = (Assignment operators)

Shorthand operators

b. +=

Example :

a = a + b, here we used variable a on both sides of  the assignment operator, so in short, we can write a+=b. 

c. -=

Example :

d = d - b, here we used variable d on both sides of the assignment operator, so in short, we can write d-=b. 

d. *=

Example :

c = c * b, here we used variable c on both sides of the assignment operator, so in short, we can write c*=b. 

e. /=

Example :

e = e / b, here we used variable e on both sides of the assignment operator, so in short, we can write e/=b. 

f. %=

Example :

f = f % b, here we used variable f  on both sides of the assignment operator, so in short, we can write f%=b. 

Program for shorthand operators

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

int main()

{
int a=1, b=2, c=3, d=4, e=8, f=7;
    a += b; //a=a+b

printf("%d\n",a);
d -= b; //d=d-b
printf("%d\n", d);
c *= b; // c=c*b
printf("%d\n", c);
e /= b; //e=e/b
printf("%d\n", e);
f %= b; //f=f%b
printf("%d\n", f);
return 0;
}

Output :
3
2
6
4
1

6. Ternary operator

Ternary operator is used to perform the operation when the first condition is true and run second condition when the first condition is false.

Syntax :

condition ? runs code if TRUE : runs if FALSE;

Example :

#include<stdio.h>

int main()

{
int age;
printf("enter age\n");
scanf("%d", &age);
    age>=18 ? printf("eligible for vote\n") : printf("not eligible for vote");
return 0;
}

Comments

Popular posts from this blog

Write a program to check if a number is prime or not.

write a program (keep taking numbers as input from user until user enters an Odd number)

write a program to print all the odd numbers from 1 to 100.