C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Structures in C

Store cricket players’ details

C Program: Store cricket players’ details

C

#include <stdio.h>

#include <string.h>

 

// Define structure for cricket player

struct Player {

    int id;

    char name[50];

    char team[50];

    int matches;

    float average;

};

 

int main() {

    struct Player p[100];

    int n, i;

 

    printf("Enter number of players: ");

    scanf("%d", &n);

    getchar(); // clear input buffer

 

    // Input player details

    for (i = 0; i < n; i++) {

        printf("\nEnter details for Player %d:\n", i + 1);

 

        printf("Player ID: ");

        scanf("%d", &p[i].id);

        getchar();

 

        printf("Name: ");

        gets(p[i].name);

 

        printf("Team: ");

        gets(p[i].team);

 

        printf("Matches Played: ");

        scanf("%d", &p[i].matches);

 

        printf("Batting Average: ");

        scanf("%f", &p[i].average);

    }

 

    // Display player details

    printf("\n===== CRICKET PLAYERS DETAILS =====\n");

    for (i = 0; i < n; i++) {

        printf("\nPlayer %d Details:\n", i + 1);

        printf("ID              : %d\n", p[i].id);

        printf("Name            : %s\n", p[i].name);

        printf("Team            : %s\n", p[i].team);

        printf("Matches Played  : %d\n", p[i].matches);

        printf("Batting Average : %.2f\n", p[i].average);

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter number of players: 2

Enter details for Player 1:
Player ID: 101
Name: Virat Kohli
Team: India
Matches Played: 254
Batting Average: 57.9

Enter details for Player 2:
Player ID: 102
Name: Steve Smith
Team: Australia
Matches Played: 150
Batting Average: 59.1

===== CRICKET PLAYERS DETAILS =====

Player 1 Details:
ID              : 101
Name            : Virat Kohli
Team            : India
Matches Played  : 254
Batting Average : 57.90

Player 2 Details:
ID              : 102
Name            : Steve Smith
Team            : Australia
Matches Played  : 150
Batting Average : 59.10


Explanation

Concept

Description

struct Player

Holds details about each player (ID, name, team, matches, and average).

p[100]

Array of structures to store multiple player records.

Input Loop

Collects information for all players.

Display Loop

Prints the stored data neatly in a formatted way.