Struct Keyword


In C programming, the 'struct' keyword is used to define a user-defined data type called a structure.

A structure is a composite data type that allows you to group related data items of different types under a single name.

Here's an example of using the 'struct' keyword to define a structure:


struct Point {
  int x;
  int y;
};

int main() {
  struct Point p1;
  p1.x = 10;
  p1.y = 20;
  printf("Coordinates: (%d, %d)\n", p1.x, p1.y);
  return 0;
}

    

In this example, the 'struct' keyword is used to define a structure named 'Point', which contains two integer data members: 'x' and 'y'.

Inside the 'main()' function, an instance of the 'Point' structure named 'p1' is declared and initialized with values for 'x' and 'y'.

The values are then printed using 'printf'.

Structures provide a way to organize related data into a single unit, making it easier to work with complex data.

They are widely used in C programming for representing real-world entities or creating more complex data structures.

Loading...

Search