Bismillahir Rahmanir Rahim
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
Explanation with Example
Tip 1 : Use allowed Characters
Valid Namesnum Num Num1 _NUM NUM_temp2
Tip 2 : blanks are not allowed
Invalid Namesnumber 1 num 1 addition of program
Tip 3 : No special symbols other that underscore
Valid Identifiernum_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.
iNt Char Continue CONTINUEInvalid 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.
- __TIME__
- __DATE__
- __FILE__
- __LINE__
__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); }
Tip 8 : Name of identifier cannot be exactly same as of name of another identifier within the scope of the function
Valid Example :#includeint main()
{ int ivar = 15; { int ivar = 20; printf("%d",ivar); } }Invalid Example : We cannot declare same variable twice within same scope
#includeint main()
{ int ivar = 15; int ivar = 20; printf("%d",ivar); }
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.
#includeOutput :int main(){ int M_PI=25; printf("%d",M_PI); }
25Illegal Example :
#includeOutput :#include int main(){ int M_PI=25; printf("%d",M_PI); }
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.