CodeNewbie Community 🌱

Cover image for Structure in C Programming
Alimam Miya
Alimam Miya

Posted on

Structure in C Programming

In this tutorial, we are going to discuss what are structure in c programming, how to create structure variables, and how to accessing structure members. So, let’s start with this wonderful concept.

Introduction to Structure in C Programming

C also provides user-defined data types, like predefined data types (int, char, float, etc.). One such user-defined data type structure is. Structures are user-defined data types.

Defining a Structure

The structure is defined using the struct keyword, after this keyword is defined, a unique name of the structure is assigned. The curly braces ({) are then used to create the variables and terminate the program by placing a semicolon (;) after the ending curly bracket (}).

struct struct_Name
{
  structure member 1;
  structure member 2;
  ..................
  structure member N;
};
Enter fullscreen mode Exit fullscreen mode

Creating Structure Variables

These variables can be created with 2 methods.

  • With structure definition
  • Without structure definition

With Structure Definition

When we create variables with the Structure Definition type along with the structure definition, the variables are written by separating them with a comma (,) before the semicolon (;) of the end.
Like:-

struct shoes
{
  int price;
}t1,t2;
Enter fullscreen mode Exit fullscreen mode

Without Structure Definition

When we create variables without structure definition, then we use struct keyword. After using the struct keyword, the name of the structure is defined. You can define unlimited variables by separating from and then before the comma (,).
Like:-

struct shoes t1, t2, t3;
Enter fullscreen mode Exit fullscreen mode

Accessing Structure Members

We access Structure members for two reasons.

  • To assign values ​​to members
  • To print the values ​​of the members as output Whenever we have to access a structure member, we do it using the dot operator. Example -
#include<stdio.h>
/* Defining shoes structure */
struct shoes
{
  int price;
};
int main()
{
  /* Declaring variables of shoes structure */
  struct shoes t1;
  /* Assigning value to price of shoes */
  t1.price=999;
  printf(β€œThe Price of the shoes is: %d”,t1.price);
return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output

The price of the shoes is: 999
Enter fullscreen mode Exit fullscreen mode

Originally posted on - Structure in C Programming

Top comments (2)

Collapse
 
app3212 profile image
app3212 • Edited

In C programming, the structure is a user-defined data type that allows you to group different variables of different data types under a single name. It provides a way to represent a complex data structure by combining related variables into a single unit. Here's the syntax for defining a structure in C:

c
Copy code
struct structure_name {
data_type1 member1;
data_type2 member2;
//...
};
Let's break down the syntax:

struct: The keyword used to define a structure in C.
structure_name: The name given to the structure. This name is used to declare variables of this structure type.
data_type1, data_type2, etc.: The data types of the members (variables) within the structure.
member1, member2, etc.: The names of the members within the structure.
Here's an example to illustrate how to define and use a structure in C:

c
Copy code

include

// Define the structure
struct student {
char name[50];
int age;
float average_grade;
};

int main() {
// Declare a variable of the structure type
struct student s1;

// Accessing and modifying structure members
strcpy(s1.name, "John Doe");
s1.age = 20;
s1.average_grade = 85.5;

// Displaying structure members
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Average Grade: %.2f\n", s1.average_grade);

return 0;
}
In this example, we define a structure named student with three members: name, age, and average_grade. We declare a variable s1 of type struct student and access its members using the dot operator (.). We can assign values to the members and display them using printf statements for ehsaas-kafalat-program.

Structures are useful when you need to group related data together and manipulate them as a single entity. They provide a way to organize and represent complex data structures in C programming.

Collapse
 
jhone55 profile image
Jhone55

Structure in C Programming