Professional Documents
Culture Documents
Notes 2 - Rules For Naming Variable, Declaration Definiton
Notes 2 - Rules For Naming Variable, Declaration Definiton
Notes 2 - Rules For Naming Variable, Declaration Definiton
Department of Training
C Variables
What is Variable in C Programming
Variable in C Programming is also called as container to store the data. Variable
name may have different data types to identify the type of value stored. Suppose
we declare variable of type integer then it can store only integer values. Variable is
considered as one of the building block of C Programming which is also called as
identifier.
A Variable is a name given to the memory location where the actual data is stored.
Consider real time example, suppose we need to store water then we can store
water in glass if quantity of water is small and Bucket is the quantity of water is
large. And big can to store water having quantity larger than bucket similarly
Variable (i.e Value container) may have different size for storing different verities
of values.
What is Variable in C Programming?
Initially 5 is Stored in memory location and name x is given to it
After We are assigning the new value (3) to the same memory location
This would Overwrite the earlier value 5 since memory location can hold only one
value at a time
Since the location ‘x’can hold Different values at different time so it is refered as
‘Variable‘
In short Variable is name given to Specific memory location or Group.
int main(){
int M_PI=25;
printf("%d",M_PI);
return 0;
}
Output :
Compile error
Remember following Tricks
Do not Create unnecessarily long variable name
Do not use underscore as first character to avoid confusion between System
Variable & user defined variables because many system variables starts with
undescore
Variable names are case-Sensitive . i.e sum,Sum,SUM these all three are
different variable names.
Reserve words with one/more Capital letters allowed eg. Int,Float,chAr are
allowed but try to skip them.
C variable attributes
Fundamental Attributes of C Variable :
Name of a Variable
Value Inside Variable
Address of Variable
Size of a Variable
Type of a Variable
1.Name of Variable
We can Give proper name to any Variable which is human readable.
Compiler won’t understand this name , This is only for human understanding.
Variable name is only the name given to the “Memory address”.
Variable name maps into “Address inside” and then by considering mapped
address compiler processes data.
We have declared variable means “We have created one empty container which
will hold data“.
2.Value inside Variable
Depending on the type of Variable we can store any value of appropriate data type
inside Variable.
Suppose we have “Integer Variable” then Value inside variable will be of type
“Integer“.
Simple Variable can only hold one value at a time.
3.Address of Variable
Variable can hold data ,it means there should be a container.
So container must have Starting Address.
Address of variable = Starting Address of Memory where Memory is allocated to
Variable.
4.Type of Variable
While declaring a variable we have to specify type of variable.
Type of variable tells compiler that – “Allocate memory for data of Specified
type“.
If we declare variable of type Integer then compiler will allocate memory container
of size 2 bytes and container will be able to store integer data only.
5.Size of Variable
We can use sizeof operator to calculate size of any data type.
Variable attributes
Variable Name ivar
Variable Type Integer
Variable Address 2000
Variable Size 2 Bytes
Variable Value 34
C variable declaration
What is Declaration ?
To Declare is nothing but to represent a variable.
Only Variable name and its Data type is Represented in Declaration.
Facts about Declaration:
Memory is neither allocated to Variable nor space is Reserved after declaration.
Declaration is just used for Identification of Data Type.
Re-Declaration of variable will cause compile Error.
int ivar;
int ivar = 10;
It will cause compile error. We cannot re-declare variable.
We cannot use variable inside program expression or in program statements
without declaring.
Declaring a variable just give rough idea to compiler that there is something which
is of type “<Data Type Specified>” and will be used in the program.
Example Of Declaration :
int ivar;
float fvar;
char cvar;
C variable categories
Variable Can be categorized depending on Type of Data it stores inside –
Variable Storing Integer Data Type is called as “Integer Variable“.
Variable Storing Character Data Type is called as “Character Variable“.
Variable Storing Float Data Type is called as “Float Variable“.
Variable Storing Double Data Type is called as “Double Variable“.
Variable storing single value at a time of any data type is called as “Normal or
Ordinary” Variable.
Variable Storing multiple values of same data type is called as “Array“.
Variable Storing multiple values of different data type is called as “Structure”
[ User defined data type]
C declaration Vs Definition
Some Examples of Declaration :
Declaration of Function :
Whenever we write function after the main function, compiler will through the
error since it does not have any idea about the function at the time of calling
function. If we provide prototype declaration of function then we compiler will not
look for the definition.
int sum(int,int);
main()
{
int res = sum(10,20);
}
int sum(int n1,int n2)
{
return(n1+n2);
}
In the above example , first line is called as function declaration.
int sum(int,int);
Declaring Variable
Whenever we write declaration statement then memory will not be allocated for
the variable. Variable declaration will randomly specify the memory location.
int ivar;
float fvar;
Variable Declaration Vs Definition Differentiation Parameters
A. Space Reservation :
Whenever we declare a variable then space will not be reserved for the variable.
Whenever we declare a variable then compiler will not look for other details such
as definition of the variable.
Declaration is handy way to write code in which actual memory is not allocated.
struct book {
int pages;
float price;
char *bname;
};
In the above declaration memory is not allocated. Whenever we define a variable
then memory will be allocated for the variable.
struct book b1;
B. What it does ?
Declaration will identify the data type of the identifier.
Definition of the variable will assign some value to it.
C. Re-Declaration Vs Re-Definition
In Both the cases we will get compile error. Re-declaration and Re-definition is
illegal in C programming language.
Re-Declaring the Variable in Same Scope :
main()
{
int num1;
int num1;
}
&
struct book {
int pages;
float price;
char *bname;
};
struct book {
int pages;
float price;
char *bname;
};
above statement will cause error.
Re-Defining the Variable :
int sum(int n1,int n2)
{
return(n1+n2);
}