Assignment Statement In C Programming Language

In the following, Counter is an INTEGER variable initialized to zero.

The meaning of the first assignment is computing the sum of the value in Counter and 1, and saves it back to Counter. Since Counter's current value is zero, Counter + 1 is 1+0 = 1 and hence 1 is saved into Counter. Therefore, the new value of Counter becomes 1 and its original value 0 disappears.

The second assignment statement computes the sum of Counter's current value and 3, and saves the result back to Counter. Thus, the new value of Counter is 1+3=4.

The following swaps the values in A and B, with the help of C. That is, after completing the following three assignment statements, A and B have 5 and 3, respectively.

Initially, A and B are initialized to 3 and 5, respectively, while C is uninitialized. The first assignment statement puts A's value into C, making A=3, B=5 and C=3.

The second assignment statements puts B's value into A. This destroys A's original value 3. After this, A = 5, B = 5 and C = 3.

The third assignment statement puts C's value into B. This makes A=5, B=3 and C=3. Therefore, the values in A and B are exchanged.

The following is another possible solution; but, it uses one more variable.

Assignment Statement

Once you've declared a variable you can use it, but not until it has been declared - attempts to use a variable that has not been defined will cause a compiler error. Using a variable means storing something in it. You can store a value in a variable using:

name = value;

For example:


stores the value 10 in the int variable a. What could be simpler? Not much, but it isn't actually very useful! Who wants to store a known value like 10 in a variable so you can use it later? It is 10, always was 10 and always will be 10. What makes variables useful is that you can use them to store the result of some arithmetic.

Consider four very simple mathematical operations: add, subtract, multiply and divide. Let us see how C would use these operations on two float variables a and b.









Note that we have used the following characters from C's character set:

+ for add

- for subtract

* for multiply

/ for divide

BE CAREFUL WITH ARITHMETIC!!! What is the answer to this simple calculation?


The answer depends upon how a was declared. If it was declared as type int the answer will be 3; if a is of type float then the answer will be 3.333. It is left as an exercise to the reader to find out the answer for a of type char.

Two points to note from the above calculation:

  1. C ignores fractions when doing integer division!
  2. when doing float calculations integers will be converted into float. We will see later how C handles type conversions.

0 Thoughts to “Assignment Statement In C Programming Language

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *