Define C Variable

In C, Variables are used to store values temporarily entered by a user. Variables are the entity that may vary during program execution. Variable names are called identifiers.

Rules to declare a variable:

  1. The variable name can contain any sequence of alphabets, digits and underscore.
  2. A variable name can start with an alphabet and underscore only. It can't start with a digit.
  3. Special characters, white space or a comma are not allowed.
  4. Any Reserved word or Keyword cannot be used to declare a variable name.
  5. Upper and lower case letters are distinguishable.
  6. All variables must be declared before use with its data type at the top before any other statement.

Valid Variable

Some valid variable names:

int a; //can be single character.
int _a; //can start with underscore character.
int a1; //can be alpha-numeric.
int circle_area; //can contain underscore character
int SUM; //can be in uppercase.

Invalid Variable

Some invalid variable names:

int $a; //cannot start with special symbols like $.
int 5; //number cannot be use to declare variable.
int 1r; //invalid, cannot start with a number.
int circle area; //invalid, cannot contain space.
int void; //keyword cannot be use for variable name.

Example 1: Program to explain variable declaration and assignment. 1

#include<stdio.h>
void main()
{
    int a, b, c; //variable declaration
    a=10; //variable assignment
    b=20; //variable assignment
    c=a+b;
    printf("\nSum = %d",c);
}
Sum = 30