Variable In C
- C programming, a variable is a named storage location in memory that can hold a value of a specific data type.
- variables are containers for storing data values, like numbers and characters.
- When a variable is declared in C, the compiler sets aside a block of memory for that variable with the specific size of its data type.
- The memory address of the variable is used to access its value during the program execution.
- Variables in C can be different data types such as integer, float, character, double, etc.
- int - it can stores integers like whole numbers.
Syntax:
type variableName = value;
To declare a variable in C, you need to specify its data type and a name
for the variable. Here's the corrected explanation:
To declare an integer variable named x, you would use the following
statement:
For example: int x;
This statement declares a variable named x of type int. You can then
assign a value to the variable using the assignment operator
=.
For example: x = 10;
This assigns the value 10 to the variable x.
You can also declare and initialize a variable in a single
statement.
For example: int x = 10;
This declares a variable named x of type int and initialize it with the
value 10.
Program:
#include<stdio.h>
int main(){
int num = 10;
float num1=20.0;
char letter='A';
printf("The value of num is: %d\n", num);
printf("The value of float num1 is: %f\n", num1);
printf("The character is: %c\n", letter);
return 0;
}
Output:
The value of num is: 10
The value of float num1 is:20.000000
The character is:A
The character is:A
There Are Many Types Of Variables In C
- Local variable
- Global variable
- Static variable
- Non Static variable
- Automatic variable
- External variable
Local Variable
A local variable is one that is defined within the scope of a function or
block. It is necessary to declare a local variable at the beginning of the
respective block or function.
Program:
#include<stdio.h>
void fun(){
int a=23;
// local variable
printf("Value of a is: %d \n",a);
}
int main() {
fun();
}
Output:
Value of a is: 23
Global Variable
A global variable is a variable declared outside any specific function or
block. Any function in the program can modify the value of this variable,
and it remains accessible to all functions. The global variable needs to
be declared at the beginning of the program.
Program:
#include<stdio.h>
// Declare a global variable outside any function
int global_variable = 10;
// Function that uses the global variable
void printGlobalVariable() {
printf("Global variable value: %d\n", global_variable);
}
// Main function
int main() {
// Call the functions
printGlobalVariable();
return
0;
}
Output:
Global variable value: 10
Static Variable
A static variable is one that uses the "static" keyword when declared.
Unlike other variables, it keeps its value even when the function it's in
finishes running. This means the variable remembers its value for the next
time the function is called.
Program:
#include<stdio.h>
void fun(){
static int y=5;
y=y+1;
printf("y= %d\n",y);
}
int main(){
fun();
fun();
fun();
}
Output:
y=6
y=7
y=8
Non Static Variable
In C, a non-static variable is a variable that doesn't use the "static"
keyword when declared. These variables are often referred to as automatic
or local variables, and they are created and initialized each time the
block or function in which they are declared is entered. They have a
limited scope and lifetime, existing only within the block or function
where they are defined.
Program:
#include<stdio.h>
void fun(){
int y=5;
y=y+1;
printf("y= %d\n",y);
}
int main(){
fun();
fun();
fun();
}
Output:
y=6
y=6
y=6
Automatic Variable
In C, when you declare a variable inside a block (like a function or a
set of curly braces), it's automatically treated as an "automatic
variable." You don't need to use any special keywords for this to happen.
However, if you want to be explicit, you can use the "auto" keyword.
Program:
#include<stdio.h>
// Function prototype
void exampleFunction();
int main(){
// x is an automatic variable by default
int x = 10;
// Explicitly declaring y as an automatic variable
auto int y =
20;
// Print values of x and y
printf("x: %d\n", x);
printf("y: %d\n", y);
// Call another function
exampleFunction();
return 0;
}
// Another function using automatic variables
void exampleFunction() {
// z is an automatic variable
int z = 30;
// Print the value of z
printf("z: %d\n", z);
}
Output:
x:10
y:20
z:30
External Variable
In C, if you want to share a variable across multiple source files, you
can use an external variable. To declare an external variable, you use the
"extern" keyword.
Program:
// File: main.c
#include<stdio.h>
// Declaration of external variable
extern int sharedVariable;
int main() {
// Accessing the external variable
printf("Value of sharedVariable in main.c: %d\n", sharedVariable);
// Modifying the external variable
sharedVariable = 42;
// Calling a function from another file that uses the external
variable
externalFunction();
return
0;
}
Program:
// File: external.c
#include<stdio.h>
// Definition of external variable
int sharedVariable;
// Function using the external variable
void externalFunction() {
// Accessing the external variable
printf("Value of sharedVariable in external.c: %d\n", sharedVariable);
}
Compile command:
gcc main.c external.c -o myprogram
./myprogram
Output:
Value of sharedVariable in main.c: 0
Value of sharedVariable in external.c: 42
More topic in C