Skip to main content
Engineering LibreTexts

3.4: Libraries

  • Page ID
    35806
  • The examples above are rather limited because, although they perform a calculation, we have no way of seeing the result! We need some way to print the answer to the computer screen. To do this, we rely on system functions and libraries. There are a series of libraries included with most C development systems to cover a variety of needs. Essentially, someone has already coded, tested and compiled a bunch of functions for you. You add these functions to your program through a process called linking. Linking simply combines your compiled code along with any required library code into a final executable program. For basic printouts, data input, and the like, we use the standard IO (Input/Output) library, or stdio for short. There is a function in this library named printf() for “print formatted”. So that the compiler can do type checking, it must know something about this new function. We tell the compiler to look into a special file called a header file to find this information. Every library will have an associated header file (usually of the same name) and it will normally end with a .h file extension1. The compiler directive is called an include statement.

    Code \(\PageIndex{1}\) (C): Program with library function call.

    // Our third program, this is an example of a single line comment
    #include <stdio.h>
    
    void main( void )
    {
        printf(“Hello world.\n”);
    }
    

    This program simply prints the message Hello world. to the screen. The backslash-n combo is a special formatting token that means add a new line (i.e., bring the cursor to the line below). If we did not add the #include directive, the compiler wouldn’t know anything about printf(), and would complain when we tried to use it. So, what’s in a header file? Well, among other things they contain function prototypes. The prototypes are nothing more than a template. You can create your own by cutting and pasting your function name with argument list and adding a semicolon to it. Here is the function prototype for our earlier math function:

    float add_mult_div( float a, float b );
    

    You could make your own library of functions if you want. To use them, all you’d need is an appropriate include statement in your code, and remember to add in your library code with the linker. This will allow you to reuse code and save time. We will look at multiple file projects and more on libraries in a later segment.

    Consequently, if we want to print out the answer to the first program, we’d wind up with something like Code \(\PageIndex{2}\):

    Code \(\PageIndex{2}\) (C): A more complete program.

    /* Our fourth program */
    #include <stdio.h>
    
    void main( void )
    {
        float x = 2.0;
        float y = 3.0;
        float z;
    
        z = x*y/(x+y);
        
        printf(“The answer is %f\n”, z);
    }
    

    The %f in the printf() function serves as a place holder for the variable z. If you need to print several values you can do something like this:

    printf(“The answer from %f and %f is %f\n”, x, y, z);
    

    In this case, the first %f is used for x, the second %f for y, and the final one for z. The result will look like:

    The answer from 2.0 and 3.0 is 1.2
    

    1. It is worth noting that a large number of subgroups of code are collected together into what is referred to as the C standard library. The precise implementation of this is dependent on the operating system, however, the header files (e.g., stdio.h, string.h, math.h, etc.) remain distinct.
    • Was this article helpful?