Variables in c

Variables in c

The Variable is the name of memory location that can used to stored values. it can take different values but take one value at a time.so these all values can change during execution of program. The datatype is associated with every variable and datatype decides what values the variables can accept or take. there are rules for naming variables are same as that for naming identifiers.

Declaration of variables

SO it is declare variable before it is used in the program. declaration of variable specifies its name and datatype.the talk about type and range of values that variables can stored its depend on data type. Syntax of declaration of variables is

Datatype variablename;

Here in the Declaration data type may int,float,char, double etc. its depend on your choice how value you want to store. some examples of declaration of variables are-

int x;
float salary;
char grade;
long a;
short s;

In this example x is variable of type int,salary variable is of type float, and grade variable is of type char. The datatype short int can written as int and long int can written as long.we can declare more then one variable in one statement example

int x, float salary, char grade, long a, short s;

Initialization of variables

When a variable is declare it contain garbage value. if we want some value we can assign some initial value to the variable during the declaration. this is known initialization of variable. example-

int x=9;
float salary=1000.9;
char grade=’a’;
long a=89;
short s=7;

Rules for Declaration of variables

1. The variable name should consist of only uppercase and lowercase letters,digits and underscore sign(_).
2. The first character should an alphabet or underscore.
3. The name should not keyword(Reserved words).
4. The C is a case-sensitive, so the uppercase and lowercase are consider different, example NOTE and note are different

Types of variables

There are five types of variables in c programming

1. Local variable
2. global variable
3. Static variable
4. automatic variable
5. External variable

Local variables

The variables in c programming that are declare inside function or inside block is known as local variables

void function()
{
int a=9; //local variable
float salary=7800.9; // local variable
}

global variables

variables in c programming that are declared outside of all function or outside of block is known as global variables. Any function can use and change the value of global variables. it is available to all functions. And the global variable automatic initialize by zero

int age=18; // global variable
void function()
{
int a=9; //local variable
float salary=7800.9;
}

Static variables

The static variable are declare by writing static keyword in front of the declaration. if a static variable is not initialize then it is automatically initialize to zero. And the static variable only initialize once

void function()
{
int count1=1;
Static int count2=1; //static variable
count1++;
count2++;
printf("%d%d",count1,count2);
}

When we call function every time the value is int count1 same everytime example 11111111 so on. but static variable count2 variable print different increment every time by one example 123456789 so on

Automatic variables

All variables in c programming are declare insides in block is known Automatic variables or local variables. we can explicitly declare Automatic variable using Auto keyword

void function()
{
int count1=1;
int age=18; //local variable also automatic variable
auto int sale=18; //automatic variable
}

External variables

The variables in c programming that have to used by many functions and in a different programs and files can declare as external variables. The default initial value of uninitialized external variable is Zerothe keyword extern is not specified in definition but some compiler may allow the extern keyword. So its better to avoid the extern keyword in the definition

Userdefined.h

extern int global=18; //also global

#include<userdefined.h> //import userdefind headerfile. that created already
void globalprint()
{
printf("%d",global);
}