Struct In C

Vishal Rashmika published on
1 min, 71 words

Categories: C

What are structs

  • collection of related members ("variables")
  • very similar to classes in other programming languages
#include <stdio.h>
#include <string.h>

struct Player
{
  char name[12];
  int score;
};

int main(){

  struct Player player1;
  struct Player player2;

  strcpy(player1.name, "Adam");
  player2.score = 4;

  strcpy(player2.name, "Peter");
  player2.score = 6;

  printf("%s\n", player1.name);
  printf("d\n", player1.score);


  printf("%s\n", player2.name);
  printf("d\n", player2.score);

  return 0;
}