In C programming, variables are the building blocks of data storage, allowing you to assign, modify, and retrieve data as your program executes. Simply put, a variable is a reserved space in memory that stores a value with a specific data type. Unlike constants, a variable’s value is not fixed; you can change it multiple times throughout the program, making it highly versatile.
When defining variables in C, understanding naming rules, syntax, and memory allocation is essential. Each variable has three primary stages: declaration, definition, and initialization. Missing any of these can lead to unwanted results, such as random or “junk” values if the variable is left uninitialized.
It can starts with underscore.
It can starts with alphabets.
It should start with numbers.
There should not be any whitespaces.
data_type variable_name = value; (defining single var)
or
data_type variable_name1, variable_name2; (defining multiple var)
int a;
char _c;
float f_data;
invalid variable names:
int auto
int !a
int 2hello
char @e;
It informs the compiler about the existence of variables and their respective data types.
data_type variable_name ; (defining single var)
Eg : int a;
char c
float f
data_type variable_name = value; (defining single var)
int a=10;
char c=’a’;
float f=12.22f;
data_type variable_name = value; ( defining single var)
int num=10;
Types of variables
#include
void myFunction() {
int a = 10;
printf(“%d”, a);
}
Example:
#include int a = 10; // Global variable void myFunction() { printf(“%d”, a); // Accessing global variable} int main() { myFunction(); return 0;}
Without Static:—————
#include
void myFunction() {
int count = 0; //( Static var)
count++;
printf(“%d\n”, count);
}
int main() {
myFunction(); // Output: 1
myFunction(); // Output: 2
myFunction(); // Output: 3
return 0;
}
Output: 1,1,1;
With static
————-
#include
void myFunction() {
static int count = 0;
count++;
printf(“%d\n”, count);
}
int main() {
myFunction(); // Output: 1
myFunction(); // Output: 2
myFunction(); // Output: 3
return 0;
}
Output :1,2, 3
Note: In C, automatic variables are the default type for local variables unless specified otherwise (e.g., with static or register).
Example:
#include void myFunction() { int x = 10; (Automatic variable) printf(“%d\n”, x);} int main() { myFunction(); // Output: 10 return 0;}
When you declare a variable with extern, it doesn’t allocate space for the variable;
it merely references a variable that is declared elsewhere (usually in another file or outside the current scope). This allows multiple files to share the same variable, which is often useful in large projects.
#include
int globalVar = 100; // Define global variable
int main() {
printf(“Global variable in file1.c: %d\n”, globalVar);
return 0;
}
#include
extern int globalVar = 100; // Define global variable
int main() {
printf(“Global variable in file2.c: %d\n”, globalVar);
return 0;
}
In C programming, the register keyword is used to suggest that a variable should be stored in a CPU register rather than in regular memory (RAM).
This is because CPU registers are faster to access than memory, and using registers for frequently accessed variables can improve the program’s performance.
However, modern compilers often optimize this automatically, and the register keyword may be ignored.
#include
int main() {
register int i; // declare register variable
for (i = 5; i < 50; i++) {
printf(“%d “, i);
}
return 0;
}
Indian Institute of Embedded Systems – IIES