C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

Operators in C: Assignment Operator


Hey, let us learn some theories... Do not worry. These are some simple things. Operators are the symbols like +, -, x, /, etc... Yeah! These are very easy...!

What is the use of ‘+’ in mathematics..? Yes, ‘+’ is using for addition. Like this, all operators are using for some specific operations. Like ‘+’, there are some other operators.



In C there are 8 types of operators:

1. Assignment Operator
2. Arithmetic Operators
3. Relational Operators
4. Logical Operators
5. Increment and Decrement Operators
6. Conditional Operator
7. Bit Vise Operators
8. Special Operators


Let us learn all operators as a little deep but not advanced

Assignment Operator:

You are already familiar with this operator. Assignment operator is using for assigning a value to a variable.

Example:
  
int a;
  a = 10;  

Here the value 10 is assigned to the variable ‘a’ using the operator ‘=’. So the memory location of ‘a’ gets the value 10.
You can assign value to variable during declaration:
Eg: int a = 10;
  • Question: Write a program to swap the value of given two numbers.?
 #include<stdio.h>
 #include<conio.h>
 void main()
  {
   int a,b,c;
   clrscr();
   printf("Enter a value for a");
   scanf("%d",&a);
   printf("\nEnter a value for b");
   scanf("%d",&b);
   c = a;
   a = b;
   b = c;
   printf("Swapped value of a is %d and b is %d:", a,b);  
   getch();
  }

Explanation:
The variables ‘a’ and ‘b’ get two different values from user.
(Suppose a = 10, b= 20. So current values are a = 10, b = 20)

c = a;
The variable ‘c’ gets the value of ‘a’.
(Now current values are: a = 10, b = 20, c = 10)

a = b;
Here the variable ‘a’ gets the value of ‘b’.
(Now the current values are: a = 20, b = 20, c = 10)

b = c;
Here the variable ‘b’ gets the value of ‘c’.
(Now the current values are: a = 20, b = 10, c = 10)

Now the process swapping completed. That is the value of ‘a’ was 10 and ‘b’ was 20, now the values of ‘a’ got 20 and ‘b’ got 10.

Sample Output:


Swapping two number in C program
  • Example: Program to accept a number and add 10 to the number, then display the updated answer.
 #include<stdio.h>
 #include<conio.h>
 void main()
  {
   int num,newnum;
   clrscr();
   printf("Enter a number");
   scanf("%d",&num);
   newnum = num + 10;
   printf("New number is %d :", newnum); 
   getch();
  }

Explanation:

scanf("%d",&num);
Entered number stored to the variable ‘num’.

newnum = num + 10;
The value of the variable 'newnum' gets the entered number + 10

Sample Output:


Add a number to the accepted number in C

Instead of this program, we can write like this with only one variable with same output.

 #include<stdio.h>
 #include<conio.h>
 void main()
  {
   int num;
   clrscr();
   printf("Enter a number");
   scanf("%d",&num);
   num = num + 10;
   printf("New number is %d :", num);  
   getch();
  }

Here Instead of:

newnum = num + 10;

we wrote:

num = num + 10;

How it works:

We know in C, all line of codes execute from right side. Here in the line “num = num + 10”,

First 10 will take as a number, and then add to the entered number (num), then store the added number to location of first entered number (location of num).

For example:
  • If we declared an integer with variable name ‘num’ (int num;), then compiler made a location to store integer value with name ‘num’. 
  • Then compiler accepts a number to the variable ‘num’ from user. (eg: 20). Therefore, the variable ‘num’ gets the value 20.
  • In line ‘num = num + 10’, from right to left, 10 is added (+) to the value of num (Here the value of num is 20). So the right side of = get the value 30 (20 + 10).
  • Next is the ‘=’ symbol, this is the assignment operator, so the value of right side (30) assigned the variable ‘num’, that is the memory location of ‘num’ gets a value 30. Now memory location of num contains the final answer.
▶ What will be output?
  int b = 40;
  b = b + 30;
  Printf(“%d”,b); 
70? Yeah, 70 is the correct answer, that is, a number 30 added to the value of ‘b’ (40) and saved to same location. So ‘b’ gets the value 70 (40+30).

What will be output?
  int c = 100;
  c = c + 50;
  Printf(“%d”,c); 
Answer is 150

▶ Question : Write a program to find sum of two number using 2 variables only.

 #include<stdio.h>
 #include<conio.h>
 void main()
  {
   int num1,num2;
   clrscr();
   printf("Enter two numbers :");
   scanf("%d %d",&num1, &num2);
   num1 = num1 + num2;
   printf("Sum  is: %d ", num1); 
   getch();
  }

Explanation:

scanf("%d %d",&num1, &num2);

Accept and store first number to num1 and second number to num2.

num1 = num1 + num2;

Execution works from right to left. That is the value of num2 added with value of num1 and answer is stored to the memory location of num1 using assignment operator. Therefore, memory location of num1 contains the final answer. So we print the value of num1 (now num1 contains new value)

Sample Output:


add two number in C program with two variables


▶ In above program, instead of num1 = num1 + num2; you can write like this as a short form:

         num1 + = 5 (it is equal to num1 = num1 + num2;)

 (NB: YOU CAN’T WRITE SHORT FORM, IF IT IS LIKE THIS: num1 = num2 + num3)

Normal Form  Short Form
a = a + b
 a + = b
c = c - d
c - = d
 x = x * (6+1)  
 x * = (6+1)
y = y / (3+b)
  y / = (3 + b)  
a = a % b
a % = b

If you can use these short form in your programming, then that is better. During interviews, many software companies give more priorities to programmers who are using short form like these.

(The symbol ‘%’ will learn on next lesson).


No comments:

Post a Comment