How to assign variable.


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.
  1. Assign the value  directly in the program.
  2. 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.


In this code "int sum" is the variable declaration.
"sum" is the variable name and "1+2" is the values that assigning to variable.

In this case output must be 3 lets check it.


02.Declare 02 variable and assign values for it.





03.Let's get a value from the user and check it.


What is "scanf"?
scanf is the function that get a input from the user.


04.Let's do a calculation by get a value from user.



 

I hope now you can understand what is variable declaration, assign a values to variable and how to do it practically.

-CodeQuasar Team-





Post a Comment

Previous Post Next Post