Friday, June 11, 2021

C instructions and Operators

 C instructions and Operators



Here we will discuss
  • Instructions
  • Operators
To write a program, we need instructions. C program is a set of instructions. Instructions help us to write a program correctly.
There are three types of instruction:
  1. Type declaration instruction
  2. Arithmetic instruction
  3. Control instruction

Type declaration instruction

Type declaration instruction states the type of the variable used in a program. We have to declare variables before we use them in our program. C program execution starts from main() function.
For example,
int is used to declare the integer function.
float and double are used to declare the decimal functions.
char is used to declare character and special symbols.

int a;

float b;

int a=15;

float b=2.5;

char messwithage;


Note: The program executes in a sequence or we say in the order in the C program. for example,
    int b=a+2;
    double a=5.66;
when we run this program it will throw an error that a is not defined.

Arithmetic instruction

Arithmetic instruction is an instruction that uses an arithmetic operator to connect constants and variables. 
+, -, *, /  are arithmetic operators

int a,b,c,d,total;

total= (a+b)*c/d

Here, +, -, *, and /are arithmetic operater.


% It is a modular division operator which gives us the remainder. This modular can't be applied on float. It gives us the same sign as of numerator. for example-
   int a = 6, b = -5,remainder;
   remainder = a/b;

This will give us an output of 1.

int a = -7, b = 5,remainder;

remainder = a/b;

 This will give us an output of -2.

Algebraic expression vs C expression

Algebraic

C

ab

2x3

(a+b)c

a*b

2*3

(a+b)*c


Notes C language doesn't apply BODMAS. It applies its own rule for calculation.
The hierarchy of operator or Operators precedence in C is as follows:

Priority

Operators

First

*, / and %

Second

+ and -

Third

=


for example 
a = 13*2/3*4+5-4/2+5*2;
Order of execution will be 
first *   a=26/3*4+5-4/2+5*2;
second / a=8*4+5-4/2+5*2;                 
third * a=32+5-4/2+5*2;
forth / a=32+5-2+5*2;
and so on

When the operator of the same priority is present then it will go with the sequence but if there will be bracket()  then it will go with bracket.

Conversion of integer and float

  1. When there will be int and int value it will give output in int.
  2. When there will be int and float/double value it will give output in float/double.
  3. When there will be float and float value it will give output in float.

Control instruction

Control instruction determines the order of instruction. It helps to execute a program in sequence.
There are four types of Control instructions:
  • Sequence control instruction
  • Decision control instruction
  • Repetition control instruction
  • Case-control instruction
Sequence control instruction help to execute the program in sequence. Repetition/Loop control instruction help to execute a statement or a group of statement repeatedly.

For more such article keep visiting our website messwithage

In the next article, we will discuss

  • If-else statement
  • Switch statement
  • Relational Operator
  • Logical operator

No comments:

Post a Comment

Please do not enter any spam link in the comment box

Best of Our Website

Popular Posts