hot post

Arithmetic instruction in c (part 2) | c programming

Arithmetic instruction in c 


In the c language learning series, we are have learned about what is operator.  And now we will know about the function or use of the operator in  Arithmetic instruction in c  which is a very important topic in c programming.

first, we start with the  topic  which is "function of the operator in Arithmetic instruction in c"

We learnt about the classification of operator in previous part so please also see previous part link is given below.
Arithmetic instruction in c (part 11)
 

Now, we study about unary operators in briefly:

Unary Operators Arithmetic instruction in c


What are unary operators


Unary operators are those operators which need only one operand to perform their function.


What is binary and ternary operators 

Those operators which need two operands to perform their function are known as binary operators and which need three operands to perform their function is known as ternary operators.


Example of unary operators
Example of unary operators

                                                         Example of unary operators


there are so many examples in the of unary operators in Arithmetic instruction in c. But I teach only most important unary operators such as '+', '-', '++', '--', 'sizeof()'. 

The function of a unary operator in Arithmetic instruction in c


A lot of students get confused to see '+', '-', sing, and say we need two operands to perform addition or subtraction. But guys, it is c programming, not math. The sings have a very different functions.so, don't get confused.
It is not addition or subtraction sing wheres it tell the nature of the number whether it is positive or negative. for example:
-6,-8,+9,+190

Use of '++', sing in Arithmetic instruction in c

Now we look at the sing which is '++' sing. Here, there are two + sing but it is a single operator is also know as increment operator.

There are two types of increment operators


  1.  post increment operator          (x++)
  2.  pre increment operator            (++x) 
Let see an example to understand better:

#include<stdio.h>
#include<conio.h>
void main()
{
int x=3;
clrscr();
x++;
printf("%d",x);

getch();
}

I make a program in which I make a variable and the value of the variable is 3, now we write x++ after that we print the value of x. Now you can see that in the program there are single operators '++' and single operands 'x'. Now let what happen in this code:
Here, the value of x increments from 1.
the work of '++', it using the 'x+1' that means the value of x which is 3 in this we add 1 and it becomes 4. When we run this code we get 4 on the output screen.
now let see the second example 


#include<stdio.h>
#include<conio.h>
void main()
{
int x=3;
clrscr();
x++;
printf("%d",x);
++x;
printf("%d",x);
getch();

}


Now we get 45 on the output screen.
++x and x++ bot have the same function but different priority in  Arithmetic instruction in c.
The priority of 'post' in c is low in category of operators and the The priority of 'pre' in c is high in category of operators 

Use of '--', sing in Arithmetic instruction in c


Now we look at the sing which is '--' sing. Here, there are two - sing but it is a single operator is also know as decrement operator.



There are two types of decrement operators



  1.  post decrement operator          (x--)
  2.  pre decrement operator            (--x) 
Let see an example to understand better:

#include<stdio.h>
#include<conio.h>
void main()
{
int x=3;
clrscr();
x--;
printf("%d",x);

getch();
}

I make a program in which I make a variable and the value of the variable is 3, now we write x-- after that we print the value of x. Now you can see that in the program there are single operators '--' and single operands 'x'. Now let what happen in this code:
Here, the value of x decrement from 1.
the work of '--', it asing the 'x-1' that means the value of x which is 3 in this we subtract 1 and it becomes 2. When we run this code we get 2 on the output screen.
now let see the second example 

#include<stdio.h>
#include<conio.h>
void main()
{
int x=3;
clrscr();
x--;
printf("%d",x);
--x;
printf("%d",x);
getch();

}

Now we get 21 on the output screen.
--x and x-- bot have the same function but different priority in  Arithmetic instruction in c.

 Now in the next part, we will study about sizeof();


 I am trying my best to explain the c language to you, please stay with me and share this post and if there is any mistake then you comment on comment box thanks for support
    


part 1(arithmetic-instruction-in-c.)

Post a Comment

0 Comments