is this the right way to assign a value to a variable created using
malloc?
For starters there is no variable crated using malloc. Variables are created by declarations.
In this line
int *var = (int *) malloc(sizeof(int) * 2) ;
there is declared the variable var having the type int * that is initialized by the address of the dynamically allocated memory using malloc for two objects of the type int.
To access the dynamically allocated objects you can use the subscript operator like
var[0] = 22;
var[1] = 33;
that is the same if to write
*var = 22;
*( var + 1 ) = 33;
So to output the values of the allocated objects you can write
printf("%d \n" , *var);
or
printf("%d \n" , var[0]);
or
printf("%d \n" , var[1]);
or
printf("%d \n" , *( var + 1 ));
As for this code snippet
char *var;
var = "birds are dying";
then the variable var itself that is assigned with the address of the first character of the string literal "birds are dying" not an object that could be pointed to by the variable var.