Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

Assignment Operators

The ‘equal to’ sign (=) is known as assignment operator in C language.  The program statement that has assignment operator is known as assignment statement.

We have already discussed about the syntax and examples of assignment statements in data types and variable declaration chapter of this course.

The assignment operator can be combined with arithmetic operators to create shorthand notations.

For example, a=a+2; can be written as a+=2;

In the above expression we want to add 2 to the existing value of ‘a’ and again want to store back into a.  In this case shorthand notations will help.

Like that, all the remaining arithmetic operators can be combined with assignment operator.

Type Casting

The value for the expression is promoted or demoted depending on the type of the variable on left hand side of = in the assignment statement.

For example, consider the following assignment statements:

int i;

float b;

i = 4.6;

b = 20;

In the first assignment statement, float (4.6) is demoted to int. Hence i gets the value 4. In the second statement int (20) is promoted to float, b gets 20.0.

If we have a complex expression like:

float a, b, c;

int s;

s = a * b / 5.0 * c;

Where some operands are integers and some are float, then int will be promoted or demoted depending on left hand side operator. In this case, demotion will take place since s is an integer.

 

This is also known as implicit type conversion or type casting.  There is a way to explicitly convert the data type of a value.

int x=7, y=5;

float z;

z=x/y; /*Here the value of z is 1*/z = (float)x/(float)y;   /*Here the value of z is 1.4*/

You cannot copy content of this page