Menu Close

写出C语言程序利用结构体储存数据并显示

Write a C program to Store Information in Structure and Display it


#include <stdio.h>
struct student
{
    char name[50];
    int roll;
    float marks;
}
    stud[2];
 
int main()
{
    int i;
 
    printf("Enter information of students:\n");
 
    // storing information
    for(i=0; i<2; ++i)
    {
        stud[i].roll = i+1;
 
        printf("\nFor roll number: %d\n",stud[i].roll);
 
        printf("Enter name: ");
        scanf("%s",stud[i].name);
 
        printf("Enter marks: ");
        scanf("%f",&stud[i].marks);
 
 
        printf("\n");
    }
 
    printf("Displaying Information:\n\n");
    // displaying information
    for(i=0; i<2; ++i)
    {
        printf("\nRoll number: %d\n",i+1);
        printf("Name: ");
        puts(stud[i].name);
        printf("Marks: %.1f",stud[i].marks);
 
        printf("\n");
    }
    return 0;
}

结果

除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Leave the field below empty!

Posted in C语言习题集

Related Posts