Variable Types

Whenever you use a variable in C, it expects you to first declare the type of variable that you want to use. We have already met variables of type int and char. There is one more basic kind of variable, other than these two, and all other types are relatives of these. The third basic type is the type float for storing floating point numbers.

You can think of a floating point number as one which has a sign, a decimal point and some exponent. The exponent can be used to conveniently specify large or small numbers. For example, the number 1237800000 can be written as 1.2378e9f. Here the f stands for floating point, and the e9 stands for multiplication by 109 (which moves the decimal place 9 places to the right). Of course if the e9 is replaced by an even larger exponent, then this number can be made even larger. An example of a small number is 0.0000000001243, which can be written as 1.243e-10f. If the exponent e-10 (which moves the decimal point 10 places to the left) is made even more negative, then the number it is attached to will become tiny. Of course there is no need to specify an exponent at all with a floating point number. The numbers 0.1f, -1.246f and 12.0f are all perfectly good floating point numbers, and can be stored in a float.

The other C types allow you to specify variations on the three basic types. One variation is to specify types which are shorter (taking up less memory) or longer (for greater precision or for storing larger numbers).

The type int can be made shorter by prepending it with the word short, and longer by prepending it with the word long. There is even a super long version where the word long is prepended twice.

To specify a floating point with higher precision, one can instead use the type double which as its name suggests, allows-na for floating point numbers with double precision (i.e. twice as many significant digits). In this case, one should not follow the number with the letter f, which is used solely for numbers of type float.

By default, all integer types in C are signed. Sometimes one wants to work with unsigned integers. When this is the case, one can prepend any of the integer types with the word unsigned. Initially it might not seem important to have separate unsigned and signed types. After all, if the largest whole number that can be saved as an unsigned int is 65535, then why not just specify that all integers above 32767 actually represent negative numbers, and all the others are positive. Actually this works if one is only going to add and subtract signed integers. However when multiplying and dividing signed integers, it fails. The computer must therefore treat signed and unsigned integers differently, thus the two different types.

Following are some examples of variable declarations in C. You will also observe that it is possible to assign values to variables at the same time as declaring their types. This is called initialization of the variables, and is always good programming practice. Some of the declarations also give examples of declaring arrays as we did in the previous program.

    float radius = 2.0f;
    long int a1 = 58683746;
    short int b1 = 35;
    char a, b, c;
    unsigned long long int mylist[100];
    double parameters[3] = {2.345473845873e79, 3.57576345683e61, -2.187634487587e11};