Variables in C

variables are identifiers which is used to store data like numbers , character , strings etc. 


so , technically it is a box or a container where we store data and it may be changed at any point in the program hence named as variable.


At first we need to learn some terms ,

1. Syntax

2. Variable declaration 

3. Initialization or Value assignment to variable

What is syntax ? Syntax is grammatical process for doing something in C. Like there are rules which you must follow to perform some task and that rule is called syntax.

For using a variable you have to declare or create it first , declaration is like telling computer to create this variable.


Initialization means using that variable to store some data.


Syntax for declaring a Variable.

data type  variable name ;

Example:

int a ;

where int is data type integer if you are reading all my articles then you should already know about data and their types .

In C every line of code is ended by semicolon , its compulsory.


so we declared a variable  , now its time for using it or lets say storing some data to it.

a =10;

This line will assign or store 10 to  variable a  and this process is called as initialization

It should be noted that int data type can only store integer , char can only store single character , float can only decimal.


Few more examples :

int num; // declaration

num =20; // assignment or initialization


char letter; // declaration

letter ='a'; // assignment or initialization

To assign a character we use single quote ' ' to data


float GPA ;  // declaration

GPA = 3.8;  // assignment


Now there is also another way of doing these things , like declaration and assignment at the same time.

Example :

int num = 20 ;

char letter ='a' ;


In most case we first declare and then use it later but sometime its easy to assign immediately like in loop 

int i = 0;

we will learn more about loops later.

Array of character or string


Apart from int  , char , float there is fourth type of data which is array of character which stores words like john , London etc


Syntax for declaration 

char name[20]; 

Above line creates an array of character which can store upto 20 letters or characters.

initialization
 name[20] = "john" ;

As i already mentioned in case of char we use single quotation but in case of string we use double quotation to data .

Some features of array

1. An array index always starts with 0 that is name[0] in above example stores letter j , name[1] stores o  , name[2] stores h and name[3] stores n 

2. At the last of an array \0 is added by default 
i.e name[4]  will have \0 by default


we can declare and initialize an array at the same time just like other variable but for that we need to leave square bracket empty.

char name[] = "john" ;

But why ?
well its because if we used index more than no of letter \0 will move to end
like char name[10] = "john";
then name[4] will not have \0 name[10] will have \0
and remaining array elements name[4] to name[9] will have garbage value stored.

but if size is correctly matched then its fine like 
char name[4] = "john" ; is correct because 0 to 3 will store john and 4 will store \0


This was all about variables .


Contact Form

Name

Email *

Message *