Rules For Constructing Variable

Bismillahir Rahmanir Rahim

Rules For Constructing Variable Name

  1. Characters Allowed :
    • Underscore(_)
    • Capital Letters ( A – Z )
    • Small Letters ( a – z )
    • Digits ( 0 – 9 )
  1. Blanks & Commas are not allowed
  2. No Special Symbols other than underscore(_) are allowed
  3. 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);
    
    }

    Tip 8 : Name of identifier cannot be exactly same as of name of another identifier within the scope of the function

    Valid Example :
    #include
    int main() 
    {
        int ivar = 15;
         { 
         int ivar = 20;
         printf("%d",ivar);
         } 
       
    }
    Invalid Example : We cannot declare same variable twice within same scope
    #include
    int main()
     {
        int ivar = 15;
        int ivar = 20;
        printf("%d",ivar);
       
    }

    Tip 9 : Constants

    1. We know that M_PI constant is declared inside math.h header file.
    2. 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
    int main(){
        int M_PI=25;
        printf("%d",M_PI);
        
    }
    Output :
    25
    Illegal Example :
    #include
    #include
    
    int main(){
        int M_PI=25;
        printf("%d",M_PI);
       
    }
    Output :
    Compile error

    Remember following Tricks

    1. Do not Create unnecessarily long variable name
    2. Do not use underscore as first character to avoid confusion between System Variable & user defined variables because many system variables starts with undescore
    3. Variable names are case-Sensitive . i.e sum,Sum,SUM these all three are different variable names.
    4. Reserve words with one/more Capital letters allowed eg. Int,Float,chAr are allowed but try to skip them.

    Socializer Widget By Blogger Yard
    SOCIALIZE IT →
    FOLLOW US →
    SHARE IT →