有关scanf()和fgets()函数的使用方法

一、scanf()函数

  1. scanf()输入结束符:   回车符 和 空格符

    例子:

#include<stdio.h>

int main(){
    char a[1024] = {'0'};
    scanf("%s\n",a);
    printf("%s\n",a);

    return 0;
}

当输入时,1 23 guah 

输出为1

 

2.scanf()由于是回车符结束(或者空格符结束),所以在缓存区里会有回车符,在循环使用scanf()函数做输入时,会出现程序进入死循环。

首先 int scanf(const char *format, ...)函数的返回值为int型,表示正确输入的个数,如:

input = scanf("%d %d",&a,&b);

当输入  1 2(回车) (1 和 2 都是整形 )

输出input = 2;

当输入 c 1(回车)

input = 1;

所以可以根据输入是否正确来判断是否进行后续操作:

#include<stdio.h>

int main(){
    int a = 0,b = 1;
    while(!(scanf("%d",&a)))
    {
        printf("%d",b)
    }
    printf("%d\n",a);
    return 0;
}

代码中是只有输入整数时,程序才执行打印a的值,代码输入错误时,打印b的值,然后继续输入,

但是输入错误时,由于没有提示信息,不方便操作,所以加入提示信息,代码如下:

#include<stdio.h>

int main(){
    int a = 0,b = 1;
    while(!(scanf("%d",&a)))
    {
        printf("%d",b);
        printf("please input correct type a!\n");
    }

    printf("%d\n",a);
    return 0;
}

在执行时,出现了无限循环

afc6881f1e3e4ace9c5d42e3a9284df4.png

 啥原因?只是加了一行  printf("please input correct type a!\n");

原来在printf语句中,最后一行有一个\n,\n作为scanf()的输入了,这时就会出现输入错误,导致了程序的无限循环。解决方法是:在printf后面加入getchar(),用于接收\n,此时就清除了缓冲区中的\n,程序能正常运行

#include<stdio.h>

int main(){
    int a = 0,b = 1;
    while(!(scanf("%d",&a)))
    {
        printf("%d",b);
        printf("please input correct type a!\n");
        getchar();
    }

    printf("%d\n",a);
    return 0;
}
w
1please input correct type a!
e
1please input correct type a!
2
2

所以在使用scanf()函数会出现很多不容易发现的致命错误。

二、fgets()函数

        为了避免scanf()输入会容易遇到一些错误,在程序中尽量使用fgets()函数做输入函数,fgets()函数时读取一行数据。注意读取的最大的空间是sizeof(str);

# include <stdio.h>
int main(void)
{
    char str[30];
    printf("请输入字符串:");
    fgets(str, sizeof(str), stdin);  
    printf("%s", str);  
    return 0;
}

当输入时,1 23 guah 

输出 1 23 guah 

 

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用fgets函数代替scanf函数的步骤如下: 1. 定义一个字符数组用于存储用户输入的字符串,数组大小要足够大,以免输入的字符串超出数组范围。 ``` char input[100]; ``` 2. 使用fgets函数读取用户输入的字符串,fgets函数的第一个参数是字符数组,第二个参数是数组大小(包括了字符串结束符),第三个参数是标准输入流(通常是stdin)。 ``` fgets(input, sizeof(input), stdin); ``` 3. 如果fgets函数读取成功,会返回指向输入字符串的指针,否则返回空指针。可以通过判断返回值是否为空指针来检查读取是否成功。如果读取成功,输入的字符串会以换行符结尾,可以使用strchr函数将其替换为字符串结束符。 ``` if (fgets(input, sizeof(input), stdin) != NULL) { char *newline = strchr(input, '\n'); if (newline != NULL) { *newline = '\0'; } } ``` 4. 可以使用sscanf函数将字符串转换为需要的类型,sscanf函数的第一个参数是输入字符串,第二个参数是格式化字符串,后面的参数是需要转换的变量。可以根据需要进行错误处理。 ``` int n; if (sscanf(input, "%d", &n) == 1) { printf("你输入的数字是:%d\n", n); } else { printf("输入格式不正确!\n"); } ``` 完整的代码如下: ``` #include <stdio.h> #include <string.h> int main() { char input[100]; printf("请输入一个数字:"); if (fgets(input, sizeof(input), stdin) != NULL) { char *newline = strchr(input, '\n'); if (newline != NULL) { *newline = '\0'; } int n; if (sscanf(input, "%d", &n) == 1) { printf("你输入的数字是:%d\n", n); } else { printf("输入格式不正确!\n"); } } return 0; } ``` 注意:fgets函数读取的字符串结尾会包含一个换行符,需要将其替换为字符串结束符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值