Variables And Datatypes In C Language
Variables
Variables is use to carry the value.and it can be reused many times in a program. variables used to specify the particular memory location. Variable name is user defined name variable defined once will remain same . During program execution value of variable can be changed by using operators.
1.Rules /Naming of a Variable
- They must begin with a letter. It can't start with a digit.
- Keyword and reserved are not allowed as variable name.
- White space are not allowed in variable name.
- Name of variable maximum length of 31 character.more than 31 character length are invalid.
- Uppercase & lowercase letter are different in c, so variable defined with upper letter are different from lower case letter variables.
- Special characters like #, $ are not allowed.
- Variable names are case sensitive.
datatype variable-name;
3.Declaration of a Variable
int x;
char y;
float z;
4.Initialization of Variable
int x=10;
char a='xyz';
float f=34.12;
5.Types of Variable in C
Example:
void add()
{
int a=20;//local variable
}
2.Global Variable: A Variable that defined outside the function is called global variable.
Example:
int a=20;//global variable
void add()
{
int b=10;//local variable
}
3.Static Variable: Static variable can be defined inside or outside the function. A variable declared with a static keyword is called static variable.
Example:
void add()
{
int a=10;//local variable
static int b=20;//static variable
a=a+1;
b=b+1;
printf("%d,%d",a,b);
}
4.External variable: A variable declared outside the function is called external variable. A variable is declared with extern keyword
is called external variable.
Example:
extern int a=20;//global variable
void add()
{
int b=10;//local variable
}
5.Automatic variable:A variable declared within the function is called automatic variable. A variable is declared with auto keyword is called automatic variable.
Well wrote 👍👍
ReplyDelete