不推荐使用功能

时间:2019-02-10 21:34:10

标签: c scanf updates

我开始学习C语言,经过短暂的休息后,我再次开始学习C语言,并且对新的更新感到困惑。此代码无效。我现在不知道该怎么办。如果我从scanf更改为scanf_s,则无论如何都行不通。我也尝试更改类型的声明。在这里的人可以解释如何更改此代码以及如何使用scanf或scanf_s,以及在什么情况下使用它们。提前致谢。

#include <stdio.h>
#define N 256

typedef struct
{
   char name[N];
    char surname[N];
    char street[N];
    char city[N];
   char district[3];
    int n_house;
    int day, month, year;

} person;


main()
{

    person s;

    printf("ask name\n");
   scanf("%s", s.name);

   printf("ask sur\n");
   scanf("%s", s.surname);

   printf("ask wh h lives\n");
   scanf("%s %s %s %d", s.street, s.city,      s.district, &s.n_house);

    printf("ask bd\n");
    scanf("%d/%d/%d", &s.day, &s.month,     &s.year);

    printf("personal data of the person :     \n");
   printf("%s %s\n Nato il %d %d %d \n    Vive in %s %s %s %d", s.name,    s.surname, s.day, s.month, s.year,    s.street, s.city, s.district, s.n_house);

}

错误是C4996 scanf。此函数或变量可能不安全。考虑改用scanf_s。要禁用折旧,请使用_CTR_SECURE_NO_WARNINGS。

1 个答案:

答案 0 :(得分:0)

我对您的程序进行了一些小的修改:

#include <stdio.h>
#define N 256

typedef struct
{
    char name[N];
    char surname[N];
    char street[N];
    char city[N];
    char district[3];
    int n_house;
    int day, month, year;

} person;


void main(void)
{

    person s;

    printf("ask name\n");
    scanf("%s", s.name);

    printf("ask sur\n");
    scanf("%s", s.surname);

    printf("ask wh h lives\n");
    scanf("%s %s %s %d", s.street, s.city, s.district, &s.n_house);

    printf("ask bd\n");
    scanf("%d/%d/%d", &s.day, &s.month, &s.year);

    printf("personal data of the person :     \n");
    printf("%s %s\n Nato il %d %d %d\nVive in %s %s %s %d\n",
           s.name, s.surname,
           s.day, s.month, s.year,
           s.street, s.city, s.district, s.n_house);

    return;
}

并与输入配合使用:

ask name
george
ask sur
mac
ask wh h lives
barcelona sskk ksl 123
ask bd
12/13/1111
personal data of the person :
george mac
Nato il 12 13 1111
Vive in barcelona sskk ksl 123

您正在尝试这种输入吗?