RULES FOR CONSTRUCTING VARIABLES NAME:
- The name is a combination of alphabets, digits or underscores and its length should not exceed 8 characters. Some compilers allow 40 characters too.
- The first character is an alphabet
- No commas, blank spaces are allowed in the variable name
- No special symbols, except underscore ‘ _ ‘ should be used
Here is the syntax for declaring variable name with an example:
VARIABLE DECLARATION :
A variable should be declared as follows:
- It should tell the compiler what the variable name is
- Specifies what data the variable will hold
A syntax and example below illustrate the above key points:
ASSIGNING VALUES TO VARIABLES :
- It is important to understand the difference between these two symbols: ‘=’ and ‘==’ in C
- ‘=’ is an assignment operator, i.e. when we write a=8, it indicates that the value 8 is assigned to the variable a.
- ‘==’ is a Boolean operator that tells whether the elements on the two sides of this operator are equal.
You can also assign a value to the variable at the time of declaration. More than one variable of similar data type can be initialized in one statement.
SCOPE OF VARIABLES :
The scope of variables explains where the variable stands in the program. It’s similar to your official name and your own nickname. While your nickname is only used at the place where it was assigned to you, while your official name is used everywhere. The same is with the scope of variables.
As with your name, variables also have two scopes: Global and local
- Local: Its usage is similar to your nickname. That is, when a variable(your name) is declared inside a function (like your home for example) then such a variable is local variable(i.e your nickname). This cannot be used in other function(your nickname is not used at your institute for example).
- Global: Here the variable is declared outside all functions. These can be used by all functions in the program(like your official name).