Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain variable declaration and rules of variables in C language
In C programming, a variable is a named memory location used to store data values. Variables are fundamental building blocks that allow programs to manipulate and store information during execution.
What is a Variable?
It is the name for memory location that may be used to store a data value.
A variable may take different values at different times during execution.
A variable name may be chosen by the programmer in a meaningful way, so as to reflect its function or nature in the program.
For example: sum, avg, total, price, count etc.
Rules for Naming Variables
The rules for naming a variable are explained below −
They must begin with a letter (a-z or A-Z) or underscore (_).
Maximum length of variable is 31 characters in ANSI standard. But, first eight characters are significant by many compilers.
Upper and lowercase characters are different. For example:
total,TOTAL,Totalare 3 different variables.The variable name should not be a C keyword (like
int,float,if,whileetc.).White space is not allowed in variable names.
Only letters, digits (0-9), and underscore (_) are allowed after the first character.
Variable Declaration
Variable declaration tells the compiler about the variable name and its data type. It reserves memory space for the variable.
Syntax
datatype variable1, variable2, ..., variableN;
Where variable1, variable2, etc. are names of variables.
Examples:
int sum; float a, b; char grade; double salary, bonus;
Types of Variable Declaration
Variables can be declared in two ways −
Local declaration − Declaring a variable inside a function (like main). Its value is available only within that function.
Global declaration − Declaring a variable outside all functions. Its value is available throughout the entire program.
Example: Local and Global Variables
#include <stdio.h>
int globalVar = 100; /* global declaration */
int main() {
int localVar = 50; /* local declaration */
printf("Global variable: %d<br>", globalVar);
printf("Local variable: %d<br>", localVar);
return 0;
}
Global variable: 100 Local variable: 50
Example: Variable Declaration and Usage
Given below is a C program to find the profit or loss from cost price and selling price −
#include <stdio.h>
int main() {
float costPrice, sellingPrice, amount; /* variable declaration */
/* Input cost price and selling price */
costPrice = 240.0;
sellingPrice = 280.0;
printf("Cost Price: %.2f<br>", costPrice);
printf("Selling Price: %.2f<br>", sellingPrice);
if (sellingPrice > costPrice) {
amount = sellingPrice - costPrice;
printf("Profit Amount: %.2f<br>", amount);
}
else if (costPrice > sellingPrice) {
amount = costPrice - sellingPrice;
printf("Loss Amount: %.2f<br>", amount);
}
else {
printf("No Profit No Loss!<br>");
}
return 0;
}
Cost Price: 240.00 Selling Price: 280.00 Profit Amount: 40.00
Key Points
Always declare variables before using them in your program.
Choose meaningful variable names that reflect their purpose.
Local variables have precedence over global variables with the same name.
Uninitialized local variables contain garbage values.
Conclusion
Variable declaration is essential in C programming for storing and manipulating data. Following proper naming conventions and understanding variable scope helps write cleaner and more maintainable code.
