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

Storing data into a variable can be done in two ways:

  1. Initialization
    1. Along with declaration
    2. Assignment statement
  2. User input

But, storing data into a constant can be done in two ways:

  1. Initialization along with declaration
  2. User Input

Using Assignment operation to initialise a constant leads to compile time error. 

Initialisation is the process of storing some value in the data element by the developer.  If the data element is left with out storing any value by using any one of the above methods then the system will store some unpredictable value in the referred memory location.

//code showing how uninitialised variable show unpredicatble results

#include

void main()

{

    int a,b;

    printf(“Value of a is %d”,a);

    printf(“Value of b is %d”,b);

    printf(“Sum of the numbers is %d”,a+b);

}

The above code could successfully compile but would result some unexpected result.  The result depends upon the system configuration, IDE and many other factors.

So, it is compulsory to have some value in the variable which was declared.  As we mentioned earlier, initialization is the process of storing value by the developer.  This can be done in two ways:

  1. Along with declaration
  2. Through assignment statement

A variable can be initialized to a value at the time of its declaration.  The following is an example line showing how a variable can be initialized along with declaration:

int a=10;

A variable can also be initialized to a value later to its declaration with the help of an assignment statement.

Example of initialization with assignment:

int a;

a=10;

At this moment, it would be appropriate to discuss about assignment operation here.  The general equal to operator ‘=’ is not equal to operator in C Language.  It is known as assignment operator.  Any program line or statement using ‘=’ operator is known as assignment statement.

Syntax of assignment statement:

Variable = value/variable/expression

As you can see in the above syntax, at the left hand side there must be a variable nothing else.  The variable at left hand side must have been declared prior to this statement but may or may not have initalised.  But on the other hand, at right hand side of the assignment statement we can see either a value or a variable which is already have value or an expression.

The following statements are the examples of 3 types of assignment statements we can see in C program:

a=10;    //variable = value

b=a;      //variable=variable already had value

c=a+b;  //variable =expression.  The result of the expression will be stored in the variable

 

You cannot copy content of this page