C Programming

Variables in C

Posted on 02nd May 2013

Variables are names used to identify some location of your computer\'s memory that can store data. They are called variables because the actual value that is held in this memory area can vary. Variables are of different types based on the size and type of data that it holds.

C has several variables types to handle data of different type and length. The following are the basic variable types in C:

charTo store a single character
intTo store signed integer values
floatTo store single precision floating point numbers
doubleTo store double precision floating point numbers

Variable Declaration

A C program requires that you first inform the compiler about any variables that will be used in the program. This is called Variable Declaration. In a variable declaration you normally specify the variable type and variable name(s).

For example :
You declare an integer variable named count as:

int count;

You can declare multiple variables separated by comma as

int count, total, index;

int denotes the variable type and count, total and index are the variable names.

Naming the Variables

There are certain rules that need to be followed when naming the variables in C. They are:

  1. Names can contain only letters, digits and underscore. You cannot have spaces or any special characters in variable names.
  2. Variable names should begin with a letter or an underscore only. You cannot have a variable name starting with a digit.
  3. You cannot use any of keywords reserved by C language as your variable name. So you cannot have a variable named int which is a reserved keyword.

As a good programming practise you should give meaningful names to the variables and avoid using uppercase letters. Remember C is a case-sensitive language so the variable names Count and count are not the same.

Post a comment


Nothing yet..be the first to share wisdom.