Variables and Data Types

A variable can be defined as an area of memory that is set aside for the storage of a particular value. Each variable must be identified using a unique name, or identifier. A valid identifier is an alpha-numeric string consisting of one or more letters, digits or underscore characters. Spaces and punctuation characters cannot be used, and variable names must begin with a letter or an underscore character.

C is case sensitive. That means that the variable names myvar, MyVar and MYVAR are referring to three different variables. A variable cannot have the same name as one of the C keywords. Keywords are reserved identifiers that have special meaning in C. They cannot be used as identifiers in your program. The following table lists the reserved keywords.


C Keywords
autodoubleintlong
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile



Data types

Variables of different types will occupy different amounts of memory. Computer memory is allocated to variables in blocks of one or more bytes, depending on the data type. A single byte, for example, can store one alpha-numeric character, an unsigned integer with a value ranging from 0 to 255, or a signed integer with a value ranging from -128 to +127. Two or more bytes are required to store larger integer values and floating point numbers. The standard C data types, together with the range of values they can represent, are listed below.


Common C Data Types
TypeSize (bytes)Value range
unsigned char10 to 255
char1-128 to 127
unsigned int20 to 65,535
short int2-32,768 to 32,767
int2-32,768 to 32,767
unsigned long40 to 4,294,967,295
long4-2,147,483,648 to 2,147,483,647
float41.17549435 * 10-38 to 3.40282347 * 1038
double82.2250738585072014 * 10-308 to 1.7976931348623157 * 10308
long double103.4 * 10-4932 to 1.1 * 104932


Note that the size and range values shown above are those found on most 32-bit systems.



Declaring variables

Before we can use a variable in a C program, we have to declare it. We do this using a statement such as the one shown below.

int myInt;

The declaration consists of a specifier (int) which signifies the data type of the variable, and a unique identifier (myInt) which allows us to refer to the variable by name later in the program. When declaring more than one variable of the same type, they may be declared in a single statement by separating the identifiers using commas, as shown below.

int myIntA, myIntB, myIntC;

All of the integer data types (char, short, long and int) can be either signed or unsigned. Signed integer variables can be used to represent both positive and negative values. Unsigned types can only represent absolute values. Whether or not a variable is signed may be specified by using one of the keywords signed or unsigned in front of the type name, as shown below.

unsigned short int ageInYears;
signed int degreesC;

If an integer type variable is declared without being specified as either signed or unsigned, most compilers will assume the variable to be signed. An exception to this rule is the char type, which must be declared as either signed or unsigned if it is to be used to store a numeric value. Otherwise, the compiler will treat the char variable as an alpha-numeric character or control code. The keywords short and long can be used on their own in place of the short int and long int data types respectively. The declarations below are thus equivalent to one another.

short age;
short int age;

In the same manner, the keywords signed and unsigned can be used in place of signed int and unsigned int respectively. The declarations below are also equivalent to one another.

unsigned year;
unsigned int year;

The following short program demonstrates the declaration and use of program variables.

// Example Program 1 - Declaring and Using Variables

#include <stdio.h>

void main ()
{
  // declare the variables:
  int a, b, result;
  char str[1];

  // initialize variables a and b
  a = 5;
  b = 2;

  // perform some arithmetic operations
  a = a + 1;
  result = a - b;

  /* print out the value of result, wait for the user
     to press ENTER, then end the program */
  printf("The value of result is: %d", result);
  printf("\n\nPress ENTER to continue.");
  gets(str);
}


The output from example program 1

The output from example program 1



Variable scope

As we have seen, the variables used in a C program must be declared before they can be used. The placement of the variable declaration within the program code will determine the scope of the variable. A variable can have either global or local scope.

A global variable is one that is declared outside of all functions (usually at the beginning of the source code), and can be accessed from anywhere in the current source code file. A local variable is one that is declared within the body of a function or code block, and its scope is limited to the block within which it is declared.

A variable declared within a function, for example, can only be accessed by program code within that function, and has automatic storage duration. This means that space is allocated when the function is called, and de-allocated when the function is no longer required.

Static storage duration can be obtained by placing the keyword static in front of the variable declaration, so that space is allocated when the program begins execution, and remains allocated until the program closes. The value of the variable is preserved during subsequent calls to the function that defines it (variables with file scope are automatically static variables).

Access to variables from outside of the source code file in which they are declared can be enabled by placing the keyword extern in front of the variable declaration. This allows a variable defined in one source code file to be accessed by code in another. The following short program illustrates the difference between local and global variables.

// Example Program 2 - Using Global Variables
#include <stdio.h>

int globalInt;  // a global variable

int square (int x)
{
  int localInt = x;  // a local variable
  return localInt * localInt;
}

void main ()
{
  // local variables
  int result = 0;
  char str1[1];
  char str2[] = " Enter a number: ";
  char str3[] = " The value of result is: ";

  printf(str2);
  scanf("%d", &globalInt);
  result = square(globalInt);
  printf("%s %d", str3, result);
  printf("\n\nPress ENTER to continue.");
  gets(str1);
}


The output from example program 2

The output from example program 2

A global variable can be referenced from anywhere in the program. A local variable can only be referred to from within the body of the function in which it is declared. In the example above, therefore, the value of the variable result cannot be referenced from within the square() function. Similarly, the value of the local variable localInt cannot be referenced from outside the square() function. For example, using

scanf("%d", &localInt);

instead of

scanf("%d", &globalInt);

would cause a compilation error, because the variable localInt is only visible within the square() function.



Initialising variables

When a variable is first declared, its value is by default undetermined. It is possible, however, to initialise (assign a value to) the variable in the same statement that declares the variable, as shown below.

int x = 10;
/* this statement declares an integer variable called x
and assigns a value of 10 to it */

The following short program demonstrates the principle.

// Example Program 3 - Initialising Variables

#include <stdio.h>

void main ()
{
  char str[1];
  int x = 5;     // initial value = 5
  int y = 10;    // initial value = 10
  int result;    // initial value undetermined

  result = x * y;
  printf("The product of x and y is: %d", result);
  printf("\n\nPress ENTER to continue.");
  gets(str);
}



The output from example program 3

The output from example program 3