Variables are used to store data that can be manipulated by a program. In C, variables must be declared before they can be used. This involves specifying the type of data that will be stored in the variable.
A variable declaration tells the compiler the name and type of the variable. The syntax for declaring a variable is:
For example:
Initialization of a variable means assigning it a value at the time of declaration. Variables can be initialized in two ways:
Static initialization means assigning a value to the variable when it is declared. For example:
Dynamic initialization means assigning a value to the variable during runtime. For example:
This code lets the user input a number which is printed on the console immediately after. Now what printf()
and scanf()
functions are doing, we'll discuss in Inputs and Outputs.
NOTE: Based on the lifespan and longevity of a variable in the program, there are four different types of variables called: Local, Global, Static and External. We'll discuss them better in Storage Classes.