Notes 2 - Rules For Naming Variable, Declaration Definiton

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

CGC COLLEGE OF ENGINEERING, LANDRAN (MOHALI)

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.

Consider following Scenario –


Step 1 : Memory Before Variable Declaration
Initially before declaring variable, we have Only memory.
Memory is block of bytes.
Each byte is filled with random or garbage data.
Step 2 : Declaring Variable in C Programming
Now we have declared a variable. (in this case we have declared character
variable)
Compiler Checks data type . Depending on data type it will allocate appropriate
bytes of memory. (in this case Compile will allocate 1 byte because we have
declared Character data type)
It’s only declaration so , garbage value inside that address remains the same.
Step 3 : Initialize Variable in C Programming
We have Initialize character variable now.
After initializing value inside that memory block gets overwritten.
Conclusion :
Variable Name always holds a single Value.
Variable Name is user defined name given to Memory Address.
During Second Run , Address of Variable may change.
During second Run , Value set inside variable during current will be considered as
garbage..
C variable naming rules
Rules For Constructing Variable Name
Characters Allowed :
Underscore(_)
Capital Letters ( A – Z )
Small Letters ( a – z )
Digits ( 0 – 9 )
Blanks & Commas are not allowed
No Special Symbols other than underscore(_) are allowed
First Character should be alphabet or Underscore
Variable name Should not be Reserved Word
Explanation with Example
Tip 1 : Use allowed Characters
Valid Names
num
Num
Num1
_NUM
NUM_temp2
Tip 2 : blanks are not allowed
Invalid Names
number 1
num 1
addition of program
Tip 3 : No special symbols other that underscore
Valid Identifier
num_1
number_of_values
status_flag
Tip 4 : First Character must be underscore or Alphabet
Valid Identifier
_num1
Num
Num_
_
__
Invalid Identifier
1num
1_num
365_days
Tip 5 : Reserve words are not allowed
C is case sensitive.
Variable name should not be Reserve word.
However if we capitalize any Letter from Reserve word then it will become legal
variable name.
Valid Identifier
iNt
Char
Continue
CONTINUE
Invalid Identifier
int
char
continue
Tip 6 : Name of Identifier cannot be global identifier
Basic Note :
Global predefined macro starts with underscore. (_) .
We cannot use Global predefined macro as our function name.
Example : Some of the predefined macros in C
__TIME__
__DATE__
__FILE__
__LINE__
Valid Identifier
__NAME__
__SUM__
Invalid Identifier
__TIME__
__DATE__
__FILE__
Tip 7 : Name of identifier cannot be register Pseudo variables
Invalid Example :
#include<stdio.h>
int main(){
long int _AH = 15;
printf("%ld",_AH);
return 0;
}
Tip 8 : Name of identifier cannot be exactly same as of name of another
identifier within the scope of the function
Valid Example :
#include<stdio.h>
int main(){
int ivar = 15;
{
int ivar = 20;
printf("%d",ivar);
}
return 0;
}
Invalid Example : We cannot declare same variable twice within same scope
#include<stdio.h>
int main(){
int ivar = 15;
int ivar = 20;
printf("%d",ivar);
return 0;
}
Tip 9 : Constants
We know that M_PI constant is declared inside math.h header file.
Suppose in our program we have declared variable M_PI and we have not included
math.h header file then it is legal variable.
Legal Example :
#include<stdio.h>
int main(){
int M_PI=25;
printf("%d",M_PI);
return 0;
}
Output :
25
Illegal Example :
#include<stdio.h>
#include<math.h>

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“.

Variables can be categorized depending on Number of Variables it can store –

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);
}

int sum(int n1,int n2)


{
return(n1+n2);
}
above definition will cause compile error.
Difference between Declaration and Definition
No Declaration Definition
1 Space is Not Reserved In Definition Space is Reserved
2 Identifies Data Type Some Initial Value is Assigned
3 Re-Declaration is Error Re-Definition is Error

You might also like