Variable in C
A variable is a name of the memory area. It is utilized to
store information. Its esteem can be changed, and it tends to be reused
commonly.
It is an approach to speak to memory area through image with
the goal that it tends to be effectively distinguished.
How about we see the sentence structure to pronounce a
variable:
type variable_list;
|
The example of declaring the variable is given below:
Int a;
Float b;
Char c;
|
Here, a, b, c are factors. The int, glide, singe are the
information types.
We can likewise give esteems while pronouncing the factors
as given underneath:
int a=10,b=20;//declaring 2 variable of
integer type
float f=20.8;
char c='A';
|
Rules For defining variable :-
·
A variable can have letters in order, digits,
and underscore.
·
A variable name can begin with the letters in
order, and underscore as it were. It can't begin with a digit.
·
No whitespace is permitted inside the variable
name.
·
A variable name must not be any saved word or
watchword, for example int, glide, and so forth.
Valid Variable Name:-
int a;
int _ab;
int a30;
|
Invalid Variable Name :-
int 2;
int a b;
int long;
|
Types Of Variables In C :-
There are many
types of variables in c:
- local variable
- global variable
- static variable
- automatic variable
- external variable
Local Variable :-
A variable that is proclaimed inside the capacity or square
is known as a nearby factor.
It must be pronounced toward the beginning of the square.
void
function1(){
int x=10;//local variable
}
|
You must have to initialize the local variable before it is
used.
Global Variable :-
A variable that is announced outside the capacity or square
is known as a worldwide variable. Any capacity can change the estimation of the
worldwide variable. It is accessible to every one of the capacities.
It must be announced toward the beginning of the square.
int
value=20;//global variable
void function1(){
int x=10;//local variable
}
|
Static Variable :-
A variable that is pronounced with the static watchword is
called static variable.
It holds its incentive between numerous capacity calls.
void
function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
|
If you call this function many times, the local variable will print the same value for each
function call, e.g, 11,11,11 and so on. But the static
variable will print the incremented value in each function
call, e.g. 11, 12, 13 and so on.
Automatic Variable :-
All factors in C that are announced inside the square, are
programmed factors of course. We can unequivocally pronounce a programmed
variable utilizing auto keyword.
void main(){
int x=10;//local variable (also
automatic)
auto int y=20;//automatic variable
}
|
Extarnal Variable :-
We can share a variable in various C source records by
utilizing an outside factor. To announce an outer variable, you have to utilize
extern keyword.
myfile.h
extern int x=10;//external variable (also global)
|
Program1.c
#include
"myfile.h"
#include <stdio.h>
void printValue(){
printf("Global variable: %d", global_variable);
}
|
No comments:
Post a Comment