Variables and Constants in C

Share Your Love

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;
}

HT4f6e8hDp9HPHAL7pvaMN1UFdKuXj 83RPbM983hELufU3 YWol1WitDeOadUS2hLB0K3wr4 sKGavkk6go wD7BhkEU12e5oJ2u2FyC4voMLOWXfnsvF6bNSLf4GN2MCQkUg

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;
}

9GaS9sKJkdGu7xEKvFFvTDriS44uDXZLSbFn8 i8bOJSPxHHGNyMH9m2UcT3svB4BackwuyL0dN3Du9CV4iS04QY74qXr5hrJwQQSyHaNeTVNG221KY6AaGgba5zTYbv ozr Q

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.
Difference Between Variable and Constant

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Avatar photo
Soham Malakar

Hello my name is Soham Malakar am currently pursuing BCA

Coding is one of my favorite hobbies.
I love to code and an enthusiast to learn different types of programming languages.
I love to get intoxicated in the world of technology and video games.
A quick learner possessing better results.

Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter