The Wayback Machine - https://web.archive.org/web/20240619181647/https://www.geeksforgeeks.org/constants-vs-variables-in-c-language/
Open In App

Difference Between Constants and Variables in C

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Output:

a

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


Output

Integer 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

What are the differences between PHP constants and variables ?
PHP Constants: PHP Constants are the identifiers that remain the same. Usually, it does not change during the execution of the script. They are case-sensitive. By default, constant identifiers are always uppercase. Usually, a Constant name starts with an underscore or a letter which is followed by a number of letters and numbers. They are no need t
2 min read
Swift - Constants, Variables & Print function
Constants and variables are those elements in a code linked with a particular type of value such as a number, a character, a string, etc. Constants once declared in a code cannot be changed. In comparison, variables can change at any time in a code within the scope. Declaring Constants &amp; Variables in Swift: Constants and variables in Swift must
2 min read
Difference between Static variables and Register variables in C
static variables Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope. Syntax: static data_type var_name = var_value; register variables Registers are faster than memory to access
2 min read
Data type of character constants in C and C++
In C, data type of character constants is int, but in C++, data type of same is char. If we save below program as test.c then we get 4 as output (assuming size of integer is 4 bytes) and if we save the same program as test.cpp then we get 1(assuming size of char is 1 byte) C/C++ Code // C++ program demonstrating that data type of character // const
1 min read
Constants in C
The constants in C are the read-only variables whose values cannot be modified once they are declared in the C program. The type of constant can be an integer constant, a floating pointer constant, a string constant, or a character constant. In C language, the const keyword is used to define the constants. In this article, we will discuss about the
6 min read
Difference between static and non-static variables in Java
There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is declared as static, then a single copy of the vari
4 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 also known as the user-defined word. Identifier na
2 min read
Differences between undeclared and undefined variables in JavaScript
This article will give the idea of undefined &amp; undeclared variables in Javascript, also discussing their differences &amp; understanding the implementation through the examples. In a programming language, the variables are used to store some kind of information &amp; every programming language contains different kinds of procedures and techniqu
4 min read
Problem Solving on Storage Classes and Scoping of Variables
Storage class of variables includes the scope, visibility and life-time which help to trace the existence of a particular variable during the runtime of a program. There exist four types of storage classes in C: auto, register, static and extern. Auto and Register Storage Classes - The scope of variable declared using auto or register storage class
8 min read
Initialization of global and static variables in C
Predict the output of following C programs. // PROGRAM 1 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(void) { static int *p = (int*)malloc(sizeof(p)); *p = 10; printf(&quot;%d&quot;, *p); } // PROGRAM 2 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int *p = (int*)malloc(sizeof(p)); int main(void) { *p = 10; printf(&quot;%d&quot;
1 min read
Practice Tags :