Variables in C

No comments
In a C program we need to store data for using it in our program, this data is stored in computer’s memory. Variables are the names given to the memory location in computer’s memory where a specific data is stored so that we can assess it using the variable assigned to that memory location when required. Each variable should be given a unique name which is called identifier.
In C programming language each variable has a type that determines the following things about the memory location-
  • Size of the assigned memory and layout/

  • It determined the range and type of values that can be stored in that memory location.

  • Types of operations which can be done on that variable.

Rules for naming a variable in C-


There are some common rules which we need to follow when defining a variable in C, to avoids the errors.
  • The variable name must always begin with a letter or an underscore (_),but is recommended not to start a variable name with a underscore.

  • Variable name can have letters(uppercase and lowercase both),numbers and underscore(_).

  • C is a case sensitive language ie word and WORD are two different variables.

How to declare a variable in C-

We have to declare a variable before we can actually use it in our program. Declaration of a variable is a statement which consist the data type(type of data we want to store ) and a list of variable name separated by commas of that data type.
Syntax:data type variable1, variable2, variable3;
For Example
#include <stdio.h>
#include <conio.h>

void main(){
int i,j,k,d; //declaration of variable i,,j,k and d
char a,c,v; //declaration of variable i,,j,k and d
}

In above two statements we have declared 7 variables , 4(i,j,k and d) in the first statement and 3(a,c,v) in the second statement.The first statement tells the compiler to create the variables i,j,k and d of type int and the second statement tells the compiler to create the variables a,c,v of type char.
Int and char are the data types which gives the information about the storage location and i,j ,k etc are the names of the variables. We will learn in detail about data types in C in our next tutorial so leave it for now.
Note:A variable must be declared before it is used in the program.

Initialization of variables in C-

C variables can be initialized at the time of declaration. C variable are initialized with the help of assignment operator '='.
For Example:
#include <stdio.h>
#include <conio.h>

void main(){
Int i=2, j=4, d=7; //initialization of i,j and d variables.
Char a ='X'; //initialization of variable a.

}

Declaring and initializing variables in C -A program

#include <stdio.h>
#include <conio.h>

void main(){
{
  /* Declaring variables x,y,z and p */
  int x, y;
  int z;
  float p;
 
  /* Initialization of variables using assignment operator.  */
  x = 12;
  y = 24;
  
  z = x + y;
  
printf("sum of x and y is z : %d \n", z);

  p = 72.0/4.0;
  printf("value of p is : %f \n", p);
 
  
}

}

Output:sum of x and y is z: 36
value of p is :18.000000
There are some terms in the above program which you may not understand now but don't worry about that we will learn more about these terms in our next tutorials.For now you just have to write this program as it is and run it.

What are Lvalues and Rvalues in C

Lvalues in C:

lvalues are the expressions which represents a memory location and can be written on either side left or right side of an assignment operator.for examples –variables are lvalues.

Rvalues in C:

rvalues are the expressions which represents the data value stored in some memory location referred by any variable. Rvalues are always written on the right side of the assignment operator.
Int a=22; is a legal statement whereas 22=33 is an invalid statement and you will get a compile-time error.

No comments :

Post a Comment