Variables:
A variable is a name of a location in a memory where values are stored. The value stored in the variable can be changed and can be reused as many times as possible depending on the instructions provided by the user.
Read More: Variables In C Programming Language
Constants:
A constant is a fixed value which once initialised or defined cannot be changed. It can be defined using const keyword or using #define keyword. There are different types of constants, let us discuss them briefly.
- Integer constant:
An integer constant holds numeric value without decimal values. For example: 1, 33, 108, 9999, etc.
- Float constant:
A float constant is one which also holds numeric value including decimal values. For example: 1.88, 99.56, 999.4456, etc.
- Character constant:
A character constant is one which holds only one character within single inverted commas. For example: ‘A’, ‘c’, ‘B’, etc.
- String constants:
A string constant is one which hold set of characters fenced within double inverted commas. For example: “abcd”, “C programming”, etc.
Let us see some examples on defining constants:
Using #define keyword:
#include<stdio.h> #include<conio.h> #define pi 3.14 int main() { int r; printf("Enter the radius of the circle"); scanf("%d",&r); int a=(pi*r*r); printf("\nThe area of the circle is=%d",a); return 0; }
Screenshot Dedicated to Soham Malakar
Using const keyword:
#include<stdio.h> #include<conio.h> int main() { const int i=10; const float g=99.236; const char c='A'; const char ss[100]="C Programing"; printf("Integer constant= %d",i); printf("\nfloat constant= %f",g); printf("\nCharacter constant= %c",c); printf("\nString constant= %s",ss); return 0; }
Screenshot Dedicated to Soham Malakar
Difference between variable and constant:
Variable | Constant |
Once declared can be changed in the program. | Once declared cannot be changed in the program. |
Variable can be declared using any name with the data type. | Constants are declared using const keyword and the data type. |
It is represented by int, float, char, double, string etc. | It is expressed by the #define or by the const keyword. |
For example: int name, float num, string s, etc. | For example: #define pi 3.17, const int num, const string s, etc. |
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.