C语言摘要 -- K&R笔记(5)

个人博客:SHIZHZ's Blogs

本文是对《K&R》第七章《Input and Output》的摘要。

1. getchar, putchar:针对字符的输入/输出,linux环境下可通过man或info命令查看帮助文档;

2. printf,格式化输出。

int printf(char *format, arg1, arg2, ...);

参数format指定输出格式,符号%格式化对应的后续参数,基本规则为:

a. 减号-,左对齐;

b. 数字,表示该输出项的宽度;

c. 点".",分隔宽度与精度;

d. 数字,表示该输出项的精度,如果输出项是float或double,则表示精确到小数点后的位数;如果输出项是字符串,则表示输出字符个数;

e. 数据类型字符,告诉输出函数该输出项的类型,具体列表如下:

宽度与精度可以用符号‘*’占位,然后作为参数传递给printf。示例:

#include <stdio.h>

int main (int argc, char const* argv[])
{
    char *str = "hello, world";
    int i = -567;
    float f = 3.1415926;
    printf("|%15.3s|\n", str);
    printf("|%-15.3s|\n", str);
    printf("|%-10s|\n", str);
    printf("|%10s|\n", str);
    printf("|%-15s|\n", str);
    printf("|%15s|\n", str);
    printf("|%-*.*s|\n", 15, 10, str);

    printf("|%5.2d|\n", i);
    printf("|%-5.2f|\n", f);
    printf("|%*.*f|\n", 8, 5, f);

    return 0;
}

结果为:

$ ./a.out 
|            hel|
|hel            |
|hello, world|
|hello, world|
|hello, world   |
|   hello, world|
|hello, wor     |
| -567|
|3.14 |
| 3.14159|

函数 int sprintf(char *string, char *format, arg1, arg2, ... )与printf一样,只是输出对象不是stdout,而是参数string。

3. 实现可变参数的函数。头文件<stdarg.h>包括了用于处理可变参数的宏和数据:

va_list:用来声明指向参数列表的变量,设变量为ap;

va_start:用来将ap初始化为指向第一个未命名参数;

va_arg:返回一个参数并将ap指向下一个未命名参数,该宏需要一个类型名作为参数,以便知道返回什么类型,以及调整ap时步长是多少;

va_end:清理工作,在函数return之前调用。

下面是一个简单的printf函数实现:

#include <stdarg.h>
#include <stdio.h>
void miniprintf(char *fmt, ...);
int main (int argc, char const* argv[])
{
    miniprintf("miniprintf: %d, %f, %s\n", 45, 12.2, "hello");
    return 0;
}

void miniprintf(char *fmt, ...)
{
    va_list ap;
    char *p, *s;
    int i;
    double d;
    va_start(ap, fmt);
    for(p = fmt; *p; ++p)
    {
        if(*p != '%')
        {
            putchar(*p);
            continue;
        }
        switch(*++p)
        {
            case 'd':
                i = va_arg(ap, int);
                printf("%d", i);
                break;
            case 'f':
                d = va_arg(ap, double);
                printf("%f", d);
                break;
            case 's':
                s = va_arg(ap, char *);
                for(; *s; ++s)
                {
                    putchar(*s);
                }
                break;
            default:
                putchar(*p);
        }
    }
    va_end(ap);
}

输出结果为:

$ ./a.out 
miniprintf: 45, 12.200000, hello

4. scanf, 格式化输入。

int scanf(char *fmt, ...)

与printf类似,格式化字符见下图:


5. 文件访问。在标准库<stdio.h>中定义了文件结构FILE,其中包括了文件描述符,buffer,读写状态,当前指针位置等信息。每个程序开始时OS会默认打开 3个文件流:标准输入流、标准输出流与标准错误流。在程序中可直接通过变量stdin, stdout, 与stderr进行访问。

FILE *fopen(char *name, char *mode): 打开文件流;

int fclose(FILE *fp): 关闭文件流。

文件使用完毕后必须关闭,因为可打开的文件数目是受限定的系统资源,而且关闭文件流可以自动刷新缓存。

6. 错误处理。

exit:在程序中调用exit()结束进程,main函数的return exrp 等同于exit(extp)。

int ferror(FILE *fp):如果文件流fp出错则返回非0整数;

int feof(FILE *fp):判断文件流是否到了末尾。

7. 行输入/输出。

char *fgets(char *line, int maxline, FILE *fp);

int fputs(char *s, FILE *fp);

type "man fgets" in shell to see more:)

8. 其它库函数。

<string.h> 字符串处理函数;

<ctype.h> 字符处理函数;

<math.h> 数学运算;

int ungetch(int c, FILE *fp):将字符放回文件流中;

*************************************************

简要摘录了些,详细google吧:)


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值