C Variables

C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. The value of the C variable may get change in the program.C variable might be belonging to any of the data type like int, float, char etc.

Variable Definition in C

type variablename;

or

type variablename, variablename, variablename;

Variable Definition and Initialization

int    width, height=5;
char   letter='A';
float  age, area;
double d;

/* actual initialization */width = 10;
age = 26.5;

Rules to name a Variable

  • Variable name must not start with a digit.
  • Variable name can consist of alphabets, digits and special symbols like underscore _.
  • Blank or spaces are not allowed in variable name.
  • Keywords are not allowed as variable name.
  • Upper and lower case names are treated as different, as C is case-sensitive, so it is suggested to keep the variable names in lower case

Datatype of Variable

A variable in C language must be given a type, which defines what type of data the variable will hold.
It can be:

  • char: Can hold/store a character in it.
  • int: Used to hold an integer.
  • float:Used to hold a float value.
  • double: Used to hold a double value.
  • void: Used to hold a void value.

Declaring & Initializing C Variable

  • Variables should be declared in the C program before to use.
  • Memory space is not allocated for a variable while declaration. It happens only on variable definition.
  • Variable initialization means assigning a value to the variable.