C Programming

variables

Variables in c language is used to store the value and it can change during run time (while execution of program). A variable can only stores values specific to it's data type. For example

          int qty;
          float price;
          char name;

In the above declaration qty has data type int, So it can only store integer values like 5,10,12,20... etc. We can not store values like 'A' or 'a'. If we store 5.2 in qty, it will converted to 5.

In price we can store decimal point value and integer value but in the case on integer value it will add .0000000 after value. For example 5 will become 5.0000000. we can not store char values like 'a' or '5' as '5' will also consider as char.

Same in name, we can only store char values like 'A' or '5'. we can not store integer or float values.

The ANSI rules for variables names are as follows.

  • Only letters( uppercase and lowercase), digits and underscore can be used.
  • Variable name always begins with letter (1_name will be invalid) .
  • Only first 32 characters are significant.
  • Keywords can not be used.
  • Variable names are case sensitive. The average  and Average are two different variables.

Variable name should be meaningful so that reader can get clear idea that which value is going to store in it. Consider following variable names.

          price

          qty

          name

          get_calc

Table below shows the some valid or invalid examples.

Variable nameValid/InvalidRemark
maxvalid
max4valid
max 3invalidspace is not allowed
avg_numvalid
doubleinvalidreserved word
1tempinvalidshould start with letter