Let’s take a simple program to explain variables & constants
printf("The number is 5 \n");
This program is not such a complex one, this is same as Hello World. But the difference is, it prints as ‘The number is 5’.
Now we need to print 10 instead of 5, not at compile time, it should be at run time. And we should not edit the code again and again to get the desired output.
Hope that now you understand the requirement, just think of it. Are you ready? can we achieve this? Yes sure. So, For that we are introducing an important concept in C Programming, Can you guess that? yep, that is Variable
Variable is a container which stores the data,Variable may have different DATA TYPES (int,char, float like that).
numberToPrint is the name of a variable
int is DATA TYPE of a variable
This is an integer variable as its data type is int, And as it is int variable, we can store integer values only.
Note : VARIABLE IS A NAME GIVEN TO THE MEMORY LOCATION WHERE THE ACTUAL DATA IS STORED.
Lets come back to our task, we need to change the value getting printed dynamically. We need to give input (a number that needs to be printed).
Here we add our variable into our program
int numberToPrint; /// Variable Declaration
printf("enter number to print \n ");
scanf("%d",&numberToPrint); /// getting input value at run time
printf("The number is %d \n,numberToPrint"); // prints output
From the above explanation , you can understand the usage of variable easily. Then Lets see how Variable works.
"VARIABLE IS A NAME GIVEN TO THE MEMORY LOCATION WHERE THE ACTUAL DATA IS STORED".
Can you understand the meaning of that Line above?
Now, we have declared a variable.
Compiler checks the data type of that variable and allocates memory depending on that data type.
Initially no value is assigned to that variable , so the garbage will persist in that memory location.
Now, we are assigning a value to that variable like below
a = 10;
instead of garbage value, 10 is stored in that memory location.
Again, i am going to change the value of that variable like below,
a = 5;
Now, 10 is replaced by 5, but the memory address is same.Hope you understand the definition of variable:
To print the address of a variable , you can use like this ‘&var_name’.
Now lets back to our task.Now you can understand the program and you can enter your desired number at run time using scanf() function. It gets input and assign it in address of that variable.
printf() function prints the number what we have entered.
Memory Space will be created as datatype says,
int 4 bytes
short 2 bytes
char 1 byte
float 4 bytes
double 8 bytes
bool 1 byte
lets consider variable declaration as below
int numberToPrint = 100;
Since its data type is int , it allocates 2 bytes in memory ,
- Variable name must begin with letter or underscore.
- Variables are case-sensitive
- They can be constructed with digits, letters.
- No special symbols are allowed other than underscore.
Lvalues and Rvalues in C:
There are two kinds of expressions in C:
lvalue : An expression that is a lvalue may appear as either the left-hand or right-hand side of an assignment.
rvalue : An expression that is a rvalue may appear on the right- but not left-hand side of an assignment.
Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side.
int var = 100;
The constant refer to a fixed value and the program can not alter it during its execution. These are fixed values and called as literals.
The constants are treated just like regular variables except that their values cannot be modified after their declaration.
Types of constants:
- integer constants.
- float constants.
- character constants.
- string constants.
const int a = 10;
In the above line, value of ‘a’ can not be changed during run time, since it is readonly.