Going In Depth of Variables in C – Declaration, Rules and Scope

RULES FOR CONSTRUCTING VARIABLES NAME:

  1. The name is a combination of alphabets, digits or underscores and its length should not exceed 8 characters. Some compilers allow 40 characters too.
  2. The first character is an alphabet
  3. No commas, blank spaces are allowed in the variable name
  4. 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:

  1. It should tell the compiler what the variable name is
  2. 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
  1. ‘=’ is an assignment operator, i.e. when we write a=8, it indicates that the value 8 is assigned to the variable a.
  2. ‘==’ 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

  1. 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).
  2. Global: Here the variable is declared outside all functions. These can be used by all functions in the program(like your official name).

Leave a Comment

Your email address will not be published. Required fields are marked *

Share This