Skip to main content

Variables in C

 

Variables in C

 


Variables in programming

In computer terms, a variable is the name of the memory location of the data which can be accessed with the help of that name and can be rewritten or used for calculations later on. Variable is a storage area. which is used to store the data. variable data is contained, which can be changed at any time during program execution. After declaring the variable it is given a value, this value is to be assigned in many forms. Like x = 5; or a=10;    Before using the variable it is necessary to declare in C. Values of variables are changeable. You can delete a value and enter another value.  You can also do this on compile time and dynamically(during program execution). For example:  Let’s say we have to store a student’s name and his roll number in a variable. For this, we take two variables, store the name of the student in a variable while the other variable stores the roll number

 

Rules for Variable:-

  • These store the values inside it.
  • Variable is also the name of a memory location.
  • Variable is case-sensitive. For example, int a or int A both are different variables.
  •  Variable stars with any alphabet (a-z, A-Z) or underscore(_).
  • Variables' names can be alphanumeric. for example, a1=5,  var1, var2
  • Variable does not allow space.
  • The variable name does not have any C keywords.
  • The name of any variable can not start with any number.
  • Any upper case and lower case characters can be used in any variable name.

 

Datatype of Variables:

A variable should be given a type in the C language,  which determines what kind of data the variable will hold. it can be:

  • char: it can hold a character in it.
  • int: it is used to hold an integer.
  • double: it is used to hold a double value.
  • float: it is used to hold a float value.


DECLARATION OF A VARIABLE

While programming you first need to tell the computer/compiler the variable’s name and its data type when you do that the compiler creates an empty memory reserved for that data.

In other words, we can say that

  • When we declare a variable, then it allocates memory according to the data type of the variable.
  • After the declaration of the variable, it takes Garbage Value inside it.

The syntax for  Single Variable Declaration

data_type single_variable_name;

Example:

int a;
float b;
char c;

Source code:

#include<stdio.h>
int main()
{
   int a=25;
   printf("value of a : %d", a);
   return 0;
}

Output:

value of a : 25

Comments