Variable Declaration
What answer that coming to your mind when ask "What is variable declaration?"
Variable Declaration;
In C programming, variable declaration is the act of telling the compiler about a variable before using it. It involves specifying the variable's name and its type, like integer or character, so the compiler knows how much memory to allocate for it.
Declaration is done by simply listing the variables before the terminating semicolon of the definition. E.g. data_type variable; int age;
The language also enables storage to be allocated at the same time that a particular variable data type is defined. You can declare multiple variables in same type using a single statement as follows;
int height, width;
Assigning Values to Variables;
After declaring of variable with a type, you can add value to the variable. this process we called assigning values to variable. There are two ways to assign values to a variable.
- Assign the value directly in the program.
- Get value from the user, the assign that value to variable.
Assign the value directly in to the program.
Method 1: variable declaration and the assigning values as two separate statements;
data_type variable;
variable = value;
int length;
length = 20;
Method 2: direct method for variable declaration and initialization;
data_type variable = value;
int length = 20;
Variable initialization;
int age = 0;
float average = 0.0;
int minimum_marks = 40;
Let's do some exercises;
01. Declare variable and assign a value for it.
scanf is the function that get a input from the user.
04.Let's do a calculation by get a value from user.