Variables and Data Types


Data Types

C has several types of variables, but there are a few basic types:

  • Integers - whole numbers which can be either positive or negative. Defined using char, int, short, long or long long.
  • Unsigned integers - whole numbers which can only be positive. Defined using unsigned char, unsigned int, unsigned short, unsigned long or unsigned long long.
  • Floating point numbers - real numbers (numbers with fractions). Defined using float and double.
  • Structures - will be explained later, in the Structures section.

The different types of variables define their bounds. A char can range only from -128 to 127, whereas a long can range from -2,147,483,648 to 2,147,483,647 (long and other numeric data types may have another range on different computers, for example - from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit computer).

Variables

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:

int playerScore = 95;

Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95.

The value of a variable can be changed, hence the name variable.

char ch = 'a'; // some code ch = 'l';

Now, we can do some math. Assuming a, b, c, d, and e are variables, we can simply use plus, minus and multiplication operators in the following notation, and assign a new value to a:

int a = 0, b = 1, c = 2, d = 3, e = 4; a = b - c + d * e; printf("%d", a); /* will print 1-2+3*4 = 11 */