Chapter 3 *Data Types

Contributed from The C DiskTutor by L. John Ribar. Converted to SGML and updated by Dave Chapeskie. Basic Variable Types and onward is written by Markus Delves.

Note: This chapter is still in need of work, it's currently a near exact copy of the chapter from The C DiskTutor [Ribar, 1992].

This chapter needs to cover:

  • integer types and constants

  • floating point types and constants

  • character types and constants

  • enumerations

  • pointers

  • structures and unions

  • arrays and strings

  • type modifiers (const, static, etc)



Before you begin to write programs in C, or in any computer language, you need to have a way to store the information that you will manipulate. This information can appear in many forms, so the C language has many different ways to handle it.

This chapter deals with the first round of data storage concepts in C. In the last chapter, you learnt how to name variables. Now, you will learn the basic variable types, and how to manipulate them in a program. These are the foundations upon which to build a C program.

3.1 Basic Variable Types

All programs are written to manipulate data in some shape or form. Therefore, data storage is very important in almost all computer programs written. Naturally, if your only purpose in writing a program is to display your name, data storage is not needed. Nor does a program which adds two numbers and prints the results, as shown in below, have much need of data storage.

    /* A simple C program with no variables. */
    
    #include <stdio.h>
    
    int
    main (void)
    {
        printf ("The sum of 4 plus 5 is %d\n", 4 + 5);
        return 0;
    }

A program that displays the result of adding two numbers is only useful if the numbers can be changed without rewriting the program. To do this, the two numbers must be stored in variables, which are then added. These variables can be entered by the user each time the program is run and Voila! You have created a simple adding program. Example 3-1 shows how a simple adding program might look.

Example 3-1. A simple adding program

    #include <stdio.h> (1)
    
    int main()
    {
        (2)
        int num1;    /* the first number to add  */
        int num2;    /* the second number to add */
        int sum;     /* the sum     */
    
        printf ("What is the first number? "); (3)
        scanf ("%d", &num1); (4)
    
        printf ("What is the second number? ");
        scanf ("%d", &num2);
    
        sum = num1 + num2; (5)
        printf ("The sum of %d and %d is %d\n", num1, num2, sum);
    
        return 0;
    }

A quick look over this program (which I will explain in a minute) and you can see that this block of code is much more dynamic. It doesn't rely on static values being placed into directly into the code but instead excepts values from which the user enters at runtime. When the users runs this program, it will ask for two numbers, then it'll print what the sum of the two numbers are.

Here is a step-by-step explanation of what the program does, and how the C language performs each of the required functions.

(1)
This line tells the compiler to read the file stdio.h, and to include its contents into this file. stdio.h is a standard header file used for certain input and output functions. The functions printf() and scanf() are declared in stdio.h, therefore we must include it (see XXX for a more complete discussion).
(2)
The below three lines declare variables of the type integer (int). An integer stores the value of a whole number. A declaration of a variable begins with the type, in this instance, int, and is followed by the variable name (see the discussion on this later in the chapter for more details).
(3)
This line contains a call to the function named printf. In this case, a parameter -- the string Hello world!\n -- is sent into the function by placing it between the parentheses. printf is a very common function, and is used throughout the book; it is covered in full detail in XXX.

The \n at the end of the string is a shortcut notation for a carriage return, which makes the cursor move to the beginning of the next line on the screen, after the string is printed. This is like pressing the carriage return on a typewriter after typing a line.

(4)
This line contains a call to the function named scanf. In this case, two parameters are sent to the function. The function scans for input of "%d", which is used for variables of type int. It then stores that value in the variable passed as the second parameter, which is num1 in the first case.

The scanf() function is too complicated to explain in it's entirely here (see XXX for a complete discussion). However, at this point in time, just be aware that it scans for input from the keyboard and stores it in the variable passed as the second parameter.

(5)
This is where the actual adding in the program is done. num1 is added with num2 and the sum of the two is assigned to the third variable sum. sum now contains the sum of num1 and num2.

Once you have entered this code into a file, such as add.c, you're ready to compile it.

From the command line type the following command:

gcc add.c -o add

Assuming you've made no errors the command should run without producing any visible output. It will have created an executable file add which you can now run.

After compiling and running this program, your terminal will look similar to this:

    % ls
    add.c
    % gcc add.c -o add
    % ls
    add
    add.c
    % ./add
    What is the first number? 12
    What is the second number? 31
    The sum of 12 and 31 is 43


This, and other documents, can be downloaded from http://opencbp.sourceforge.net/.

For questions about the Open C Book Project, read the documentation.