Sunday, June 30, 2013

Variables in C

A variable is a named data storage location in your computer's memory. By using a variable in the program, we are just referring to the data in our memory. To use the variables in our program, we must know how to create them. In creating a variable, there are certain conditions to be followed. They are

Conditions to setting Variable Names:

  • The name can contain letters, digits and the underscore character i.e., '_'.
  • The first character of the name must be a letter or an underscore.
  • If there is any change in the case of the letters, then they are treated as two different variables i.e., count  and Count are two different variables.
  • Keywords can't be a variable.

Examples of variable names:

  1. percent --> Correct
  2. double  --> Incorrect because double is a keyword.
  3. 9cloud  --> Incorrect as the variable name can't start with number.
  4. abc#xy --> Incorrect because of using special character #.
  5. _hello   --> Correct
  6. Hello_  --> Correct
  7. abc1     --> Correct
To know about the Keywords or the Reserved words, refer here.

For most of the C compilers, a variable name can be up to 31 characters long. Actually it can be longer than 31 characters but the compiler takes only first 31 characters.

Generally for a variable name with multiple words, there are two styles of using it.

  • using and underscore to separate words i.e., interest_rate.
  • camel notation, which means using a capital letter in the starting of the word i.e., InterestRate.