Most important function is storing data while the application running. The data may be age, username, picture, full name etc. To hold data variables are used. Variables are stored in the memory. After application run all variables are cleaned. So variables do not persists between application runs. As variables stored in memory they need some space in the memory but how much? This memory size can change according to data type or variable type.We will look variable types below. Say we have a string like “Hello poftut” and as number
1 . They will occupy different lengths of memory. Knowing this is enough for now.
We have already seen syntax of C programming language but look again will make learning better. Below is a syntax which is the same with statement to define a variable.
type is the type of variable as we stated before. Because there are different type of data like number, string, floating-point etc. Types are differentiated to make C programming language more efficient and fast. If all data were same type there will be a lot of casting or type guessing to operate on them.
Here we will define a variable that holds age.
int is our variable type and
age is our variable or variable name which will hold the age data.
Just defining a variable is not enough because we want to hold data on variable. Here is how can we do this
In this line there is two step. One step is defining an
int variable named
age . Second is assigning
25 to the this variable named
An other way to set value is after definition. So we learned that we have no obligation to set data on the variable definition.
int age; age=25;
Define Multiple Variables
We can define multiple variables in a single shot to make it easy and readable like below. This is called as multiple variable definition with single statement.
All of these variables named
length are integer type are defined in a single statement. We will look variable type next chapters.
Define and Assign Multiple Variables
Now we merge what we have learned and define multiple variables in a single line by setting values to them.
int age=25, year=2016, length=180;