Ads

Assignment Operator

ASSIGNMENT OPERATOR:
- The equal to symbol ‘=’ is an Assignment operator. Assignment operator assigns the value which is on its right to left.


Syntax:
<variable>=<value>; (or) <lvalue>=<rvalue>

- Angled brackets are used for demonstration purpose only and should not be used in your c program
- Thus we can use this assignment operator to assign a constant value to variable or to assign the value in one variable to other.

EX:

i) a=10/*Assigning a constant value 10 to variable a*/

ii) a=11; /*Assigning a value 11 to a */
    b=a; /* After assigning a value to a and then again assigning value in a to b*/
(After this operation value in b is 11 )
iii) i=10;           /*assigning a integer value to i */
     f=10.43;     /*assigning a float value to f */
     c=’A’;         /*assigning a character to variable c*/


- But remember before assigning a value to variable, the variable should be declared first and the variable data type should match the type of value you are storing in that variable, if not, wrong data is stored (type casting or data management ).


Program:


/* Write a program to assign a value to a variable and then assign the same value to another variable using the same variable.*/

#include <stdio.h>
int main()
{
 int a;
 int b;
 a=10;
 printf("\n The value in a is %d", a);
 b=a; /*Assigning the value in a to b */
 printf("\n After assigning value in a to b ",);
 printf("\n The value in b is %d", b);

 return 0;
}

Output:

The value in a is 10
After assigning value in a to b
The value in b is 10


Program:

/* Storing different type of values in respective types*/

#include<stdio.h>
int main()
{
 int i;
 float f;
 char c;
 i=10;
 f=10.43;
 c='A';
 printf("\n i= %d",i); 
 printf("\n f=%f",f);
 printf("\n c=%c",c);

 return 0;
}


Output:

i= 10
f=10.430000
c=A



WARNING: We can’t assign a value to constant like

5=6;

It is invalid in C programming language because 5 is constant not a memory location.
- We can reassign an assigned value as demonstrated in below example.

Program:


/*A program to assign a value first and then reassign the same variable and print the value*/ 

#include<stdio.h>
int main()
{
 int a;
 a=5;
 printf("\nValue in a is %d",a);
 a=10;
 printf("\nValue in a is %d", a);

 return 0; 
}


Output:

Value in a is 5
Value in a is 10


- When we do the above operation the previous value in memory is replaced with new value. We can also assign value obtain by an expression to a variable like
a=5*5*5;         It is valid. Now in a we have value 125.


- We can also assign a variable expression like

a=x+y+z;


- we can use these type of expressions when user is willing to give values for x, y, z during the program execution. As show in below program

Program:


/* Calculate the result of the expression x+y+z */

#include<stdio.h>
int main()
{
 int a, x ,y, z;
 printf ("\n Enter value of x:\t");
 scanf("%d",&x);
 printf ("\n Enter value of y:\t");
 scanf("%d",&y);
 printf("\n Enter value of z:\t");
 scanf("%d",&z);
 a=x+y+z;
 printf("\n After x+y+z the value is %d",a);

 return 0;
}


Output:

Enter value of x:   1
Enter value of y:   2
Enter value of z:   3
After x+y+z the value is 6


COMMON MISTAKES WHILE ASSIGNMENT:
TYPE CASTING IMPLICIT ASSIGNMENT:


Program:

/* Wrong data is stored due to Implicit Type cast*/

#include<stdio.h>
int main()
{
 int a;
 a=7.3;
 printf("\nx=%d",a);

 return 0;
}


Output:

x=7

- Here we have declared variable ‘a’ as a integer type and trying to store a variable of float type which is not possible; so a store’s 7 and truncates .3, so we get output as 7.




No comments:

Post a Comment