The C Programming Language 笔记(3)

Chapter 7: Input and Output


    Input and output are not part of the C language itself.  
    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 services for C porgrams. We will concentrate on input and output.
  
   
Formatted Ouput - Printf
    int printf(char *format, arg1, arg2, ...)
    it returns the number of characters outputed.
     
    The function sprintf does not the same conversions as printf does, but storage the output in a string.
    int sprintf(char *string, char *format, arg1, arg2, ...)
    string must be big enough to reveice the result.

   
Variable-length argument lists:
    the proper declaration for printf is:
    int printf(cahr *fmt, ...)    /* The declaration ... can only appear at the end of an argument list */
    The tricky bit is how printf walks along the argument list when the list doesn't even have a name. The standard header <stdarg.h> contains a set of macro definitions that define how to step through an argument list.
    The type va_list is used to declare a variable that will refer to each argument in turn; (maybe i can name it "ap" - argument pointer). The va_start initializes ap to point to the first unnamed arguments. it must be called once before ap is used. There must be at least one named argument; the final named argument is used by va_start to get started.
    Each call of va_arg returns one argument and steps ap to the next; va_arg uses a type name to determine what type to return and how big a step to take. Finally, va_end does whatever cleanup is necessary. It must be called before the function returns.
     
  
Formated Input - Scanf
    int scanf(char *fmt, ...)
    scanf returns as its value the number of successfully matched and assigned input items.
   
    int sscanf(char *string, char *fmt, ...)
    It scans the string according to the format fmt, and stores the resulting values through arg1, arg2, etc. These arguments must be pointers.
   
    For example:
     
   
File Access:
    When a C program is started, the operating system enviroment is responsible for opening three files and providing file pointers for them. These files are the standard input, the standard output, the standard error; the corresponding file pointers are called stdin, stdout, and stderr, and are declared in <stdio.h>
    

 

Miscellaneous Functions:

 

    string operations:

   

 

    Characters class testing and conversion:

   

 

    ungetc: The standard library provides a function ungetc that pushed the character c back onto file fp, and return either c, or EOF for an error.

   

   Only one character of pushback is guaranteed per file. ungetc may be used with any of the input functions like scanf, getc, or getchar.

 

    Storage management:

   

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值