Variable in C
Every C program is consists of functions and variables, whatever its size. a variable is an entity that changes during the execution of the program.
Variable is the name given to the memory location in which data/value is stored and these stored values are used during the execution of the program.
All variables we need to be declared before they are
used, mostly at the beginning of the function before any executable
statements.
When we declare the variable must announce the properties of that
variables; it consists of a data type name and a list of variables.
A variable definition specifies a data type and contains a list
variable of that type as follows −
Data_type variable_list;
char c, ch;
float a, colour;
double b;
Rules for defining
variables:
- The variable name can be a
combination of alphabets( Alphabets can be in capital or lower
case), digits, and underscore
- Variable can start with
alphabets or underscore _ symbol
- Alphabets can be in capital or
lower case
- Never start a variable name with
the number.
- The maximum length allowed to
declare is 32 characters
- You can not use a keyword as a
variable name.
- No special symbols allowed except underscore
Declaring Variables:
Variables are declared in three basic places:
Inside functions: local variables
In the definition of function parameters: Global Variable
Outside of the functions: Global Variable
Types of Variables:
Global variables:
int value=30;
/ *global variable */
main()
{
}
Local variables:
Local
Varibles are declared inside a function are called as local variables You
must have to initialize the local variable before it is used.
void main(){
int x=20;
/ *local variable*/
}
Static
Variable
A
static variable is declared with the static keyword and it retains its value
for multiple functions call.
void main(){
Static
int x=20; / * static variable*/
}
Automatic Variable
Automatic
variables are similar as local variables. All variables in C which are
declared inside the block, are automatic variables by default. We can
explicitly declare an automatic variable using auto keyword.
void
function()
{
int a=20;//)
auto int b=10; /*automatic variable*/
}
External Variable
The external variable is shared between multiple C
files. We need to store the external variables in a separate file and file extension is .h example: myfile.h and We can declare an external
variables using extern keyword.
Example:
myfile.h
program11.c
#include "myfile.h"
#include <stdio.h>
void mail(){
}