Table of contents
1.
Introduction
2.
What is a Variable?
3.
Declaration of Variables
4.
Initialization of Variables
5.
Constants
6.
Examples
6.1.
Example 1: Swapping two variables
6.2.
Example 2: Using a constant
7.
Difference between Variables and Constants in C Program
8.
Frequently Asked Questions 
8.1.
Can a variable be declared without specifying its data type?
8.2.
Is it mandatory to initialize a variable when it is declared?
8.3.
Can a constant be declared without initialization?
9.
Conclusion
Last Updated: Dec 4, 2024
Easy

Difference between Variables and Constants in C

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In every programming language, variables and constants are like the building blocks that allow us to store and manipulate data. Just like in English, where we have nouns to name things, we use variables and constants to name and store data. Variables are used to hold values that can change during program execution, while constants represent fixed values that remain unchanged. Think of variables as sticky notes where you can write and rewrite information and constants as permanent markers on a board that can’t be changed once you have written something. It’s very important to understand the difference between variables and constants in C language. 

Difference between Variables and Constants in C

In this article, we will discuss what variables and constants are and how to declare and initialize them, with examples to understand their use.

What is a Variable?

A variable in C is a named storage location in the computer's memory that holds a value. It allows us to store data that can be modified and accessed throughout the program. You can think of a variable as a container that holds a specific type of data, such as an integer, floating-point number, character, or more complex data structure.

Variables are very important because they enable us to perform operations, make decisions, and keep track of data as the program runs. By assigning meaningful names to variables, we can write code that is easier to read and understand.

Declaration of Variables

Before you can use a variable in C, you need to declare it. Declaring a variable involves specifying its data type & giving it a name. The syntax for declaring a variable is:

data_type variable_name;


Let’s look at an example of declaring an integer variable named "count":

int count;


In this declaration, "int" is the data type, indicating that the variable "count" will store integer values.

You can also declare multiple variables of the same data type in a single line by separating them with commas:

int x, y, z;


This declares three integer variables: x, y, & z.


Note: To enhance code readability, it's important to choose meaningful names for your variables. Variable names should be descriptive and follow certain naming conventions, such as starting with a letter or underscore and consisting of letters, digits, or underscores.

Initialization of Variables

After declaring a variable, you can assign it an initial value. This process is called initialization. Initializing a variable means giving it a starting value at the time of its declaration or later in the program.

For example: 

int age = 25;


In this case, the integer variable "age" is declared and initialized with the value 25.

You can also initialize a variable separately from its declaration:

int score;
score = 100;


Here, the variable "score" is declared first and then initialized with the value 100 later in the program.

If you don't explicitly initialize a variable, it may contain a garbage value (an unpredictable or random value) until you assign it a value.

Note: It's always a good practice to initialize variables when you declare them to avoid using uninitialized variables, which can lead to unexpected behavior.

Constants

Constants, also known as literals, are fixed values that cannot be modified during program execution. They represent values that remain unchanged throughout the program.

In C, you can define constants using the "const" keyword followed by the data type and the constant name. For example:

const int MAX_VALUE = 100;


In this case, "MAX_VALUE" is a constant of type int with a fixed value of 100. Any attempt to modify the value of a constant will result in a compilation error.

Constants are useful when you have values that should not be altered accidentally. They also make the code more readable by providing meaningful names for fixed values.

C also provides enumeration constants, which allow you to define a set of named integer constants. 

For example:

enum DaysOfWeek {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};


In this enumeration, each constant (MONDAY, TUESDAY, etc.) is assigned a unique integer value starting from 0.

Examples

Let's look at a few more examples to understand the use of variables and constants in C:

Example 1: Swapping two variables

#include <stdio.h>
int main() {
    int a = 10;
    int b = 20;
    int temp;
    printf("Before swapping: a = %d, b = %d\n", a, b);
    // Swapping variables using a temporary variable
    temp = a;
    a = b;
    b = temp;
    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10


In this example, we declare three integer variables: `a`, `b`, and `temp`. We initialize `a` with 10 and `b` with 20. Then, we use the `temp` variable to swap the values of `a` and `b` by assigning `a` to `temp`, `b` to `a`, and finally `temp` to `b`.

Example 2: Using a constant

#include <stdio.h>
#define PI 3.14159

int main() {
    const float RADIUS = 5.0;
    float area;
    area = PI * RADIUS * RADIUS;

    printf("The area of the circle is: %.2f\n", area);


    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

The area of the circle is: 78.54


In this example, we define a constant `PI` using the `#define` preprocessor directive. We also declare a constant `RADIUS` using the `const` keyword. We calculate the area of a circle using the formula `PI * RADIUS * RADIUS` and store the result in the `area` variable. Finally, we print the calculated area.

Difference between Variables and Constants in C Program

VariablesConstants
Variables are named storage locations in memory that can hold values that may change during program execution.Constants are fixed values that cannot be modified once they are defined.
Variables are declared with a specific data type, such as int, float, char, etc., determining the kind of data they can store.Constants can be of any data type, but their values remain unchanged throughout the program.
Variables are declared using a data type followed by a variable name, such as int count;.Constants are defined using the const keyword followed by the data type and constant name, such as const int MAX_VALUE = 100;.
Variables can be initialized with a value during declaration or later in the program.Constants must be initialized at the time of declaration and cannot be assigned a new value later.
Variables can be assigned new values multiple times throughout the program using the assignment operator (=).Constants cannot be reassigned a new value once defined, as any modification attempt will cause a compilation error.
Variables are used when data needs to be stored and modified during the program's execution.Constants are used for values that should remain fixed, ensuring they are not accidentally modified.

Frequently Asked Questions 

Can a variable be declared without specifying its data type?

No, in C, you must specify the data type when declaring a variable.

Is it mandatory to initialize a variable when it is declared?

No, initializing a variable is optional, but it's good practice to do so.

Can a constant be declared without initialization?

No, constants must be initialized at the time of declaration in C.

Conclusion

In this article, we discussed the fundamental concepts of variables and constants in C. We learned how to declare and initialize variables, and we understood the role of constants in representing fixed values. 

You can also check out our other blogs on Code360.

Live masterclass