In C programming, a variable is a named storage location in memory that holds a value. Variables allow you to store, modify, and retrieve data throughout your program. Each variable has a unique name and a type, which determines the kind of data it can hold.
data_type variable_name;
• data_type: Specifies the type of data the variable can store (e.g., int, float, char).
• variable_name: The identifier for the variable.
int number; // Declares an integer variable named 'number'
float price; // Declares a floating-point variable named 'price'
char grade; // Declares a character variable named 'grade'
int count = 100;
// Declares and initializes an integer variable 'count' to 100
Variable names can include letters (both uppercase and lowercase), digits, and underscores.
They must start with a letter or an underscore.
C is case-sensitive. For example, myVar, MyVar, and myvar are considered different variables.
Variable names cannot be the same as C keywords (reserved words) such as int, float, char, return, etc.
While there is no strict limit, it's best to keep variable names reasonably short and descriptive. Some compilers may impose limits.
Variable names cannot contain spaces or special characters other than the underscore.
Avoid using names starting with double underscores (__), as they are typically reserved for system use.
int id;
float salary;
char _address;
int 123number; // Starts with a digit
float my-price; // Contains a hyphen
char return; // Conflicts with a keyword
int doubleValue; // Conflicts with a keyword
float my@var; // Contains an unsupported special character
Informing the compiler about the variable's existence and its data type. No memory is allocated at this stage.
data_type variable_name;
int age; // Declares a variable of type int
Allocating memory space for the variable and associating it with a specific memory address. Variables can be declared and defined simultaneously.
data_type variable_name = initial_value;
int age = 25; // Defines and initializes the variable 'age'
Assigning an initial value to the variable at the time of definition. If not initialized, variables may contain garbage values.
int age = 25; // Initializes 'age' with a value of 25
1. Local Variable: Declared inside a function or block. Accessible only within that function or block.
<#include <stdio.h>>
<int main() {>
int localVar = 5; // Local variable
printf("%d\n", localVar); // Use %d to print an integer
return 0;
<}>
2. Global Variable: Declared outside any function. Accessible from any function within the same file.
<#include <stdio.h>>
int globalVar = 50; // Global variable
void show() {
printf("Global variable: %d\n", globalVar);
}
int main() {
show();
return 0;
}
3. Static Variable: Retains its value between function calls. Declared with the static keyword.
<#include <stdio.h>>
void counter() {
static int count = 0; // Static variable
count++;
printf("Count: %d\\n", count);
}
int main() {
counter();
counter();
return 0;
}
4. Automatic Variable: Default type for variables declared inside a function or block. Declared with the auto keyword, but it's optional.
<#include <stdio.h>>
void function() {
int autoVar = 20; // Automatic variable, 'auto' is redundant here
printf("Automatic variable: %d\\n", autoVar);
}
int main() {
function(); // Call the function to see the output
return 0;
}
5. External Variable: Shared across multiple C source files using the extern keyword.
<#include <stdio.h>>
<#include "extern_var.h">
int sharedVar = 100; // Definition of external variable
void display() {
printf("Shared variable: %d\\n", sharedVar);
}
int main() {
display();
return 0;
}
Variables are fundamental in C programming for storing and manipulating data. They must be declared before use, and they can be initialized to a specific value. Understanding how to properly declare, define, and use variables is crucial for effective programming in C.