0% found this document useful (0 votes)
59 views

Unit 5 - Handling Standard Input and Output

1) The document discusses standard input/output in C programming. There are three standard streams - stdin, stdout, and stderr which are linked by default to keyboard, screen, and screen respectively. 2) It introduces functions like getc(), getchar(), putc(), and putchar() to read from and write to these standard streams. getc() and getchar() read a character from stdin while putc() and putchar() write a character to stdout. 3) It also discusses printf() formatting strings and how minimum field widths can be specified to control output formatting.

Uploaded by

barnabas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Unit 5 - Handling Standard Input and Output

1) The document discusses standard input/output in C programming. There are three standard streams - stdin, stdout, and stderr which are linked by default to keyboard, screen, and screen respectively. 2) It introduces functions like getc(), getchar(), putc(), and putchar() to read from and write to these standard streams. getc() and getchar() read a character from stdin while putc() and putchar() write a character to stdout. 3) It also discusses printf() formatting strings and how minimum field widths can be specified to control output formatting.

Uploaded by

barnabas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT -5 : Handling Standard Input and Output

Understanding Standard I/O

A file contains related characters and numbers. Because all characters and numbers
are represented in bits on computers, and a byte is a series of bits, the C language
treats a file as a series of bytes. A series of bytes is also called a stream. In fact, the C
language treats all file streams equally, although some of the file streams may come
from a disk or tape drive, from a terminal, or even from a printer.

Additionally, in C, there are three file streams that are pre-opened for you — that is to
say, they are always available for use in your programs:

• stdin—The standard input for reading.


• stdout—The standard output for writing.
• stderr—The standard error for writing error messages.

Usually, the standard input (stdin) file stream links to your keyboard, while the
standard output (stdout) and the standard error (stderr) file streams point to your
terminal screen. Also, many operating systems allow the user to redirect these file
streams.

In fact, you’ve already used stdout. When you called the printf() function in the
previous lesson, you were actually sending the output to the default file stream,
stdout, which points to your screen.

These days, typing on the keyboard is still the de facto standard way to input
information into computers. The C language has several functions to tell the computer
to read input from the user (typically through the keyboard.) In this lesson the getc()
and getchar() functions are introduced.

Using the getc() Function

The getc() function reads the next character from a file stream, and returns the
character as an integer.

1: /* Reading input by calling getc() */


2: #include <stdio.h>
3:
4: main()
5: {
6: int ch;
7:
8: printf(“Please type in one character:\n”);
9: ch = getc( stdin );
10: printf(“The character you just entered is: %c\n”, ch);
11: return 0;
12: }

You see that the header file stdio.h is included for both
the getc() and printf() functions used in the program. Lines 4–12 give the
name and body of the main() function.

In line 6, an integer variable, ch, is declared; it is assigned the return value from the
getc() function later in line 9. Line 8 prints out a message that asks the user to enter
one character from the keyboard. As I mentioned earlier in this lesson, the printf()
function call in line 8 uses the default standard output stdout to display messages on
the screen.

In line 9, the standard input stream stdin is passed to the getc() function, which
indicates that the file stream is from the keyboard. After the user types in a character,
the getc() function returns the numeric value (that is, an integer) of the character. You
see that, in line 9, the numeric value is assigned to the integer variable ch.

Then, in line 10, the character entered by the user is displayed on the screen via the
help of printf(). Note that the character format specifier (%c) is used within the printf()
function call in line 10.

Using the getchar() Function

The C language provides another function, getchar(), that performs a similar function
to getc(). More precisely, the getchar() function is equivalent to getc(stdin).

1: /* Reading input by calling getchar() */


2: #include <stdio.h>
3:
4: main()
5: {
6: int ch1, ch2;
7:
8: printf(“Please type in two characters together:\n”);
9: ch1 = getc( stdin );
10: ch2 = getchar( );
11: printf(“The first character you just entered is: %c\n”, ch1);
12: printf(“The second character you just entered is: %c\n”, ch2);
13: return 0;
14: }
TYPE

The program above is quite similar to the one discussed earlier, except that
this one reads in two characters. The statement in line 6 declares two integers, ch1
and ch2.
Line 8 displays a message asking the user to enter two characters together.

Then, the getc() and getchar() functions are called in lines 9 and 10, respectively, to
read in two characters entered by the user. Note that in line 10, nothing is passed to
the getchar() function. This is because, as mentioned earlier, getchar() uses the default
input file stream—stdin. You can replace the getchar() function in line 10 with
getc(stdin) because getc(stdin) is equivalent to getchar().

Lines 11 and 12 send two characters (kept by ch1 and ch2, respectively) to the screen.

Printing Output on the Screen

Besides getc() and getchar() for reading, the C language also provides two functions,
putc() and putchar(), for writing. The following two sections introduce these functions.

Using the putc() Function

The putc() function writes a character to the specified file stream, which, in your case,
is the standard output pointing to your screen.

The putc() function is used in the program below to put the character A on the screen.

1: /* Outputting a character with putc() */


2: #include <stdio.h>
3:
4: main()
5: {
6: int ch;
7:
8: ch = 65; /* the numeric value of A */
9: printf(“The character that has numeric value of 65 is:\n”);
10: putc(ch, stdout);
11: return 0;
12: }
5
As mentioned, the header file stdio.h, containing the declaration of putc(), is
included in line 2. The integer variable, ch, declared in line 6, is assigned the numeric
value of 65 in line 8.
The numeric value of character A is 65 in the ASCII character set.
Line 9 displays a message to remind the user of the numeric value of the character
that is going to be put on the screen. Then, the putc() function in line 10 puts
character A on the screen. Note that the first argument to the putc() function is the
integer variable (ch) that contains 65, and the second argument is the standard
output file stream, stdout.
Another Function for Writing: putchar()

Like putc(), putchar() can also be used to put a character on the screen. The only
difference between the two functions is that putchar() needs only one argument to
contain the character. You don’t need to specify the file stream because the standard
output (stdout) is set as the file stream to putchar().

1: /* Outputting characters with putchar() */

2: #include <stdio.h>
3:
4: main()
5: {
6: putchar(65);
7: putchar(10);
8: putchar(66);
9: putchar(10);
10: putchar(67);
11: putchar(10);
12: return 0;
13: }
ANALYSIS

After running the executable file, I get the following output:


A
B

The way to write the program above is a little bit different. There is no
variable declared in the program. Rather, integers are passed to putchar()
directly, as shown in lines 6–11.
As you might have figured out, 65, 66, and 67 are, respectively, the numeric values of
characters A, B, and C in the ASCII character set.

Therefore, respectively, lines 6 and 7 put character A on the screen and cause the
computer to start at the beginning of the next line. Likewise, line 8 puts B on the
screen, and line 9 starts a new line. Then, line 10 puts C on the screen, and line 11
starts another new line. Accordingly, A, B, and C, are put at the beginning of three
consecutive lines, as shown in the output section.

Revisiting the printf() Function

The printf() function is the first C library function you used in this book to print out
messages on the screen. printf() is a very important function in C, so it’s worth it to
spend more time on it.

The figure below shows the relationship between the format string and expressions.
Note that the format specifiers and the expressions are matched in order from left to
right.

Please remember that you should use exactly the same number of expressions as the
number of format specifiers within the format string.

The following are some of the format specifiers that can be used in printf():

%c The character format specifier.


%d The integer format specifier.
%i The integer format specifier (same as %d).
%f The floating-point format specifier.
%e The scientific notation format specifier (note the lowercase e).
%E The scientific notation format specifier (note the uppercase E).
%s The string format specifier.
%u The unsigned integer format specifier.
%x The unsigned hexadecimal format specifier (note the lowercase x).
%X The unsigned hexadecimal format specifier (note the uppercase X).
%p Displays the corresponding argument that is a pointer.

Specifying the Minimum Field Width

The C language allows you to add an integer between the percent sign (%) and the
letter in a format specifier. The integer is called the minimum field width specifier
because it specifies the minimum field width and ensures that the output reaches the
minimum width.

For example, in %10f, 10 is a minimum field width specifier that ensures that the
output is at least 10 character spaces wide. This is especially useful when printing out
a column of numbers.
1: /* Specifying minimum field width */
2: #include <stdio.h>
3:
4: main()
5: {
6: int num1, num2;
7:
8: num1 = 12;
9: num2 = 12345;
10: printf(“%d\n”, num1);
11: printf(“%d\n”, num2);
12: printf(“%5d\n”, num1);
13: printf(“%05d\n”, num1);
14: printf(“%2d\n”, num2);
15: return 0;
16: }

The following is the output I obtain by running the executable file.

12
12345
12
00012
12345

The two integer variables, num1 and num2, are declared in line 6, and
assigned 12 and 12345, respectively, in lines 8 and 9.

Without using any minimum field width specifiers, lines 10 and 11 print out the two
integers by calling the printf() function. You can see in the output section that the
output from the statement in line 10 is 12, which takes two character spaces, while
the output 12345 from line 11 takes five character spaces.

In line 12, a minimum field width, 5, is specified by %5d. The output from line 12
therefore takes five character spaces, with three blank spaces plus two character
spaces of 12.

The %05d in printf(), shown in line 13, indicates that the minimum field width is 5,
and the 0 indicates that zeros are used to fill, or “pad,” the spaces. Therefore, you see
the output made by the execution of the statement in line 13 is 00012. The %2d in
line 14 sets the minimum field width to 2, but you still see the full-size output
of 12345 from line 14. This means that when the minimum field width is shorter than
the width of the output, the latter is taken, and the output is still printed in full.
Aligning Output

By default, all output is placed on the right edge of the field, as long as the field
width is longer than the width of the output.

You can change this and force output to be left-justified. To do so, you need to prefix
the minimum field specifier with the minus sign (-). For example, %-12d specifies the
minimum field width as 12, and justifies the output from the left edge of the field.

1: /* Aligning output */
2: #include <stdio.h>
3:
4: main()
5: {
6: int num1, num2, num3, num4, num5;
7:
8: num1 = 1;
9: num2 = 12;
10: num3 = 123;
11: num4 = 1234;
12: num5 = 12345;
13: printf(“%8d %-8d\n”, num1, num1);
14: printf(“%8d %-8d\n”, num2, num2);
15: printf(“%8d %-8d\n”, num3, num3);
16: printf(“%8d %-8d\n”, num4, num4);
17: printf(“%8d %-8d\n”, num5, num5);
18: return 0;
19: }

I get the following output displayed on the screen of my computer after I run the
executable exe

1 1
12 12
123 123
1234 1234
12345 12345

In the above example, there are five integer variables, num1, num2, num3, num4, and
num5, that are declared in line 6 and are assigned values in lines 8–12.

These values represented by the five integer variables are then printed out by the
printf() functions in lines 13–17.

Note that all the printf() calls have the same first
argument: “%8d %-8d\n”. Here the first format specifier, %8d, aligns the output at the
right edge of the field, and the second specifier, %-8d, aligns the output to the left
edge of the field.

After the execution of the statements in lines 13–17, the alignment is accomplished
and the output is put on the screen in the format shown.

Using the Precision Specifier

You can put a period . and an integer right after the minimum field width specifier.
The combination of the period (.) and the integer makes up a precision specifier.

You can use the precision specifier to determine the number of decimal places for
floating-point numbers, or to specify the maximum field width (or length) for integers
or strings.

For instance, with %10.3f, the minimum field width length is specified as 10
characters long, and the number of decimal places is set to 3. (Remember, the default
number of decimal places is 6.)

For integers, %3.8d indicates that the minimum field width is 3, and the maximum
field width is 8.

1: /* precision specifiers */
2: #include <stdio.h>
3:
4: main()
5: {
6: int int_num;
7: double flt_num;
8:
9: int_num = 123;
10: flt_num = 123.456789;
11: printf(“Default integer format: %d\n”, int_num);ANALYSIS
12: printf(“With precision specifier: %2.8d\n”, int_num);
13: printf(“Default float format: %f\n”, flt_num);
14: printf(“With precision specifier: %-10.2f\n”, flt_num);
15: return 0;
16: }

Output:
Default integer format: 123
With precision specifier: 00000123
Default float format: 123.456789
With precision specifier: 123.46

The program declares one integer variable, int_num, in line 6, and


one floating-point number, flt_num, in line 7.
Lines 9 and 10 assign 123 and 123.456789 to int_num and flt_num, respectively.

In line 11, the default integer format is specified for the integer variable, int_num,
while the statement in line 12 specifies the integer format with a precision specifier
that indicates that the maximum field width is eight characters long. Therefore, you
see that five zeros are padded prior to the integer 123 in the second line of the output.

For the floating-point variable, flt_num, line 13 prints out the floating-point value in
the default format, and line 14 reduces the decimal places to two by putting the
precision specifier .2 within the format specifier %-10.2f.

Note here that left-justification is also specified by the minus sign (-) in the floating-
point format specifier.

The floating-point number 123.46 in the fourth line of the output is produced by the
statement in line 14 with the precision specifier for two decimal places. Therefore,
123.456789 rounded to two decimal places becomes 123.46.

You might also like