Broad Network


Standard Input, Standard Output and Standard Error in C

Part 43 of the Complete C Course

Foreword: Standard Input, Standard Output and Standard Error in C

By: Chrysanthus Date Published: 22 Jun 2024

Introduction

Standard input is input from the keyboard, and the predefined pointer to the keyboard stream is stdin. Standard output is output to the terminal (monitor/window), and the predefined pointer to the terminal stream is stdout. Standard error is still output to the terminal (monitor/window), and the predefined pointer for this is stderr. When a terminal command is typed, both the streams for stdin and stdout are active, from the operating system.

When the computer is running, the stdin, stdout and stderr streams are already opened. So they do not have to be reopened. They are ready to handle text. The functions for disk file streams are used for stdin, stdout and stderr. In this section of the chapter, only stdin and stdout streams are addressed. In order to see the effects of these functions on stdin, stdout and stderr, the terminal (console) window has to be opened, manually (using the OS interface).

The fwrite() Function and stdout
The following program sends three text lines to the terminal (window):

    #include <stdio.h>

    int main(int argc, char *argv[])
        {
            char str1[] = "I love you.\n";
            char str2[] = "Yes, I need you.\n";
            char str3[] = "Indeed, I want you.\n";
            
            fwrite(str1, 1, sizeof(str1)-1, stdout);
            fwrite(str2, 1, sizeof(str2)-1, stdout);
            fwrite(str3, 1, sizeof(str3)-1, stdout);

            return 0;
        }

The output at the terminal window is:

    I love you.
    Yes, I need you.
    Indeed, I want you.

The newline character to send the cursor to the next line below, at the output, has been included at the end of each text line. Where the disk file stream pointer had to be typed in the fwrite() function, stdout was typed. Note: the printf() function definition, uses stdout by default. Remember, an expression like "sizeof(str1)-1", omits the '\0' character.

The fputc() Function and stdout
The following program sends one line of text to the output, character by character:

    #include <stdio.h>

    int main(int argc, char *argv[])
        {
            char str[] = "I love you.\n";

            for (int i=0; i<sizeof(str)-1; i++)
                fputc(str[i], stdout);

            return 0;
        }

The for-loop has only one statement, so it does not have the braces for the block. Where the stream pointer was expected, stdout was typed. The output is:

    I love you.

The fread() Function and stdin
For the following program, 9 characters are typed on the keyboard. The characters are '1', '2', '3', '4', '5', '6', '7', '8', '\n'. The ninth character is the press of the Enter Key. After compilation, when the program is running, the short vertical character bar will be flashing at the cursor position, where the next character is expected to be typed. If any error is made while typing, the Backspace key can be pressed for erasure. Where the stream pointer is expected, stdin is typed.

    #include <stdio.h>

    int main(int argc, char *argv[])
        {
            char arr[9];

            fread(arr, sizeof(char), 9, stdin);
            printf("%s", arr);
            
            return 0;
        }

The display is:

    12345678
    12345678

The reason for having the first line is that, whenever a character is typed on the keyboard, it is echoed at the terminal (screen). Pressing the Enter Key, to send in '\n' causes the cursor to be sent to the line below. Note: to type a space, press the Space-bar long key.

The fgetc() Function and stdin
For the following program, type in the same 9 characters typed above, with the ninth character being, '\n' (the press of the Enter key). stdin has been typed where the stream pointer is expected.

    #include <stdio.h>

    int main(int argc, char *argv[])
        {
            char arr[9];
            
            for (int i=0; i<9; i++)
                arr[i] = fgetc(stdin);
                
            printf("%s", arr);
            
            return 0;
        }

The display is:

    12345678
    12345678

The second line was printed by a call to printf() function, and not a call to fwrite() or fputc().

End of the Complete C Course

This is the end of the Complete C Course. What follows next are articles in C by the same author. Just click the Next Button below, to continue reading (the articles).



Related Links

More Related Links

Cousins

BACK

Comments