Variables In C

Variables In C

Definition 

Variable in C is  "Name" given to "Memory Location", which is used to store data value of any kind.

Variables can be declared  with any Data Types available  in C. for example : int, float, char and double. 

int n1,n2,sum;

float pi, area;

char choice;

double Gross_Total;

Variables can also be declared using User Defined Data Types in C like structure.

struct student s1here s1 is variable of type student.

we can also have pointer variables, arrays etc.

int *ptr; Here ptr is pointer variable of type integer.

float temp[5]; Here temp is array name which can hold temperature of 5 cities.

Compiler can process data only if at is available in memory. Variables  create memory blocks which are used to store data values, these stored values are used during execution of program. 

Example

int num=10;

here "num" is variable of type "integer" and it holds value "10" as data.

Integer Data Occupies 2 Bytes or 4 Bytes in memory, depending on compiler being used.

Assume that int occupies 4 Bytes in memory, it will create following scenario in memory location.


Memory References:

Here 23256 and 23260 are called as memory references or else could be referred as memory addresses.
so here block of 4 bytes from 23256 to 23260 is named as "num". 

Data in Variable:

Data stored in variable can vary (change) during execution of program.
for example:
int num=25
So Currently variable "num" is holding "25" as data value.
num=num+1;
after execution of this statement num will be holding "26" as data value.

Protocols for giving variables name:


Variable names should not begin with digits, they can begin with Alphabets or Underscore ( _ ).

Keywords Should not be used as Variable Names. ( C Programming Consists of 32 Keywords).

Upper case Names and Lower case Names will be allocated separate memory blocks. for example,
int num,NUM;
here in this example "num" and "NUM" will get 4 Bytes of separate memory blocks and can hold different data value.

Variable Names should not include White Space.

Valid Variable Names  : num1, n, _area , area_of_circle etc.
Invalid Variable Name : 1num, for (keyword) area of circle, etc.

it will be good habit declaring variables relevant to concept. 
Suppose if area has to be calculated "area" could be relevant variable name. like,
float area;


.
.
.

Thanks and Regards

Prof. Shivling G. Swami.
Head of Computer Science and Engineering, 
Sant Gajanan Maharaj College of Engineering, Mahagaon.
Whats app and Contact Number : 99 22 970 696
Click here to view my You Tube Profile


Comments

Post a Comment

Popular posts from this blog

Data Structures : Time Complexity

If Statements in C

Most Useful String Functions in Python