Difference Between Constants and Variables in C
Last Updated :
12 Jul, 2025
The constants and variables in C are both used to store data. So it is essential to know the difference between the variables and constants in C so that we can decide which one to use based on the situation.
In this article, we will discuss the basic difference between a constant and a variable in C language.
Variables in C
A variable in simple terms is a storage place that has some memory allocated to it. It is used to store some form of data and retrieve it when required. Different types of variables require different amounts of memory and have some specific set of operations that can be applied to them.
C Variable Declaration
type variable_name;
type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers, and the underscore '_' character. However, the name must not start with a number.
Example of C Variable
C
#include <stdio.h>
int main()
{
// declaration and definition of variable 'a123'
char a123 = 'a';
// This is also both declaration
// and definition as 'b' is allocated
// memory and assigned some garbage value.
float b;
// multiple declarations and definitions
int _c, _d45, e;
// Let us print a variable
printf("%c \n", a123);
return 0;
}
Constants in C
The constants are those variables or values in the C which cannot be modified once they are defined in the program.
- They have fixed values till the program's life.
- We can only assign value to the constant in the declaration.
- There can be any type of constant like integer, float, octal, hexadecimal, character constants, etc.
Example of C Constant
C
#include <stdio.h>
// Constants Macro
#define val 10
// Driver code
int main()
{
// constant variables
const float floatVal = 5.8;
const char charVal = 'a';
// printing constants
printf("Integer Constant: %d\n", val);
printf("Floating point Constant: %f\n", floatVal);
printf("Character Constant: %c\n", charVal);
return 0;
}
OutputInteger Constant: 10
Floating point Constant: 5.800000
Character Constant: a
Difference between Constant and Variables
The following table lists the differences between the constant and variables in C:
Constant
| Variables
|
---|
A constant is a variable or value that cannot be altered once defined. | A variable is a name associated with some memory location. |
A constant is used to hold the fixed values which we can retrieve later but cannot change. | A variable is used to hold some value that can be changed according to the requirement. |
The constants are generally stored in the text segment as they are read-only | The variables are stored inside a data segment, heap, or stack depending on the environment it is declared in. |
We can only assign a value to the constant while defining it. | We can assign value to the variable anytime. |
A constant can be defined by using #define or const keyword. | A variable can only be defined using the standard variable definition syntax. |
Example: #define pi 3.14
const int pi = 3.14;
|
Example: int var = 25;
var = 10;
|
Similar Reads
Difference between #define and const in C In C, both #define and const define constant values, but these constants differ greatly in their behaviors and implementation. #define is a preprocessor directive used to define constants that are replaced by their value during preprocessing, before actual compilation begins. while const defines a t
2 min read
Difference between Identifiers and Variables in C Perquisites: Identifiers, Variables Identifiers Identifiers are used for the naming of variables, functions, and arrays. It is a string of alphanumeric characters that begins with an alphabet, or an underscore( _ ) that are used for variables, functions, arrays, structures, unions, and so on. It is
2 min read
Different ways to declare variable as constant in C There are many different ways to make the variable as constant in C. Some of the popular ones are: Using const KeywordUsing MacrosUsing enum Keyword1. Using const KeywordThe const keyword specifies that a variable or object value is constant and can't be modified at the compilation time. Syntaxconst
2 min read
Difference between Keyword and Identifier in C In C, keywords and identifiers are basically the fundamental parts of the language used. Identifiers are the names that can be given to a variable, function or other entity while keywords are the reserved words that have predefined meaning in the language.The below table illustrates the primary diff
3 min read
Difference between Keyword and Identifier in C In C, keywords and identifiers are basically the fundamental parts of the language used. Identifiers are the names that can be given to a variable, function or other entity while keywords are the reserved words that have predefined meaning in the language.The below table illustrates the primary diff
3 min read
Static Variables in C In C programming, a static variable is declared using static keyword and have the property of retaining their value between multiple function calls. It is initialized only once and is not destroyed when the function returns a value. It extends the lifetime of the variable till the end of the program
4 min read