Using structures, write a simple declaration for teams in the Cameroon football league.

Source Code

Brief explanation is provided after the source code.

#include 

typedef struct {
    char *name[45];
    long int license_number;
    char *local_league[25];
    char *created_date[10];
} TEAM;

int main(int argc, char ** argv) {
    TEAM team;
    printf("Name: ");  scanf("%s", team.name);
    printf("\nLicense Number: ");  scanf("%ld", &team.license_number);
    printf("\nLocal League: ");  scanf("%s", team.local_league);
    printf("\nCreated Date: ");  scanf("%s", team.created_date);

    printf("\nName: ");  printf("%s", team.name);
    printf("\nLicense Number: ");  printf("%ld", team.license_number);
    printf("\nLocal League: "); printf("%s", team.local_league);
    printf("\nCreated Date: ");  printf("%s\n", team.created_date);

    return 0;
}

When you compile and execute the above program it produces the following result on Linux:

Name: Cotonsport

License Number: 5672145

Local League: Elite_One

Created Date: 31-10-2015

Name: Cotonsport
License Number: 5672145
Local League: Elite_One
Created Date: 31-10-2015

Brief Explanation

  • A structure in C is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. As seen in this example, the structure TEAM has variables name, license_number, local_league and created_date.
  • Using typedef, TEAM is created as a synonym for the structure.
  • A variable team of type TEAM (the structure) is defined in the main function.
  • scanf function is then used to read data from input and assign to the fields define in the structure.
  • Using printf function, the inputted data is displayed to the stdout screen.

Add comment


Security code
Refresh