Variables in C
variables are identifiers which is used to store data like numbers , character , strings etc.
so , technically it is a box or a container where we store data and it may be changed at any point in the program hence named as variable.
At first we need to learn some terms ,
1. Syntax
2. Variable declaration
3. Initialization or Value assignment to variable
What is syntax ? Syntax is grammatical process for doing something in C. Like there are rules which you must follow to perform some task and that rule is called syntax.
For using a variable you have to declare or create it first , declaration is like telling computer to create this variable.
Initialization means using that variable to store some data.
Syntax for declaring a Variable.
data type variable name ;
Example:
int a ;
where int is data type integer if you are reading all my articles then you should already know about data and their types .
In C every line of code is ended by semicolon , its compulsory.
so we declared a variable , now its time for using it or lets say storing some data to it.
a =10;
This line will assign or store 10 to variable a and this process is called as initialization
It should be noted that int data type can only store integer , char can only store single character , float can only decimal.
Few more examples :
int num; // declaration
num =20; // assignment or initialization
char letter; // declaration
letter ='a'; // assignment or initialization
To assign a character we use single quote ' ' to data
float GPA ; // declaration
GPA = 3.8; // assignment
Now there is also another way of doing these things , like declaration and assignment at the same time.
Example :
int num = 20 ;
char letter ='a' ;
In most case we first declare and then use it later but sometime its easy to assign immediately like in loop
int i = 0;
we will learn more about loops later.