Understanding Variables and Constants in C Language

 If you’ve just started your C programming journey, you’ve probably seen a lot of terms being thrown around—variables, constants, keywords, and so on. Among these, variables and constants are the true heroes of your C code. Without them, your program would be like a story without characters.

 In this blog, we’ll break down what variables and constants are in C, why they matter, and how you can use them with real-life examples.

What is a Variable? 

 Think of a variable as a box in your computer’s memory that stores some information (like a number, character, or decimal value). The special thing about this box is that the value inside it can change during program execution.  

In simple words: A variable is a named location in memory that stores data, and its value can vary (change). 

Rules for Naming Variables 

 Before we see them in action, here are some rules: 

 1. Variable names must start with a letter or underscore (_). 

 2. They cannot contain spaces or special characters like @, $, %. 

 3. They are case-sensitive (age and Age are different). 

 4. Keywords like int, float, if, else cannot be used as variable names.

Examples of valid variable names: 

age, marks, student_name, _total 
 Invalid names: 
2value, marks%, float

Declaring Variables 

In C, you must tell the compiler the type of data your variable will store. Common data types:

 int → for integers (whole numbers like 10, -25) 

 float → for decimal numbers (like 3.14, -0.99) 

 char → for single characters (like 'A', 'z')  

double → for large decimal numbers

 Syntax: 

datatype variable_name;

 Example 1: Declaring and Using Variables 


#include <stdio.h>
int main() {
int age = 20; // integer variable
float pi = 3.14; // float variable
char grade = 'A'; // character variable
printf("Age: %d\n", age);
printf("Value of Pi: %.2f\n", pi);
printf("Grade: %c\n", grade);
return 0;
}



Output: 

 Age: 20 
 Value of Pi: 3.14 
 Grade: A 

Here, age, pi, and grade are variables. Notice how their values are stored and later printed. 


 What is a Constant? 

Now imagine a box that you cannot change once you’ve put something in it. That’s what a constant is. 
 A constant is a value that remains the same throughout the execution of the program.
 For example, the value of π (pi = 3.14159) never changes, so it’s better to store it as a constant. 


Declaring Constants in C 

There are two common ways to declare constants:
 1. Using const keyword const float PI = 3.14159; 
 2. Using #define preprocessor directive 
 #define PI 3.14159 
 Both mean that PI cannot be changed later in the program.

Example 2: Using Constants




#include <stdio.h>
#define MAX_STUDENTS 50 // constant using #define
int main() {
const float PI = 3.14159; // constant using const keyword
printf("Value of PI: %.2f\n", PI);
printf("Maximum Students allowed: %d\n", MAX_STUDENTS);
return 0;
}


 Output: 

Value of PI: 3.14 Maximum Students allowed: 50

 If you try to change the value of PI or MAX_STUDENTS, the compiler will throw an error because constants cannot be modified.


Difference Between Variables and Constants

Feature Variable Constant
Value Can change during program Fixed
Declaration int age = 20; const int age = 20;
or
#define AGE 20
Storage Stored in memory Stored in memory (const) or replaced during compilation (#define)

Example 3: Variables vs Constants in Action


#include <stdio.h>
#define TAX_RATE 0.05 // constant
int main() {
int price = 1000; // variable
float tax = price * TAX_RATE;
int total = price + tax;
printf("Price: %d\n", price);
printf("Tax: %.2f\n", tax);
printf("Total Price: %d\n", total);
// price = 1200; //
Allowed (variable can change)
// TAX_RATE = 0.1; //
Error (constant cannot be changed)
return 0;
}
***

Output: 

 Price: 1000 
 Tax: 50.00 
 Total Price: 1050 



Why Do We Need Constants?

To make programs more readable (PI is more meaningful than 3.14159).
 To prevent accidental changes to fixed values. 
 To make programs easier to update (change once, apply everywhere). 


 Final Thoughts 

Variables are like boxes where values can change.
 Constants are like locked boxes with fixed values. 
 Both are essential to make your C programs meaningful and safe.




As you continue learning C, always ask yourself: Should this value change during the program? If yes, use a variable. If no, make it a constant.

Previous Post Next Post