Variable in C
In C programming language, a variable is a named storage location that can hold a value. It represents a memory location where data is stored, and its value can change during program execution.
Rules for naming variables in C:
- Variable names can consist of letters (both uppercase and lowercase), digits, and underscores.
- The first character of a variable name must be a letter or an underscore.
- Variable names are case-sensitive. For example, "myVariable" and "myvariable" are considered different variables.
- C keywords (reserved words) cannot be used as variable names. For example, you cannot name a variable "int" or "for" because these words have special meanings in the C language.
Here's an example of variable declaration and initialization in C:
#include
int main() {
int age; // Declaration of an integer variable
float salary = 2500.75; // Declaration and initialization of a float variable
age = 30; // Assigning a value to the variable
printf("Age: %d\n", age); // Printing the value of the variable
printf("Salary: %.2f\n", salary);
return 0;
}
Types of Variable in C
In C programming, variables can be categorized into various types based on their scope and lifetime. The common types of variables in C include:
- Local Variables: Local variables are declared within a block, such as a function or a compound statement, and their scope is limited to that block. They are created when the block is entered and destroyed when the block is exited. Local variables are not accessible outside the block in which they are declared.
#include
void exampleFunction() {
int localVar = 10; // Local variable
printf("Local variable: %d\n", localVar);
}
int main() {
exampleFunction();
// printf("%d", localVar); // Error: localVar is not accessible here
return 0;
}
- Global Variables: Global variables are declared outside of any function and have global scope, meaning they can be accessed by any function in the program. They are created when the program starts and destroyed when the program terminates. Global variables can be accessed and modified from multiple functions, but they should be used with caution due to their potential for creating dependencies and making code harder to maintain.
#include
int globalVar = 20; // Global variable
void exampleFunction() {
printf("Global variable: %d\n", globalVar);
}
int main() {
exampleFunction();
globalVar = 30; // Modifying the global variable
printf("Modified global variable: %d\n", globalVar);
return 0;
}
- Static Variables: Static variables are declared with the static keyword. They are local to the block in which they are declared, but unlike normal local variables, they retain their values between function calls. Static variables are created and initialized only once, and their lifetime is throughout the execution of the program. They are commonly used when you want a variable to retain its value across multiple function calls.
#include
void exampleFunction() {
static int staticVar = 5; // Static variable
printf("Static variable: %d\n", staticVar);
staticVar++; // Incrementing the static variable
}
int main() {
exampleFunction(); // Static variable: 5
exampleFunction(); // Static variable: 6
exampleFunction(); // Static variable: 7
return 0;
}
- Register Variables: The register keyword is used to suggest the compiler to store a variable in a CPU register for faster access. However, the compiler is free to ignore the suggestion. Register variables have similar scope and lifetime as automatic (local) variables, but their storage is allocated in CPU registers if possible. Register variables are rarely used in modern C programming, as compilers are efficient in optimizing register allocation automatically.
#include
int main() {
register int regVar = 100; // Register variable
printf("Register variable: %d\n", regVar);
return 0;
}
- External Variables: External variables are declared with the extern keyword and have global scope. They are typically used when a variable is defined in one source file and accessed in another file. The extern keyword informs the compiler that the variable is defined in another file, and it should be linked appropriately during the compilation and linking process.
// File1.c
#include
extern int sharedVar; // External variable declaration
int main() {
printf("External variable: %d\n", sharedVar);
return 0;
}
// File2.c
int sharedVar = 50; // External variable definition
In the example above, sharedVar is defined in File2.c and accessed in File1.c using the extern keyword.
These are some of the common types of variables in C. Understanding their scope and lifetime is crucial for writing efficient and maintainable C programs.