C Variables

In Programming Languages variables are used for storing values such as numeric values, characters, character strings etc. Basically a variable acts like a container for information, which can be accessed by using the variable name in all over the program code depending on its scope.

The primary purpose of variables is to store data in memory for later use. A variable represents a specific piece of memory. Every variable in a program has a name, which will correspond to the memory address for the variable. We use the variable name to store a data value in memory or retrieve the data from the memory.

Variable Name :

The name we give a variable is called the variable name. A variable name is a sequence of one or more uppercase letters, digits and underscore characters ( _ ), which always begins with a letter or underscore ( _ ). For example :
num    NUM    n1    var_123    john90    _var90

Rules for Naming a  Variables :

1. A variable name must not be start with a digit.

2. It can consists of alphabets, digits and special symbols like underscore ( _ ).

3. Blank or spaces are not allowed in variable name.

4. C Keywords are not allowed as variable name.

5. C is a case-sensitive programming language, thus NUM and num are two different identifiers in C,
    means upper case and lower case names are treated as different.


Datatype of variables :

A variable in C language must be given a type, which defines what type of data the variable will hold. It can be the following :
  • char : for holding/storing character.
  • int : for holding/storing integer.
  • float : for holding/storing float value.
  • double : for holding/storing double value.
  • void.

Creating Variables in C :

In C a variable is must be declare and defined before they are used in the program. A variable definition tells the compiler where and how much storage to create for the variable. The basic syntax of defining a variable in c is :
 type variable_name;
Or
 type variable_list;
Where
  • type denotes C data type.
  • variable_name denotes the name of the variable.
  • variable_list denotes the list of variable names.
For example :
int num;
or
int num1, num2, num3;
Initializing values to variables
num = 10;

num1 = 30;
num2 = 40;
num3 = 50;
We can also initialize the values to variable at the time of declaration of variables.
int num = 20;
int num1 = 30, num2 = 40, num3 = 50;

Next Topic :