Accessing the Printer in C

Back in the old days, the printer was a device wired to the computer and handled directly by whatever program wanted to use it. Printing in C involved opening the printer device and sending the data. Today, things work differently.

All printing is now managed by the operating system. In Linux, while the printer might be accessible as a file located in the /dev directory, it could also be a network device. Regardless, the operating system is the printer’s commanding officer. To write C code that sends something to the printer requires a study of the operating system’s Application Programming Interface (API). The approach is different for each operating system.

Yet, all hope isn’t lost.

The trick I use to print from C is to piggyback on another program that handles the printing job for me. After all, why re-invent the wheel?

In this case, the program is lp, a command line tool that prints in the Linux/Unix/macOS universe. If lp isn’t installed for your distro, use the OS package manager to obtain it. Alas, Windows lacks such a tool, so this lesson is primarily targeted to the Unix/Linux environment.

I suppose you could spawn the lp program to handle the printing task, but an easier approach is to open the program as a process. To perform this feat, use the popen() function, which I wrote about in a previous Lesson. Here’s the statement:

printer = popen("lp","w");

The popen() function opens the program lp for writing, "w". The return value is a FILE handle, printer. You can then use standard file I/O functions to print data, as the following code demonstrates:

2022_03_26-Lesson.c

#include <stdio.h>

int main()
{
    FILE *printer;

    printer = popen("lp","w");
    if( printer==NULL )
    {
        fprintf(stderr,"Unable to open `lp`\n");
        return(1);
    }

    fprintf(printer,"Hello, Mr. Printer!\n");

    pclose(printer);
    return(0);
}

Line 7 opens the lp program for input (writing). Line 8 confirms that the process opened successfully. If not, the program spews forth an error message and dies.

At Line 14, the fprintf() function sends a string of text to the printer; this program’s output becomes the lp program’s input.

Line 16 closes the process, signaling to the lp program that input is done. The lp program then ejects the page.

If only things were as uncomplicated in Windows.

A long time ago, Windows (and DOS before it) used the PRN device to represent the primary printer. You could also use LPT1, the computer’s first parallel port. Opening one of these devices as a file worked the same as opening any file. Once opened, you used standard file I/O commands to send information to the printer. In fact, I once used this command line to eject a sheet of paper:

echo ^L > prn

The control-L code ejects a page, even on today’s printers. But this command no longer works in Windows, neither does attempting to open the PRN device in your C programs.

Again, if you want to code for the printer directly in C, you must use the operating system’s API. This information is widely available, but specific to the given operating system.

Leave a Reply