Variables, the basic data objects in a program, allow a programmer to store data such as floating point values (real numbers), integer values, and characters. Variables in C can be given any name made from numbers, letters, and the underscore "_". However, there are two rules that a programmer must follow:

  • Variables must not begin with a number
  • Variables must not be a keyword

The basic types are char, int, long, short, float, and double. Each type is stored in memory whose size is measured in bits, the smallest unit of computer storage, capable of storing only a 0 or a 1.

Note that the following code snippets do not constitute whole C programs that can be run. They are merely excerpts from hypothetical programs that illustrate the given topics.

char:

Variables of type char store a single character value which is typically 8 bits in size. A char can be assigned a value between -128 and 127 or a single quoted ASCII character.

Note that in this example the "%" characters in the first parameter of the printf function specify the way the provided values should be interpreted for printing (%c is for character output), and are not related to the mathematical modulo operator.

int:

Variables of type int store an integer value, which is typically 32 bits in size. An int can be assigned a value between INT_MIN and INT_MAX which are defined on your system in /usr/include/limits.h. On most of today"s computers the maximum signed int value is 2,147,483,647.

float:

Variables of type float store a floating point value in single precision (32 bits).

double:

Variables of type double store a floating point variable in double precision (64 bits).

Note:

C uses the IEEE 754 floating point representation standard.

Type modifiers

Each different C compiler defines which type modifiers are in effect by default for each variable type. To write portable code, you may want to declare which type modifiers you want to be in effect for each variable. Available type modifiers are:

  • unsigned: alters an int or char declaration to reflect that the variable can only be non-negative
  • signed: alters an int or char declaration to reflect that the variable can be positive, negative, or zero
  • long: alters an int, float, or double declaration to increase its precision
  • short: alters an int, float, or double to reduce the precision of the variable
  • const: specifies that a variable should not have its value changed; the variable is immutable (though the relevant memory is still mutable)

There are other modifiers, such as static, volatile, and extern that are used less frequently.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement