Ch.7 - Input and Output

    In this chapter we will describe the standard library, a set of functions that provide input and output, string handling, storage management, mathematical routines, and a variety of other services for C programs.

7.1 Standard Input and Output

    The simplest imput mechanism is to read one character at a time form the standard input, normally the keyboard, with getchar:
    int getchar(void)
getchar returns the next input character each time it is called, or EOF when it encounters end of file. The symbolic constant EOF is defined in <stdio.h>. The value is typically -1, but tests should be written in terms of EOF so as to be independent of the specific value.
   
In many environments, a file may be substituted for the key board by using the < convention for input redirection: if a program prog uses getchar, then the command line
    prog <infile
causes prog to read characters form infile instead. The switching of the input is done in such a way that prog itself is oblivious to the change; in particular, the string "<infile" is not included in the command-line arguments in argv, Input switching is also invisible if the input comes from another program via a pipe mechanism:on some systems, the command line 
    otherprog | prog
runs the two programs otherprog and prog, and pipes the standard output of otherprog into the standard input for prog.

7.2 Formatted Output - printf

int printf(char *format, arg1, arg2, ...);
printf converts, formats, and prints its arguments on the standard output under control of the format.

Each conversion specification begins with a % and ends with a conversion character. Between the % and the
conversion character there may be, in order:
·  A minus sign, which specifies left adjustment of the converted argument.
·  A number that specifies the minimum field width. The converted argument will be printed
in a field at least this wide. If necessary it will be padded on the left (or right, if left
adjustment is called for) to make up the field width.
·  A period, which separates the field width from the precision.
·  A number, the precision, that specifies the maximum number of characters to be printed
from a string, or the number of digits after the decimal point of a floating-point value, or
the minimum number of digits for an integer.
·  An h if the integer is to be printed as a short, or l (letter ell) if as a long.

A width or precision may be specified as *, in which case the value is computed by
converting the next argument (which must be an int). For example, to print at most
max characters from a string s,
printf("%.*s", max, s);
Most of the format conversions have been illustrated in earlier chapters. One exception is the
precision as it relates to strings. The following table shows the effect of a variety of specifications
in printing ``hello, world'' (12 characters). We have put colons around each field so you can see it
extent.
:%s: :hello, world:
:%10s: :hello, world:
:%.10s: :hello, wor:
:%-10s: :hello, world:
:%.15s: :hello, world:
:%-15s: :hello, world :
:%15.10s: : hello, wor:
:%-15.10s: :hello, wor :
A warning: printf uses its first argument to decide how many arguments follow and what their type
is. It will get confused, and you will get wrong answers, if there are not enough arguments of if
they are the wrong type. You should also be aware of the difference between these two calls:
    printf(s); /* FAILS if s contains % */
    printf("%s", s); /* SAFE */

to be continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值