Pages

Wednesday, September 23, 2009

How to name a variable

Before declaring a variable keep the following things in mind
  1. The name of the variable shouldn't increase 31 characters in length. For example if you create a variable like
    int abcdefghijklmnopqrstuvwxyz123456;
    the compiler will flash an error since the length is exceeding 31 characters.
  2. The first character of any variable must be an alphabet or underscore(Shift + Hyphenation). The following declaration therefore is invalid
    int 1abc;
    int #incorrect;
    while the following is correct
    int marks,int data,int _message;
  3. Variable names must not contain a space or any other symbol except underscore.
  4. Variable names are case sensitive. Variable name,Name,NAME,nAme are treated as different.
  5. Names must be unique. Once you have used a variable name you can not use it again.
  6. You can not use a keyword as a variable name, for example keywords like void,int,main have a special meaning in C therefore the names must be userdefined.
    Invalid variable names
    1. auto
    2. enum
    3. int
    4. return
    5. switch etc.

0 comments:

Post a Comment