首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >学习C基础-结构功能:

学习C基础-结构功能:
EN

Stack Overflow用户
提问于 2022-11-17 15:46:23
回答 1查看 27关注 0票数 2

我目前正在尝试从struct函数创建一个程序,如下所示。我不想直接从程序中打印一行,而是修改程序以打印一条消息,要求用户输入关于某个人的信息,并按结构指示显示它。

而不是做"struct p“和插入信息,我希望它被输入为扫描。

代码语言:javascript
复制
#include <stdio.h>

typedef struct {
    int day, month, year;
} DATA;
typedef struct person {
    char name[100];
    int age;
    float salary;
    DATA birth;
} PERSON;

void Mostrar(struct person x){
    printf("name: %s\n",x.name);
    printf("age: %d\n",x.age);
    printf("Salário: %f\n",x.salary);
    printf("Dt.birth: %d/%d/%d\n",x.birth.day, x.birth.month, x.birth.year);
}

int main(){
    struct person p = {"Carlos", 23, 12345, {23,5,1954}};
    Mostrar(p);
}

谢谢你们的帮助!

EN

回答 1

Stack Overflow用户

发布于 2022-11-17 15:52:31

只需使用scanf

代码语言:javascript
复制
PERSON getPerson(void)
{
    PERSON person;
    if(!fgets(person.name, sizeof(person.name), stdin)) { /* error handling */} //you can also remove \n at the end if you need to
    if(scanf("%d %f", &person.age, &person.salary) != 2) { /* error handling */}
    if(scanf("%d %d %d", &person.birth.day, &person.birth.month, &person.birth.year) != 3) { /* error handling */}
    return person;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74478309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档