Variables and Constants
Data Element is a type of program element that can enable us to store data in memory.
In this lesson we are discussing about two such data elements that are variable and constant.
Variable is a memory location referring with an identifier whose value changes from time to time during program execution.
Declaration of Variables: Declaration associates a group of identifiers with a specific data type. We can understand declaration as an introduction of an identifier with its characteristics.
Example of Variable Declaration statements:
int a;
float r1,r2;
CONSTANT:
A constant is a memory location referred by an identifier whose value cannot be changed throughout the execution of a program.
Declaration of constant can be done with the following syntax:
const <datatype> <identifier>=’value’;
Again, the values that can be stored inside the memory location referring by a variable or constant also known as Constants.
Based on the four data types we have four types of constant values:
- Integer Constants Eg: 32727, 45, 8
- Floating Point Constants Eg: 2.58, 56.0
- Character Constants Eg: ‘A’, ‘c’, ‘u’
- String Constants Eg: “Rama”, “blue sea”, “ABC123”
Rules to form Integer and Floating Point Constants
- No comma or blank space is allowed in a constant.
- It can be preceded by – (minus) sign if desired.
- The value should lie within a minimum and maximum permissible range decided by the word size of the computer.
Symbolic Constant is a name associated with a sequence of characters or any basic type of constant. Symbolic constant is replaced with the constant at the time of compilation.
Symbolic Constants can be defined with the following preprocessor statement:
#define
Eg:
#define print printf
#define MAX 100
#define TRUE 1
#define FALSE 0
#define SIZE 10
INITIALISATION
Storing data into a variable can be done in two ways:
- Initialization
- Along with declaration
- Assignment statement
- User input
But, storing data into a constant can be done in two ways:
- Initialization along with declaration
- 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<stdio.h>
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:
- Along with declaration
- 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