Sunday, October 27, 2013

Rules for naming variables


♦ Variable name is formed by using alphabets, digits and underscore characters.

♦ Variable name must begin with alphabet or underscore.

♦ The maximum number of characters used in forming a variable name is 32. Some compilers allow the variable name length to be more than 32 characters, however only first 32 characters are significant. i.e. the following two variable names are same.
E.g. TheSalary_Of_theEmployeePerMonth
       TheSalary_Of_theEmployeePerMonthisEqualto 
Because the compiler considers only first 32 characters.

♦ C is case sensitive language. For instance the variable names such as IQ,Iq,iq, and iQ are treated as different variable names.

♦ Keywords cannot be used as variable names.
E.g. int char; is wrong. As char is keyword.  

Note: It is not a good idea to begin variable name with an underscore character because compiler often starts special variable names and function names with underscore.

The following are some of the valid variable names:
MAXIMUM,   sum,  Salary_of_employee,       _DA,
Name007,    ClassMarks, age_18 etc.

The following are some of the invalid variable names:
Boy’s            : Illegal character “’”.
My salary      : Blank space is not allowed.
gross-salary  : Illegal character “-“.
2Brothers      : First character should be alphabet or underscore.
PriceIn$       :Illegal character “$”.

Note: Variables that are declared but not initialized will contain garbage value or unpredictable value.
                      
Look at the following program.

#include <stdio.h>
#include <conio.h>
void main()
{
          int smallb;
          clrscr();
          printf(“Hi ! I am smallb. Your local friend.”);
          printf(“\nValue assigned to me by Big B (compiler) is=%d”,smallb);
          getch();
}

#----------------------------------------------------------------#
Hi ! I am smallb. Your local friend.
Value assigned to me by Big B (compiler) is=-28715

#----------------------------------------------------------------#

No comments:

Post a Comment