Check The Programming Section
variable_name = expression;
In C/C++, the assignment operator can be used as part of any valid expression. In the above general form, an expression can be simple as single constnat value or as complex contains anything such as, function call, an arithmetic expression or a relation and logical expression.
The left part of the assignment operator must be a variable or a pointer. But it should not contain a function or a constant. The left part also called a target and identifies as l-value of the assignment operator. The right part (expressions) of the assignment operator refers as r-value and the r-value means the value of the expression.
If type of the r-value and l-value did not match, then r-value is converted as the type of and then assigned to it. This type of conversion may leads to a possible information loss. The type conversion is explained with an example:
int x;
char ch;
float f;
Consider the following type converstion within the varibales declared above using an assignment operator
An integer is assigned to a character
ch = x;
Here, type of l-value and r-value did not match and the type of x is integer (requires 4 byte). The type of ch is char (requires 1 byte), so lower 8-bits of x are assigned to ch and remaining higher bits of x are truncated. Consider the example below:
x = 67;
ch = x;
x = 265;
ch = x;
The memory requirement of char is 1 byte = 8 bits and maximum it can store a value which requires 8 bits. If any number is assigned to a char type which requires more than 8 bits to represent in memory then type conversion happens. For an 8-bits character, it can store a maximum value in between 0 to (28-1 =) 255. So for the statement ch = x; (the value of x is 67), no type conversion occurs and no information will be loss. But for the last assignment statement ch = x; (the value of x is 265) a value larger than 255 is assigned and there is a possible information loss due to type conversion.
A float is assigned to integer
f = 3.5;
x = f;
For the above assignment statement, the non-fractional part of f (is 3) is assigned to x. Because the target type is int, can only have decimal values. So, possible information loss.
A float is assigned to char
f = 4.5;
ch = f;
When a float is assigned to char, first the float value is converted to an integer value and then lower bits information of integer is assigned to ch. So, in this type of converstion also possible information loss.
An integer is assigned to float
x = 7;
f = x;
When an integer is assigned to float, simply an integer value is converted to a floating-point format. No information loss.
Consdier the Table below, which highlights the possible type conversions between various data type with information loss.
When converting from integers to characters and long integers to integers, the appropriate amount of highers order-bits will be removed.
The conversion between int to float, float to double, and double to long double does not add any precision or accuracy.
This type of conversion only change the form in which data is represented.
Do not use character variables to represent short int, int or signed chars when needed to avoid possible portability problems.
Some compiler always treat an character as positive value, when converting it to an int or float.
In C/C++, we can use multiple assignments in a single statement to assign one value to multiple variables. Consider the following example:
#include<cstdio>
using namespace std;
int main(){
int a,b,c;
a = b = c = 90;
printf("%d %d %d",a,b,c);
}
Note: Multiple assignments can not be used in a declaration statement to initialise the variables. Consider the following example:
int a = b = c = 90; //compile time error