『实例1』一个价值“三天”的bug【C语言实例105例】

源代码

#include <stdio.h>
#include <stdlib.h>
#define BUFFERSIZE 1024
int main()
{
    unsigned int a,b,sum;
    char buffer[BUFFERSIZE];
    printf("*************************************\n");
    printf("*   Welcome to use our counter      *\n");
    printf("*   Input two intergers in one line *\n");
    printf("*   The sum will be printed         *\n");
    printf("*   Input the char '#' to quit      *\n");
    printf("*************************************\n");

    while ((fgets(buffer,BUFFERSIZE,stdin) != NULL) && (buffer[0] != '#'))
    {
        if(sscanf(buffer,"%d%d",&a,&b)!=2)
        {
            printf("The input is skipped:%s",buffer);
            continue;
        }
        sum = a + b;
        printf("The sum of %d and %d is %d\n",a,b,sum);
    }

    return 0;
}

 

代码说明:

int fscanf (FILE *stream, char const *format, ...)

int scanf (char const *format, ...)
int sscanf (char const *string, char const *format, ...)
char *fgets (char *buffer, int BUFFER_SIZE, FILE *stream)
stdin为标准输入流
格式代码%后包括四种类型
1.星号(*):并不存储转换后的值,而是将其丢弃,用来跳过不需要的输入字符
2.宽度(一个非负整数):用来限制被读取转换的字符个数,在没有指定宽度的情况下,函数会连续读入字符知道遇到空白字符为止。
3.限定符
4.格式代码
d    十进制整型,有符号数
限定符h-short,l-long
u    十进制整型,无符号数
x    十六进制整型,无符号数
o    八进制整型,无符号数
限定符h-usigned short,l-unsigned long
f,e  一个浮点数
限定符l-double,L-long double
c   一个字符
s   一个字符串

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值