Categories
b. Variables and Keywords

Creating a Variable – Behind the Scenes

Declaration of variables must be done before they are used in the program. The declaration does the following things.

  1. It tells the compiler what the variable name is.
  2. It specifies what type of data the variable will hold.
  3. Until the variable is defined the compiler doesn’t have to worry about allocating memory space to the variable.
  4. The declaration is more like informing the compiler that there exists a variable with the following datatype which is used in the program.
  5. We can even declare a variable outside the main() function, using the extern keyword.
extern int a;
extern float b;
extern double c, d;

Copy

Defining or Initializing a variable means the compiler has to now assign storage to the variable because it will be used in the program.

We can even declare multiple variables of the same data type in a single line by using a comma to separate them.

For example,

int a;
float b, c;

Copy

Initializing a variable means providing it with a value.

int a = 10;

Leave a Reply

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