What is a variable in C Language

Preview

What is a variable in C Language

A variable may be defined as a named memory location i.e. a variable is the name given to a data item to refer to a memory location. The value of a variable can vary during the execution of a program.

Rules to name a variable in C Language

There are few basics rules which must be kept in mind while defining a variable. They are as follows:-

  1. Both digits and alphabets are allowed while naming a variable.
  2. First character must be an alphabet or underscore.
  3. Variable name can contain digits only after first character.
  4. Keyword can’t be used to name a variable.
  5. No special character other than underscore can be used.
  6. It can’t contain spaces.
  7. Capital letters and small letters are treated differently.

Examples of valid variable names

StudentNameTeacher_name no1 price

Examples of invalid variable names

55First character can’t be a digit.
charIt is a keyword of C.
{area}Special characters other than underscore are not allowed.
father nameVariable name can’t contain a space.
Number-modSpecial character other than underscore are not allowed.
3rdFirst character can’t be a digit.

 Declaration of Variables

In a C program, the variables must be declared before they can be used The syntax for declaring a variable is :-

<data-type> variable-1,variable-2,variable3;

Here data-type refers to any valid data type of C which may be int, float, char or any user defined data type of C.

variable-1, variable-2, vaiable-3 are name of various variables defined by the programmer.

Example:

 int number;            

Above statement declares a variable named number of int type.

Initialization of Variables  

Initialization refers to assigning some value to a variable during declaration of variable.

Example

 int a=5;

Above statement initializes the variable named a with value 5.

Test Your Knowledge

Q1. Define a variable.
Q2. How can we declare and initialize a variable?
Q3. Which of the following are invalid variable names and why?
(i) Rs1000
(ii) $250          
(iii) Father’s name       
(iv) student-name
(v) Raghav parsad      
(vi)newvalue1  
(vii) India@india.com  
(viii) Factorial

Best Books of C

Lesson tags: C variables, declaration of C variable, rules to name a variable in c language, What is a variable
Back to: C Programming Language